我正在运行一个长时间运行的批处理文件。现在我意识到需要在批处理文件末尾添加一些命令(不对现有内容进行修改,只是增加一些额外的命令)。鉴于大多数批处理文件是逐步读取并逐个执行的,这是否可以实现?或者系统是否会读取整个文件内容然后运行作业?
我正在运行一个长时间运行的批处理文件。现在我意识到需要在批处理文件末尾添加一些命令(不对现有内容进行修改,只是增加一些额外的命令)。鉴于大多数批处理文件是逐步读取并逐个执行的,这是否可以实现?或者系统是否会读取整个文件内容然后运行作业?
我刚试过了,在 Windows XP 上,尽管违反我的直觉,它也能够在末尾捕获到新的命令。
我创建了一个包含以下内容的批处理文件:
echo Hello
pause
echo world
我运行了文件,当它在暂停时,添加了echo Salute
保存后按回车键继续暂停,所有三个提示都被输出到控制台。
所以,就这样做吧!
命令解释器会记住批处理文件中字节偏移量的位置。只要在当前代码行执行结束时,修改批处理文件,就不会有问题。
如果你在此之前进行修改,它将开始出现奇怪的行为(如重复命令等)。
*.cmd
文件吗? - François Beaunejeb's example很有趣,但它非常依赖于添加或删除的文本长度。我认为当rein说“如果在此之前修改它,它将开始做奇怪的事情(重复命令等)”时,他指的就是这种反直觉的结果。
我修改了jeb的代码,以展示如何在执行批处理文件的开头自由修改长度不同的动态代码,只要适当的填充在位即可。每次迭代时,整个动态部分都被完全替换。每个动态行都是以不干扰其他代码的分号;
前缀开头的。这方便了FOR /F
通过隐式的EOL=;
选项剥离动态代码。
我不是寻找特定行号,而是寻找特定注释来定位动态代码开始的位置。这更容易维护。
我使用等号行来无害地填充代码,以允许扩展和收缩。可以使用以下字符的任何组合:逗号、分号、等号、空格、制表符和/或换行符。(当然,填充不能以分号开头。)括号内的等号允许代码扩展。括号后面的等号允许代码收缩。
注意,FOR /F
会剥离空行。使用FINDSTR在循环内部添加每行的行号前缀,然后在循环内部删除前缀可以克服这种限制。但是多余的代码会使速度变慢,所以除非代码依赖空行,否则没必要这样做。
@echo off
setlocal DisableDelayedExpansion
echo The starting filesize is %~z0
:loop
echo ----------------------
::*** Start of dynamic code ***
;set value=1
::*** End of dynamic code ***
echo The current value=%value%
::
::The 2 lines of equal signs amount to 164 bytes, including end of line chars.
::Putting the lines both within and after the parentheses allows for expansion
::or contraction by up to 164 bytes within the dynamic section of code.
(
call :changeBatch
==============================================================================
==============================================================================
)
================================================================================
================================================================================
set /p "quit=Enter Q to quit, anything else to continue: "
if /i "%quit%"=="Q" exit /b
goto :loop
:changeBatch
(
for /f "usebackq delims=" %%a in ("%~f0") do (
echo %%a
if "%%a"=="::*** Start of dynamic code ***" (
setlocal enableDelayedExpansion
set /a newValue=value+1, extra=!random!%%9
echo ;set value=!newValue!
for /l %%n in (1 1 !extra!) do echo ;echo extra line %%n
endlocal
)
)
) >"%~f0.tmp"
::
::The 2 lines of equal signs amount to 164 bytes, including end of line chars.
::Putting the lines both within and after the parentheses allows for expansion
::or contraction by up to 164 bytes within the dynamic section of code.
(
move /y "%~f0.tmp" "%~f0" > nul
==============================================================================
==============================================================================
)
================================================================================
================================================================================
echo The new filesize is %~z0
exit /b
上述方法可行,但如果将动态代码移至文件末尾的子程序中,则会更加简单。代码可以无限扩展和收缩,而无需进行填充。FINDSTR在删除动态部分方面比FOR /F快得多。动态行可以安全地以分号(包括标签)为前缀。然后,使用FINDSTR /V选项来排除以分号开头的行,新的动态代码只需简单地附加到末尾即可。
@echo off
setlocal DisableDelayedExpansion
echo The starting filesize is %~z0
:loop
echo ----------------------
call :changeBatch
call :dynamicCode1
call :dynamicCode2
echo The current value=%value%
set /p "quit=Enter Q to quit, anything else to continue: "
if /i "%quit%"=="Q" exit /b
goto :loop
:changeBatch
(
findstr /v "^;" "%~f0"
setlocal enableDelayedExpansion
set /a newValue=value+1, extra=!random!%%9
echo ;:dynamicCode1
echo ;set value=!newValue!
echo ;exit /b
echo ;
echo ;:dynamicCode2
for /l %%n in (1 1 !extra!) do echo ;echo extra line %%n
echo ;exit /b
endlocal
) >"%~f0.tmp"
move /y "%~f0.tmp" "%~f0" > nul
echo The new filesize is %~z0
exit /b
;:dynamicCode1
;set value=33
;exit /b
;
;:dynamicCode2
;echo extra line 1
;exit /b
set value=1000
这一行。@echo off
setlocal DisableDelayedExpansion
:loop
REM **** the next line will be changed
set value=1000
rem ***
echo ----------------------
echo The current value=%value%
<nul set /p ".=Press a key"
pause > nul
echo(
(
call :changeBatch
rem This should be here and it should be long
)
rem ** It is neccessary, that this is also here!
goto :loop
rem ...
:changeBatch
set /a n=0
set /a newValue=value+1
set /a toggle=value %% 2
set "theNewLine=set value=%newValue%"
if %toggle%==0 (
set "theNewLine=%theNewLine% & rem This adds 50 byte to the filesize.........."
)
del "%~f0.tmp" 2> nul
for /F "usebackq delims=" %%a in ("%~f0") DO (
set /a n+=1
set "line=%%a"
setlocal EnableDelayedExpansion
if !n!==5 (
(echo !theNewLine!)
) ELSE (
(echo !line!)
)
endlocal
) >> "%~f0.tmp"
(
rem the copy should be done in a parenthesis block
copy "%~f0.tmp" "%~f0" > nul
if Armageddon==TheEndOfDays (
echo This can't never be true, or is it?
)
)
echo The first line after the replace action....
echo The second line comes always after the first line?
echo The current filesize is now %~z0
goto :eof
:top
call :displayMenu
:prompt
REM The script will spend most of its time waiting here.
choice /C:1234 /N "Enter selection: "
if ERRORLEVEL == 4 goto DoOption4
if ERRORLEVEL == 3 goto DoOption3
if ERRORLEVEL == 2 goto DoOption2
goto DoOption1
:displayMenu
(many lines to display menu)
goto prompt
:DoOption1
(many lines to do Option 1)
goto top
:DoOption2
(many lines to do Option 2)
goto top
(etc)