【发布时间】:2023-04-07 03:02:01
【问题描述】:
我有一个批处理脚本,它进入文件夹的子文件夹(不包括一个文件夹)获取 .log 文件,将它们归档,然后删除原始文件。
文件夹结构:
\Logs\logs1
\Logs\logs2
\Logs\logs3
最初,我只将这个循环作为一行,它的工作原理如下:
FOR /F "usebackq tokens=* delims=" %%A IN (`DIR "%LogsFilespec%" /B /S ^|find ^"logs3^" /v `) DO %zipCommand% >> %ProcessLog% & DEL "%%~fA" & ECHO Deleting "%%~fA" >> %ProcessLog% & ECHO. >> %ProcessLog%
但是,我被指示将它变成一个看起来更简单的 FOR DO CALL,结果我被卡住了,不确定出了什么问题。 %targetDate% 是 YYYYMMDD。
**根据 rojo 的建议编辑了更完整的代码:
SET ProcessLog=C:\Users\Me\Documents\batch_files\%~n0_%jobLog%.txt
:DetermineArchiveApp
:: Create specifics for ITD Logs collection
SET ITDLogsLocation=C:\Users\Me\Documents\fakeG
SET ITDLogsName=trace*%targetDate%.log
SET ITDLogsFilespec=%ITDLogsLocation%\%ITDLogsName%
:: ********************************************************************************
:: * Check if server has WinZip or 7-Zip installed. setup environment variable *
:: * accordingly with executable path and name, and with any required parameters. *
:: ********************************************************************************
SET PathWinZip="C:\Program Files\WinZip\wzzip.exe"
SET Path7Zip_64bit="C:\Program Files\7-Zip\7z.exe"
SET Path7Zip_32bit="C:\Program Files (x86)\7-Zip\7z.exe"
SET Path7Zip_true="C:\Users\Me\Documents\batch_files\7za.exe"
:: Check for 32-bit version of 7-Zip. If found, configure
:: its command line parameter to produce a .zip file
IF EXIST %Path7Zip_32bit% SET zipCommand=%Path7Zip_32bit% a
::Check for WinZip
IF EXIST %PathWinZip% SET zipCommand=%PathWinZip% -a
:: Check for 64-bit version of 7-Zip. If found, configure
:: its command line parameter to produce a .zip file
IF EXIST %Path7Zip_64bit% SET zipCommand=%Path7Zip_64bit% a
:: Check for 64-bit version of 7-Zip. If found, configure
:: its command line parameter to produce a .zip file
IF EXIST %Path7Zip_true% SET zipCommand=%Path7Zip_true% a
:ArchiveProcess
ECHO ***************************************************** >> %ProcessLog%
::Write the date and time of job starting to a log
ECHO %DATE% %TIME% START >> %ProcessLog%
::Loop through the list that DIR gives for the given folder, and for each folder do zipCommand, excluding ITD\Data\AFS, and write to log
FOR /F "delims=" %%A IN (
'DIR "%LogsFilespec%" /B /S ^|find /v "logs3"'
) DO CALL :DoZip "%%~dpnA" "%%~fA" "%zipCommand%" "%ProcessLog%"
::Write end to log
ECHO %DATE% %TIME% %~nx0 END >> %ProcessLog%
ECHO ***************************************************** >> %ProcessLog%
:DoZip
setlocal
SET "archiveName=%~1"
SET "SourceFileSpec=%~2"
SET "zipCommand=%~3"
SET "RunLog=%~4"
>>"%RunLog%" (
echo Archiving %SourceFileSpec%...
"%zipCommand%" "%archiveName%.zip" "%SourceFileSpec%"
echo %TIME% Deleting %SourceFileSpec%...
DEL "%SourceFileSpec%"
echo;
)
GOTO :EOF
我从 cmd NOW 得到的错误是:
'""' 不是内部或外部命令、可运行程序或批处理文件。
两者的语法有什么问题!?
编辑 2:现在回家,还不能远程工作,所以我明天会回来。 当前的问题是:7zip命令行不喜欢
"C:\Users\Me\Documents\batch_files\7za.exe" a
而且 >>"%RunLog%" 块也不起作用,如果只注释掉存档和删除行以查看发生了什么,我就会被抛出
The system cannot find the drive specified.
The system cannot find the drive specified.
The system cannot find the drive specified.
The system cannot find the drive specified.
The system cannot find the path specified.
【问题讨论】:
-
请edit您的问题并包含足够的代码以允许其他人重现该问题。如需帮助,请阅读How to create a Minimal, Complete, and Verifiable example。
-
好的,我想我现在已经做得很好了。
标签: batch-file for-loop logging 7zip