回应一个空白行

时间:2013-01-07 17:47:51

标签: file batch-file cmd

我遇到以下代码遇到空行时回显文件的空行时遇到问题。我应该怎么做呢?

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

1 个答案:

答案 0 :(得分: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
)