当遇到空行时,下面的代码将空行回显到文件中有问题。我该怎么做呢?
for /f "tokens=1,* delims=]" %%A in ('"type Setup.ini|find /n /v """') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:PRINTER_JAM_SENSOR_PRESENT: TRUE=PRINTER_JAM_SENSOR_PRESENT: FALSE%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X >>Setup.ini.replaced%~x1
)
)
当遇到空行时,line
变量将被设置为空白,从而删除该变量。您已经检查了它何时被定义。
for /f "tokens=1,* delims=]" %%A in ('"type Setup.ini|find /n /v """') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:PRINTER_JAM_SENSOR_PRESENT: TRUE=PRINTER_JAM_SENSOR_PRESENT: FALSE%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X >>Setup.ini.replaced%~x1
)
if not defined line echo.>>Setup.ini.replaced%~x1
)
额外:这里是代码,但我建议使用setlocal EnableDelayedExpansion
编辑。
for /F "tokens=1,* delims=]" %%A in ('type Setup.ini ^| find /n /v ""') do (
set "line=%%B"
if defined line (
set "line=!line:PRINTER_JAM_SENSOR_PRESENT: TRUE=PRINTER_JAM_SENSOR_PRESENT: FALSE!"
echo(!line!>>Setup.ini.replaced%~x1
)
if not defined line echo.>>Setup.ini.replaced%~x1
)