[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

仅用批处理生成所有 ASCII 字符

对于获取 ASCII 全范围(0x00 -- 0xFF)字符, 本论坛上原本就有一篇由 plp626  所发:
扩展ASCII码字符集0x00~0xff 批处理获取函数
http://www.bathome.net/viewthread.php?tid=12347

本帖转载 dostips 上不同方式的实现
http://www.dostips.com/forum/viewtopic.php?f=3&t=5326

原文标题:
"Create nul and all ascii characters with only batch"
-- 译: 只用批处理生成 nul 和所有 ASCII 字符

其实 NUL (码值: 0) 也是一个 ASCII 字符, 只是由于它的特殊性和处理上的困难, 所以作者特别强调了一下吧.

先贴出两个提速稳定版: carlos 版 和 dbenham 版, 最后再贴出一个初始稳定版.

关于代码的详细说明, 请阅读 dostips 原帖, 转载到此, 主要为备份之用.

carlos 提速稳定版:
  1. @echo off
  2. :gen255
  3. ::generates 0-255 .chr files that have 1 byte
  4. ::Teamwork of carlos, penpen, aGerman, dbenham, einstein1969
  5. ::Tested under Win8, WinXP(64mb)
  6. setlocal enableextensions disabledelayedexpansion
  7. set "map=paddingpaddingpaddingpaddingpadding"
  8. set "map=%map%#$%%&'()*+,-./0123456789:;<p>?@"
  9. set "map=%map%ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`"
  10. set "map=%map%abcdefghijklmnopqrstuvwxyz{|}~"
  11. set "map=%map: =%"
  12. set "gchr=(type nul >%%#.tmp"
  13. set "gchr=%gchr% & makecab /d compress=off"
  14. set "gchr=%gchr% /d reserveperfoldersize=%%#"
  15. set "gchr=%gchr% /d reserveperdatablocksize=26"
  16. set "gchr=%gchr% %%#.tmp %%#.chr 1>nul"
  17. set "gchr=%gchr% & type %%#.chr"
  18. set "gchr=%gchr% |((for /l %%_ in (1 1 38) do pause) 1>nul"
  19. set "gchr=%gchr% & findstr ^^^^ 1>%%#.tmp)"
  20. set "gchr=%gchr% & copy /y %%#.tmp /a %%#.chr /b 1>nul"
  21. set "gchr=%gchr% & del %%#.tmp)"
  22. set "gmap=for /l %%# in (35 1 126) do"
  23. set "gmap=%gmap% set /p "=!map:~%%#,1!" 0<nul 1>%%#.chr"
  24. set "new_thread=start /i /w /b cmd /a /q /d /c"
  25. if exist ???.chr del /a /f /q ???.chr
  26. %new_thread% "for /l %%# in (127 1 169) do %gchr%"
  27. %new_thread% "for /l %%# in (170 1 211) do %gchr%"
  28. %new_thread% "for /l %%# in (212 1 255) do %gchr%"
  29. for /l %%# in (0 1 34) do %gchr%
  30. cmd /a /v:on /q /d /c "%gmap%"
  31. :gen255w
  32. ping -l 0 -n 1 -w 100 1.0.0.0 >nul
  33. for %%# in (169.chr 211.chr 255.chr) do (
  34. if not exist %%# goto :gen255w
  35. if not "1"=="%%~z#" goto :gen255w)
  36. for %%# in (61) do %gchr%
  37. copy /y nul + nul /a 26.chr /a 1>nul
  38. goto :eof
复制代码
dbenham 提速稳定版:
  1. @echo off
  2. setlocal
  3. :genAllChr
  4. ::This code creates 256 1 byte files, one for each possible byte value.
  5. ::This is encoded in a macro way to be called asynchronously with start cmd /c
  6. ::Teamwork of carlos, penpen, aGerman, dbenham, einstein1969
  7. ::Tested under Win7 and XP
  8. set ^"genchr=(^
  9. for /l %%N in (%%A !cnt! 255) do (^
  10.   if %%N equ 26 (^
  11.     copy /y nul + nul /a 26.chr /a ^>nul^
  12.   ) else (if %%N geq 35 if %%N leq 126 if %%N neq 61 (^
  13.     ^<nul set /p "=!ascii:~%%N,1!" ^>%%N.chr^
  14.   ))^&^
  15.   if not exist %%N.chr (^
  16.     makecab /d compress=off /d reserveperdatablocksize=26 /d reserveperfoldersize=%%N %%A.tmp %%N.chr ^>nul^&^
  17.     type %%N.chr ^| ((for /l %%n in (1 1 38) do pause)^>nul^&findstr "^^" ^>%%N.temp)^&^
  18.     ^>nul copy /y %%N.temp /a %%N.chr /b^&^
  19.     del %%N.temp^
  20.   )^
  21. ))^&^
  22. del %%A.tmp^"
  23. del /f /q /a *.chr >nul 2>&1
  24. set "ascii=                                   #$%%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
  25. set /a cnt=number_of_processors
  26. if %cnt% lss 1 set cnt=1
  27. if %cnt% gtr 256 set cnt=256
  28. set /a "end=cnt-1"
  29. for /l %%A in (0 1 %end%) do (
  30.   type nul >%%A.tmp
  31.   if %%A equ %end% (
  32.     cmd /q /v:on /c "%genchr%"
  33.   ) else (
  34.     start "" /b cmd /q /v:on /c "%genchr%"
  35.   )
  36. )
  37. :genAllChr.check
  38. for /l %%N in (0 1 %end%) do if exist %%N.tmp goto :genAllChr.check
  39. exit /b
复制代码
初始稳定版:
  1. REM This code creates 256 files containing one single Byte each from 0x00 until 0xFF
  2. REM Teamwork of carlos, penpen, aGerman, dbenham
  3. REM Tested under Win2000, XP, Win7, Win8
  4. @echo off
  5. setlocal EnableDelayedExpansion
  6. 2>nul md "characters"
  7. pushd "characters"
  8. >"t.tmp" type nul
  9. set "hex=0 1 2 3 4 5 6 7 8 9 A B C D E F"
  10. for %%A in (%hex%) do for %%B in (%hex%) do (
  11.   set /a "N=0x%%A%%B"
  12.   >nul makecab /d compress=off /d reserveperfoldersize=!N! /d reserveperdatablocksize=26 "t.tmp" "%%A%%B.chr"
  13.   type "%%A%%B.chr" | ((for /l %%N in (1 1 38) do pause)>nul&findstr "^">"temp.tmp")
  14.   >nul copy /y "temp.tmp" /a "%%A%%B.chr" /b
  15. )
  16. >nul copy /y nul + nul /a "1A.chr" /a
  17. del "t.tmp" "temp.tmp"
  18. popd
复制代码

回复 3# CrLf

初始稳定版

我在 Win 7 64 位上没有发现问题

TOP

回复 3# CrLf
回复 6# amwfjhh
我在 chcp 936 下没有得到正确结果
但我的命令行设置成了 默认 chcp 437, 这时没有发现错误.
在 chcp 437 下重新运行看看

TOP

返回列表