首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在批处理文件中有条件复制

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

Stack Overflow用户
提问于 2014-12-05 17:08:17
回答 2查看 2.1K关注 0票数 1

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

代码语言:javascript
复制
@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%
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-12-06 22:41:38

自从Vista和Windows2008以来,XCopy就被废弃了,所以请使用robocopy。它有内置的默认重试.Robocopy的出口代码是有据可查

代码语言:javascript
复制
:: 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重试功能:

代码语言:javascript
复制
@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 :)

票数 0
EN

Stack Overflow用户

发布于 2014-12-06 22:54:49

如果以下情况下复制日志文件失败

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

第一个原因是我可能会用

代码语言:javascript
复制
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

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

代码语言:javascript
复制
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%

有关ping的使用情况,请参阅如何在Windows命令提示符下睡眠5秒?以等待一定时间。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27321132

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档