for循环不会遍历文本文件的行

时间:2012-07-06 20:29:58

标签: windows printing batch-file for-loop command-prompt

我有一个for循环,应该打印文本文件的每一行。而是打印logPath 这是代码:

  set enabledelayedexpansion

  for %%G in (C:\ExecutionSDKTest_10.2.2\*.properties) DO (
      Set fileName=%%~nxG
       ...
     set logPath="C:/ExecutionSDKTest_10.2.2/Logs/!fileName!.log"
       ...         
        For /f "tokens=*" %%B in (!logPath!) Do (
      echo Inside the for loop for printing each line!!
      set logLine=%%B
      print !logLine!  REM this prints the logPath instead of each logLine and jumps out of this for loop after the 1st iteration!
     )
 )

任何帮助?

3 个答案:

答案 0 :(得分:0)

echo off
For %%G in (C:\ExecutionSDKTest_10.2.2\*.properties) DO (      
      FOR /F "tokens=*" %%i in (%%G) do @echo %%i 
 )

答案 1 :(得分:0)

您没有告诉我们哪一行发出了“无效切换”错误消息,但我发现了几个潜在的问题:

  1. 使用!variables!您需要启用延迟展开

    SetLocal EnableDelayedExpansion
    
  2. 请勿在文件名中使用'/',请更改为'\'

    set logPath="C:\ExecutionSDKTest_10.2.2\Logs\!fileName!.log"
    
  3. print命令将文本文件发送到打印机。将其更改为echo

    echo !logLine!
    

答案 2 :(得分:0)

使用反斜杠代替正斜杠。

set "logPath=C:\ExecutionSDKTest_10.2.2\Logs\!fileName!.log"

虽然通常可以在Windows中互换使用它们,但cmd是一种特殊情况,因为正斜杠用于内置命令的开关和选项。它的解析器经常会遇到正斜杠。但是,您通常可以安全地将此类路径传递给外部命令。