I have a Windows batch file that processes a bunch of files. As part of this I use the following line:
我有一个处理一堆文件的Windows批处理文件。作为其中的一部分,我使用以下行:
forfiles /p "%~dpn1%LogDir%" /m "%SupportLog%*" /c "cmd /c logreader.py @file > \"%~dpn1%ParsedLogDir%\@file_Logreader.txt\"
This works OK, but essentially loops through all my files (%SupportLog%*
) and passes each one by one into the logreader.py script.
这工作正常,但基本上循环遍历我的所有文件(%SupportLog%*)并将每个文件逐个传递到logreader.py脚本中。
What I really want to do is to create a list or parameter of all these files and pass all of them at once into the Python script, such the command that should be run would resemble:
我真正想要做的是创建所有这些文件的列表或参数,并将所有这些文件一次性传递到Python脚本中,这样应该运行的命令将类似于:
logreader.py "logfile.log" "logfile.log.1" "logfile.log.3" .....
I tried to use the set
command within the forfiles
command like that:
我尝试在forfiles命令中使用set命令,如下所示:
forfiles /p "%~dpn1%LogDir%" /m "%SupportLog%*" /c "cmd /c set PARAMS=%PARAMS%@file "
However, when run this and leave the ECHO ON
, I see:
但是,当运行此并保持ECHO开启时,我看到:
forfiles /p "C:\Path\log" /m "logfile.log*" /c "cmd /c set PARAMS=@file "
This is incorrect. And when I do echo %PARAMS%
, I get no result.
这是不正确的。当我回复%PARAMS%时,我没有得到任何结果。
Is there a way of achieving this?
有没有办法实现这个目标?
3 个解决方案
#1
0
forfiles is complicated or may even be buggy. It does not support the quotes within /c "cmd /c ...."
must be replaced by hex characters 0x22
, or by \"
.
forfiles很复杂,甚至可能是错误的。它不支持/ c“cmd / c ....中的引号。必须用十六进制字符0x22或\”替换。
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('dir /b /A-D "logfile.log.?"') do (
set _logfile=!_logfile! "%%a"
)
echo logreader.py %_logfile%
output:
logreader.py "logfile.log.1" "logfile.log.2" "logfile.log.3" "logfile.log.4" "logfile.log.5"
#2
0
forfiles
creates a new cmd
instance per each loop iteration, so a variable PARAMS
only exists as long as the containing instance.
forfiles为每个循环迭代创建一个新的cmd实例,因此变量PARAMS仅在包含实例时才存在。
If you want to use forfiles
and work around that issue, you might do the following:
如果要使用forfiles并解决该问题,可以执行以下操作:
rem collect all items in a temporary file as a single line:
del /F /Q "loglist.tmp" 2> nul
forfiles /P "%~dpn1%LogDir%" /M "%SupportLog%*" /C "cmd /C echo | set /P _=\"@file \" >> \"loglist.tmp\""
rem pass over the content of temporary file as parameters:
for /F "usebackq eol=| delims=" %%L in ("loglist.tmp") do logreader.py %%L
rem clean up temporary file:
del /F /Q "loglist.tmp" 2> nul
Relying on a variable for collecting the parameters rather than a temporary file, the following could work:
依靠变量来收集参数而不是临时文件,以下方法可以起作用:
rem collect all items in a variable:
set "PARAMS="
setlocal EnableDelayedExpansion
for /F "delims=" %%L in (
'forfiles /P "%~dpn1%LogDir%" /M "%SupportLog%*" /C "cmd /C echo.@file"'
) do set "PARAMS=!PARAMS!%%L "
endlocal & set "PARAMS=%PARAMS%"
rem pass over the content of variable as parameters:
if defined PARAMS logreader.py %PARAMS%
#3
-1
Thanks all. I eventually used a combination of the pointers listed above (and elsewhere) to come up with the following (an excerpt of the full batch file):
谢谢大家。我最终使用了上面列出的指针(和其他地方)的组合来得到以下内容(完整批处理文件的摘录):
setlocal EnableDelayedExpansion
Set SupportLog=support.log
Set LogDir=\var\log
set PARAMS=
for %%A in ("%~dpn1%LogDir%\%SupportLog%*") do (
set PARAMS=!PARAMS! "%%A"
)
logreader.py %PARAMS% > "%~dpn1%ParsedLogDir%\%SupportLog%_Logreader.txt"
So, the a shortcut to the batch file is created in the "SendTo" folder, I can then right click the zipped log file and "SendTo" the batch, and all works as expected.
因此,批处理文件的快捷方式是在“SendTo”文件夹中创建的,然后我可以右键单击压缩的日志文件和“SendTo”批处理,所有工作都按预期工作。
Perhaps I should switch to PowerShell for things like this going forward.
也许我应该切换到PowerShell来做这样的事情。
#1
0
forfiles is complicated or may even be buggy. It does not support the quotes within /c "cmd /c ...."
must be replaced by hex characters 0x22
, or by \"
.
forfiles很复杂,甚至可能是错误的。它不支持/ c“cmd / c ....中的引号。必须用十六进制字符0x22或\”替换。
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('dir /b /A-D "logfile.log.?"') do (
set _logfile=!_logfile! "%%a"
)
echo logreader.py %_logfile%
output:
logreader.py "logfile.log.1" "logfile.log.2" "logfile.log.3" "logfile.log.4" "logfile.log.5"
#2
0
forfiles
creates a new cmd
instance per each loop iteration, so a variable PARAMS
only exists as long as the containing instance.
forfiles为每个循环迭代创建一个新的cmd实例,因此变量PARAMS仅在包含实例时才存在。
If you want to use forfiles
and work around that issue, you might do the following:
如果要使用forfiles并解决该问题,可以执行以下操作:
rem collect all items in a temporary file as a single line:
del /F /Q "loglist.tmp" 2> nul
forfiles /P "%~dpn1%LogDir%" /M "%SupportLog%*" /C "cmd /C echo | set /P _=\"@file \" >> \"loglist.tmp\""
rem pass over the content of temporary file as parameters:
for /F "usebackq eol=| delims=" %%L in ("loglist.tmp") do logreader.py %%L
rem clean up temporary file:
del /F /Q "loglist.tmp" 2> nul
Relying on a variable for collecting the parameters rather than a temporary file, the following could work:
依靠变量来收集参数而不是临时文件,以下方法可以起作用:
rem collect all items in a variable:
set "PARAMS="
setlocal EnableDelayedExpansion
for /F "delims=" %%L in (
'forfiles /P "%~dpn1%LogDir%" /M "%SupportLog%*" /C "cmd /C echo.@file"'
) do set "PARAMS=!PARAMS!%%L "
endlocal & set "PARAMS=%PARAMS%"
rem pass over the content of variable as parameters:
if defined PARAMS logreader.py %PARAMS%
#3
-1
Thanks all. I eventually used a combination of the pointers listed above (and elsewhere) to come up with the following (an excerpt of the full batch file):
谢谢大家。我最终使用了上面列出的指针(和其他地方)的组合来得到以下内容(完整批处理文件的摘录):
setlocal EnableDelayedExpansion
Set SupportLog=support.log
Set LogDir=\var\log
set PARAMS=
for %%A in ("%~dpn1%LogDir%\%SupportLog%*") do (
set PARAMS=!PARAMS! "%%A"
)
logreader.py %PARAMS% > "%~dpn1%ParsedLogDir%\%SupportLog%_Logreader.txt"
So, the a shortcut to the batch file is created in the "SendTo" folder, I can then right click the zipped log file and "SendTo" the batch, and all works as expected.
因此,批处理文件的快捷方式是在“SendTo”文件夹中创建的,然后我可以右键单击压缩的日志文件和“SendTo”批处理,所有工作都按预期工作。
Perhaps I should switch to PowerShell for things like this going forward.
也许我应该切换到PowerShell来做这样的事情。