第二段生成代码。测试的文件如下:- ::=====================================================
- :: code by freeants CMD@XP version:1.00
- ::=====================================================
- @echo off
- cd/d "%~dp0">nul
- if "%~z1" equ "" goto:usage
- if %~z1 gtr 65280 goto:NOT_SUPPORT
- setlocal ENABLEDELAYEDEXPANSION
- copy/b "%~1" $$$>nul
- set/a D_NUM=%~z1+255
- call:DEC_HEX %D_NUM% >nul
- echo @echo off>"%~n1.cmd"
- echo more +5 "%%~0"^|debug^&graftabl 936^>nul>>"%~n1.cmd"
- echo copy/b/y $tmp$ "%~nx1"^>nul>>"%~n1.cmd"
- echo del/a/f/q $tmp$>>"%~n1.cmd"
- echo goto:eof>>"%~n1.cmd"
- for /f "eol=- skip=1 tokens=*" %%i in ('^(echo d100,%FILE_SIZE%^&echo q^)^|debug $$$^&^&del $$$') do (
- set op=%%i
- set op=!op:-= !
- echo e!op:~5,53! 2>nul>>"%~n1.cmd"
- )
- echo rcx>>"%~n1.cmd"
- call:DEC_HEX %~z1 >nul
- echo %FILE_SIZE%>>"%~n1.cmd"
- echo n$tmp$>>"%~n1.cmd"
- echo w>>"%~n1.cmd"
- echo q>>"%~n1.cmd"
- goto :EOF
-
- :DEC_HEX
- set FILE_SIZE=
- set PATTERN=123456789ABCDEF
- set dec=%1
- :WHILE
- set /a res=%dec%%%16-1
- set /a dec/=16
- set res=!PATTERN:~%res%,1!
- set FILE_SIZE=%res%%FILE_SIZE%
- if "%dec%" neq "0" goto WHILE
- goto:eof
-
- :usage
- echo.================================================
- echo 你没有指定要处理的文件或指定的文件不存在……
- echo.
- echo 用法:
- echo %~n0 fileName
- echo.================================================
- goto:eof
-
- :NOT_SUPPORT
- echo 指定的文件过大,脚本(%~nx0)不能处理……
- pause
- goto:eof
复制代码 转换后的文件如下;- (
- echo ::=====================================================
- echo :: code by freeants CMD@XP version:1.00
- echo ::=====================================================
- echo @echo off
- echo cd/d "%~dp0">nul
- echo if "%~z1" equ "" goto:usage
- echo if %~z1 gtr 65280 goto:NOT_SUPPORT
- echo setlocal ENABLEDELAYEDEXPANSION
- echo copy/b "%~1" $$$>nul
- echo set/a D_NUM=%~z1+255
- echo call:DEC_HEX %D_NUM% >nul
- echo echo @echo off>"%~n1.cmd"
- echo echo more +5 "%%~0"^|debug^&graftabl 936^>nul>>"%~n1.cmd"
- echo echo copy/b/y $tmp$ "%~nx1"^>nul>>"%~n1.cmd"
- echo echo del/a/f/q $tmp$>>"%~n1.cmd"
- echo echo goto:eof>>"%~n1.cmd"
- echo for /f "eol=- skip=1 tokens=*" %%i in ('^(echo d100,%FILE_SIZE%^&echo q^)^|debug $$$^&^&del $$$') do (
- echo set op=%%i
- echo set op=-=
- echo echo e~5,53 2>nul>>"%~n1.cmd"
- echo )
- echo echo rcx>>"%~n1.cmd"
- echo call:DEC_HEX %~z1 >nul
- echo echo %FILE_SIZE%>>"%~n1.cmd"
- echo echo n$tmp$>>"%~n1.cmd"
- echo echo w>>"%~n1.cmd"
- echo echo q>>"%~n1.cmd"
- echo goto :EOF
- echo :DEC_HEX
- echo set FILE_SIZE=
- echo set PATTERN=123456789ABCDEF
- echo set dec=%1
- echo :WHILE
- echo set /a res=%dec%%%16-1
- echo set /a dec/=16
- echo set res=~%res%,1
- echo set FILE_SIZE=%res%%FILE_SIZE%
- echo if "%dec%" neq "0" goto WHILE
- echo goto:eof
- echo :usage
- echo echo.================================================
- echo echo 你没有指定要处理的文件或指定的文件不存在……
- echo echo.
- echo echo 用法:
- echo echo %~n0 fileName
- echo echo.================================================
- echo goto:eof
- echo :NOT_SUPPORT
- echo echo 指定的文件过大,脚本(%~nx0)不能处理……
- echo pause
- echo goto:eof
- )>a.bat
复制代码 把这个2.txt改名成2.bat后运行,无法生成a.bat。
还有个小问题是原代码中的空行没有了,不过这个不是太要紧,如果可以保留更好,不好处理这样也行。 |