1.

Solve : exit batch if copy fails?

Answer»

Hi, can anyone help me modify this batch file to exit if it fails to complete the 'copy' job

the main function of the batch file is to copy a test file from a source to a destination
and count the time it takes to complete the copy

sometimes the destination computer is turned off so the comand WINDOW displays '0 files copied' in stead of '1 file copied'

i need to add an 'if' satement so that the batch will jump to :end if the file fails to copy

@echo off

FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Second /FORMAT:table ^| findstr /r "."') DO (
set Milisecond=%time:~9,2%
set Day=%%A
set Hour=%%B
set Minute=%%C
set Second=%%D
)
set /a Start=%Day%*8640000+%Hour%*360000+%Minute%*6000+%Second%*100+%Milisecond%

copy c:\temp\1meg.test \\COMPUTERX\c$\temp
::::::: i need to jump from this point to the :end if this copy job fails to complete ::::::::

echo COMPUTER: ADL017 >> c:\temp\BENCHMARK\TIMER.log
echo DATE: %DATE% >> c:\temp\BENCHMARK\TIMER.log
echo tIME: %TIME% >> c:\temp\BENCHMARK\TIMER.log

FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Second /Format:table ^| findstr /r "."') DO (
set Day=%%A
set Hour=%%B
set Minute=%%C
set Second=%%D
)
set Milisecond=%time:~9,2%
set /a End=%Day%*8640000+%Hour%*360000+%Minute%*6000+%Second%*100+%Milisecond%
set /a Diff=%End%-%Start%
set /a DiffMS=%Diff%%%100
set /a Diff=(%Diff%-%DiffMS%)/100
set /a DiffSec=%Diff%%%60
set /a Diff=(%Diff%-%Diff%%%60)/60
set /a DiffMin=%Diff%%%60
set /a Diff=(%Diff%-%Diff%%%60)/60
set /a DiffHrs=%Diff%

:: format with leading zeroes
if %DiffMS% LSS 10 set DiffMS=0%DiffMS!%
if %DiffSec% LSS 10 set DiffMS=0%DiffSec%
if %DiffMin% LSS 10 set DiffMS=0%DiffMin%
if %DiffHrs% LSS 10 set DiffMS=0%DiffHrs%

echo COUNTER: %DiffHrs%:%DiffMin%:%DiffSec%.%DiffMS% >> c:\temp\BENCHMARK\TIMER.log

echo **************************************** >> c:\temp\BENCHMARK\Timer.LOG

:end Code: [Select]copy c:\temp\1meg.test \\COMPUTERX\c$\temp || goto endThe way I understand it is that you what the bat file to copy a file and log how long it takes. And for the bat file to end if it can't FIND it.
I don't know why you have all the set statements. Here's ONE that worked for me.
@echo off
echo Starting on %date% at %time%>>c:\users\bonz\bat\TIMER.log
if not exist \\COMPUTERX\c$\temp\*.* goto NOTTHERE

copy c:\temp\1meg.test \\COMPUTERX\c$\temp
echo 1meg.test has been copied to \\COMPUTERX\c$\temp >>c:\users\bonz\bat\TIMER.log
echo Done copying at %time%>>c:\users\bonz\bat\TIMER.log
goto end

:NOTTHERE
echo Couldn't find \\COMPUTERX\c$\temp>>c:\users\bonz\bat\TIMER.log
echo ending at %time%>>c:\users\bonz\bat\TIMER.log

:end
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~>>c:\users\bonz\bat\TIMER.log

This is what printed to the TIMER.log file.

Starting on Sat 04/02/2011 at 23:28:39.74
Couldn't find \\COMPUTERX\c$\temp
ending at 23:29:00.95
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
As you can see, it also gave me the message that it couldn't find the destination as well as the starting date & time and ending time. I noticed it took some time trying to find \\COMPUTERX\c$\temp. I counted off 21 seconds and that's what it shows in the TIMER.log file. (actually 21.21 seconds)



Discussion

No Comment Found