【问题标题】:Copy conditionally in batch file在批处理文件中有条件地复制
【发布时间】:2014-12-05 17:08:17
【问题描述】:

使用批处理文件我使用 McAfee 扫描我的 PC 中的病毒。在扫描结束时,它会在本地写入名为“OnDemandScanLog.txt”的文件。在我关闭系统之前的最后一步,我必须将此文件从本地目录复制到共享文件夹。 问题是有时它不会复制它。万一它失败了,我总是可以使用“复制”命令手动复制它,但我需要在扫描结束时完成它。 我假设我可以有条件地复制它......并检查 ERRORLEVEL 直到它被确定复制。然后它应该关闭PC。 有人可以帮我插入条件语句以确保它被复制。 我将附上我的批处理文件:

@echo off

REM Perform a Full scan and log result


if exist "%ProgramFiles(x86)%" (
    set "PATH_=%ProgramFiles(x86)%\McAfee\VirusScan Enterprise"
    set SHUTDOWN=shutdown /s /f
) else (
    set "PATH_=%ProgramFiles%\McAfee\VirusScan Enterprise"
    set SHUTDOWN=shutdown -s -f
)

set LOGDIR=C:\McAfee
set VADIR=\\servername\McAfee Logs\Log1\


"%PATH_%\scan32.exe" /Task {ED73BEB7-1E8F-45AC-ABBC-A749AF6E2710}  %* /ANALYZE /MANY /ALL /CLEAN /DAM /NC /NOEXPIRE /PLAD /PROGRAM /SUB /STREAMS /UNZIP /THREADS=4 /TIMEOUT=15 /APPEND  /AUTOEXIT 

copy %LOGDIR%\OnDemandScanLog.txt   /Y "%VADIR%"


start %SHUTDOWN%

【问题讨论】:

  • 不,我没有使用“start”命令来启动 McAfee。我使用 %PATH_%\scan32.exe 并且它始终有效。唯一的问题是复制日志文件并不总是成功
  • This answer 可能会有所帮助,建议使用 xcopy 而不是 copy。顺便说一句,您应该使用%SHUTDOWN%start "" %SHUTDOWN%,因为"title"obligatory for start 命令
  • 感谢您使用 xcopy 而不是复制 %SHUTDOWN% 而不是启动 %SHUTDOWN% 的建议。实际上我的意思不是“抓住”复制的错误,而是强迫它复制。我的意思是我想在循环中执行 copy 或 xcopy 命令,直到 %errorlevel% 为 0。我注意到它通过/失败不一致,因此尝试几次直到响应为零,我认为应该可以完成这项工作。需要类似'while'循环并检查 %errorlevel% 的条件

标签: batch-file copy


【解决方案1】:

由于 XCopy 自 Vista 和 Windows 2008 以来已被弃用,请使用 robocopy。它具有内置的默认重试...并且来自Robocopy 的退出代码是well documented

:: syntax ::
:: robocopy <Source> <Destination> [<File>[ ...]] [<Options>]
::
set "LOGDIR=C:\McAfee"
set "VADIR=\\servername\McAfee Logs\Log1"

"%PATH_%\scan32.exe" /Task ... /AUTOEXIT 

robocopy "%LOGDIR%" "%VADIR%" "OnDemandScanLog.txt"

嵌入的robocopy重试功能可以通过下一个代码sn-p进行测试:

@echo off
@SETLOCAL enableextensions enabledelayedexpansion
@echo :: using temporary directory %temp%
@echo :: define and make destination folder
set "destfldr=%temp%\dstfldr"
md "%destfldr%" 2>Nul
@echo :: create destination file first to be older
echo text test >"%destfldr%\mytest.txt"
@echo :: lock destination file
start "" notepad >>"%destfldr%\mytest.txt"
@echo :: waiting for 5 second to assure file is locked
ping 1.1.1.1 -n 1 -w 5000 > nul
set "sourcef=%temp%"
@echo :: create source file to be newer 
echo test text %date% %time% >"%sourcef%\mytest.txt"
@echo :: classic copy file should fail
copy /Y "%sourcef%\mytest.txt" "%destfldr%\."
@echo :: robocopy file, 5 retries with seconds delay
robocopy "%sourcef%\." "%destfldr%\." "mytest.txt" /r:5 /w:3
@echo ::
@ENDLOCAL
::

请不要忘记关闭notepad :)

【讨论】:

    【解决方案2】:

    好吧,如果复制日志文件失败

    1. copy 命令已在 McAfee 扫描完成之前执行,因此在 McAfee 扫描从日志文件中删除独占文件访问锁定之前,或
    2. 另一台计算机上的目标目录尚无法访问。

    第一个原因我可能会帮助使用

    start "Scan for malware" /wait "%PATH_%\scan32.exe" /Task {ED73BEB7-1E8F-45AC-ABBC-A749AF6E2710} %* /ANALYZE /MANY /ALL /CLEAN /DAM /NC /NOEXPIRE /PLAD /PROGRAM /SUB /STREAMS /UNZIP /THREADS=4 /TIMEOUT=15 /APPEND /AUTOEXIT
    

    出于第二个原因,下面的注释代码可能很有用。

    rem Count the retries to avoid an endless loop if server is not reachable ever.
    set "RetryCount=0"
    
    :RetryLoop
    
    rem Copy with verification on destination directory and resume if
    rem connection to destination server is lost during copy process.
    copy /V /Y /Z %LOGDIR%\OnDemandScanLog.txt "%VADIR%"
    
    rem Was there no error on copying the file?
    if not errorlevel 1 goto TurnOff
    
    rem There was an error. Therefore increase counter and shutdown
    rem nevertheless if copying the log file failed already 30 times.
    set /A "RetryCount+=1"
    if "%RetryCount%"=="30" goto TurnOff
    
    rem Set a default wait time of 5 seconds before next try.
    set "MilliSeconds=5000"
    
    rem Ping server to test if server connection is established at all. Increase
    rem the wait time to 60 seconds before next try if server is not connected.
    %SystemRoot%\System32\ping.exe -n 1 servername >nul
    if errorlevel 1 set "MilliSeconds=60000"
    
    rem Wait 5 or 60 seconds before next try.
    %SystemRoot%\System32\ping 1.1.1.1 -n 1 -w %MilliSeconds% >nul
    goto RetryLoop
    
    :TurnOff
    start %SHUTDOWN%
    

    请参阅How to sleep for 5 seconds in Windows' Command Prompt? 了解如何使用 ping 等待特定时间。

    【讨论】:

    • 嗯...... robocopy 也没有让我的程序可靠地运行。我认为问题不在于“复制”,而在于我的程序设计。我打算改变设计并避免复制。票可以关闭
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多