how to exit a batch if it fails to complete

March 30, 2011 at 16:54:26
Specs: Windows XP

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


See More: how to exit a batch if it fails to complete

Report •


#1
March 30, 2011 at 17:21:42
2>nul copy c:\temp\1meg.test \\COMPUTERX\c$\temp | >nul find /i "0 file" && goto :end


Report •

#2
March 30, 2011 at 18:37:23
thanks for your help
i added this to my batch
2>nul copy c:\temp\1meg.test \\COMPUTERX\c$\temp | >nul find /i "0 file" && goto :end
now im getting the following error

find: '/i' : No such file or directory
find: '0 file' : No such file or directory


Report •

#3
March 30, 2011 at 18:54:50
Have you got a port of *nix find installed? Try this:

2>nul copy c:\temp\1meg.test \\COMPUTERX\c$\temp | >nul "%systemroot%\system32\find.exe" /i "0 file" && goto :end


Report •

Related Solutions


Ask Question