在批处理文件中有条件地复制

时间:2014-12-05 17:08:17

标签: batch-file copy

使用批处理文件我使用McAfee扫描我的PC以查找病毒。在扫描结束时,它会在本地写入名为“OnDemandScanLog.txt”的文件。关闭系统之前的最后一步我必须将此文件从本地目录复制到共享文件夹。 问题是有时它不会复制它。如果它失败了我总是可以使用'copy'命令手动复制它,但我需要在扫描结束时完成它。 我假设我可以条件地复制它...并检查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%

2 个答案:

答案 0 :(得分:0)

由于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重试功能可以通过下一个代码段进行测试:

@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,请:)

答案 1 :(得分:0)

好吧,如果

,复制日志文件就会失败
  1. 复制命令已在McAfee扫描完成之前执行,因此在McAfee扫描从日志文件中删除了独占文件访问锁定之前,或
  2. 另一台计算机上的目标目录尚不可访问。
  3. 第一个原因我可能会帮助使用

    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用法。