1

Using batch file I scan my PC for viruses using McAfee. At the end of the scan it writes locally file called 'OnDemandScanLog.txt'. The last step before I shut down the system I have to copy this file from local directory to shared folder. The problem is that sometimes it does not copy it. In case when it failed I always could copy it manually using 'copy' command but I need it to be done at the end of the scan. I assume that I could copy it condititionally ... and check for ERRORLEVEL until it get copied for sure. Then it should shut down PC. Could someone please help me to insert conditional statement in order to make sure it was copied. I will attach my batch file:

@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%
susja
  • 311
  • 3
  • 12
  • 33
  • no, I did not use 'start' command for starting McAfee. I use %PATH_%\scan32.exe and it always works. The only problem is with copy log file which not always is successful – susja Dec 05 '14 at 23:32
  • [This answer](http://stackoverflow.com/questions/8219040/windows-copy-command-return-codes/8219166#8219166) might be helpful, advising `xcopy` instead of `copy`. By the way, you should use either `%SHUTDOWN%` or `start "" %SHUTDOWN%`, as the `"title"` is [obligatory](http://ss64.com/nt/start.html) for `start` command – JosefZ Dec 06 '14 at 09:11
  • Thanks for advice using xcopy instead of copy %SHUTDOWN% instead of start %SHUTDOWN%. Actually my point was not to 'catch' the error of copying but instead force it to copy. I mean that I want perform copy or xcopy command in a loop until %errorlevel% will be 0. I noticed that it pass/fail inconsistently hence trying a few times until response is zero should do the job in my opinion. Need something like 'while' loop with checking condition for %errorlevel% – susja Dec 06 '14 at 13:35

2 Answers2

0

As XCopy has been deprecated since Vista and Windows 2008, use robocopy. It has built-in default retry... And exit codes from Robocopy are 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"

Embedded robocopy retry features could be tested by next code snippet:

@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
::

Don't forget close that notepad, please :)

JosefZ
  • 28,460
  • 5
  • 44
  • 83
0

Well, copying the log file fails if

  1. the copy command was already executed before McAfee scan finished and therefore before exclusive file access lock was removed by McAfee scan from log file, or
  2. the destination directory on the other computer is not yet accessible.

For the first reason I might help to use

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

For the second reason something like the commented code below could be useful.

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%

See How to sleep for 5 seconds in Windows' Command Prompt? for usage of ping to wait a certain time.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143
  • Well ... robocopy also didn't make my program to run reliably. I suggest the problem is not with 'copy' but with the design of my program. I'm planning to change design and avoid copy. Ticket could be closed – susja Dec 13 '14 at 16:02