1

当以下代码遇到空行时,我无法将空行回显到文件中。我该怎么办?

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
    )
)
4

1 回答 1

2

当遇到空行时,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
)
于 2013-01-07T18:05:18.473 回答