Windows for循环不';t遍历文本文件的行
我有一个for循环,它应该打印文本文件的每一行。而是打印Windows for循环不';t遍历文本文件的行,windows,printing,batch-file,for-loop,command-prompt,Windows,Printing,Batch File,For Loop,Command Prompt,我有一个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" ...
日志路径
代码如下:
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!
)
)
有什么帮助吗?您没有告诉我们哪一行发出了“无效开关”错误消息,但我看到了几个潜在问题:
echo off
For %%G in (C:\ExecutionSDKTest_10.2.2\*.properties) DO (
FOR /F "tokens=*" %%i in (%%G) do @echo %%i
)
!变数代码>您需要启用延迟扩展
SetLocal EnableDelayedExpansion
'/'
,请更改为'\'
set logPath="C:\ExecutionSDKTest_10.2.2\Logs\!fileName!.log"
print
命令将文本文件发送到打印机。将其更改为echo
echo !logLine!
使用反斜杠而不是正斜杠
set "logPath=C:\ExecutionSDKTest_10.2.2\Logs\!fileName!.log"
虽然通常可以在Windows中互换使用它们,
cmd
是一种特殊情况,因为正斜杠用于内置命令的开关和选项。它的解析器经常会在前斜杠上出错。不过,您通常可以安全地将这些路径传递给外部命令。我只是添加了另一个猜测。不管怎样,从他们所写的内容中,没有人能真正找出问题所在;-)您是对的,Windows API可以接受反斜杠或斜杠,但惯例是使用反斜杠,而返回路径的microsoft API使用反斜杠。许多程序和库不接受斜杠。所以,我已经编辑了状态潜在问题的答案,因为我们不知道So的BAT文件中运行了哪些其他程序。问题是:这些斜杠是否会导致错误。我不这么认为,我也不这么认为。但我们不能确定,因为我们不知道这些线是什么。。。做在Windows上的文件名中使用斜杠是一个潜在的问题源。