2012-07-06 48 views
0

的线我有一个for循环迭代。相反,它将打印logPath
这是代码:for循环不通过是应该打印文本文件的每一行的文本文件

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

任何帮助吗?

回答

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

你不告诉我们这行发出“无效开关”错误消息,但我看到一些潜在的问题:

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

    SetLocal EnableDelayedExpansion 
    
  2. 不使用文件名'/',更改为'\'

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

    echo !logLine! 
    
+0

Windows接受斜杠_和_反斜杠。 – 2012-07-07 10:56:23

+0

你说得对,Windows API可以接受反斜杠或反斜杠,但约定是使用反斜杠,并且返回路径的Microsoft API使用反斜杠。许多程序和库不接受斜杠。所以,我编辑的答案状态*潜在问题*,因为我们不知道还有什么其他程序中SO的BAT文件运行。 – 2012-07-07 17:45:41

+0

问题是:这些斜杠是否会导致错误。我不这么认为。 – 2012-07-07 18:59:29

0

使用反斜杠而不是正斜杠。

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

虽然通常你可以在Windows交替使用它们,cmd是正斜杠用于开关和选项的内置命令的特殊情况。它的解析器经常在斜杠上绊倒。不过,您通常可以安全地将这些路径传递给外部命令。

+0

我刚刚添加了另一个猜测。从他们写的东西中,无论如何,没有人能够真正弄清楚什么是错的;-) – Joey 2012-07-07 15:14:36

相关问题