etech0
asked on
batch file copy catch errors
I have a batch file that copies a certain file. I tested it a few times, and once, it gave an error.
Is there a way to catch that error and echo a custom message to the user?
Here is my code:
COPY /b /v "F:\Buying\AccessDatabase\ Merchandis ing.accdb" "C:\Merchandising MDB Backups\Merchandising_DAY_ %date:~7,2 %.accdb"
Is there a way to catch that error and echo a custom message to the user?
Here is my code:
COPY /b /v "F:\Buying\AccessDatabase\
I forgot to mention, COPY also returns an errorlevel value. You can do this:
COPY /b /v "F:\Buying\AccessDatabase\Merchandising.accdb" "C:\Merchandising MDB Backups\Merchandising_DAY_%date:~7,2%.accdb"
IF %ERRORLEVEL% NEQ 0 (
ECHO Failed copy: Merchandising.accdb
)
ASKER
The second option looks more viable. Can I show one message if it works, and one message if it doesn't?
Yep, like this:
COPY /b /v "F:\Buying\AccessDatabase\Merchandising.accdb" "C:\Merchandising MDB Backups\Merchandising_DAY_%date:~7,2%.accdb"
IF %ERRORLEVEL% EQU 0 (
ECHO Successful copy: Merchandising.accdb
) ELSE (
ECHO Failed copy: Merchandising.accdb
)
ASKER
Thanks!
One more thing - this batch file copies three files. Is there a way, at the end, to display one message if all three were successful, and another if they were not? IE: the script would have to 'remember' what happened.
One more thing - this batch file copies three files. Is there a way, at the end, to display one message if all three were successful, and another if they were not? IE: the script would have to 'remember' what happened.
ASKER
FYI here is my whole script before this edit.
@ECHO OFF
color 03
Title Backing up Merchandising Database... Front End (accdb) [daily backup]
ECHO Backing up Merchandising Database
COPY /b /v "F:\Buying\AccessDatabase\Merchandising.accdb" "C:\Merchandising MDB Backups\Merchandising_DAY_%date:~7,2%.accdb"
ECHO front end accdb backed up successfully
ECHO.
Title Backing up Merchandising Database... Front End (mdb) [daily backup]
COPY /b /v "F:\Buying\AccessDatabase\Merchandising.mdb" "C:\Merchandising MDB Backups\Merchandising_DAY_%date:~7,2%.mdb"
ECHO front end mdb backed up successfully
ECHO.
Title Backing up Merchandising Database... Back End [daily backup]
COPY /b /v "F:\Buying\AccessDatabase\Merchandising_be.mdb" "C:\Merchandising MDB Backups\Merchandising_BackEnd_DAY_%date:~7,2%.mdb"
ECHO front end mdb backed up successfully
ECHO.
ECHO daily backup complete
ECHO press any key to exit
pause > nul
Sure, give me a mo...
Is this what you had in mind?
@ECHO OFF
color 03
set "errorcount=0"
ECHO Backing up Merchandising Database
Title Backing up Merchandising Database... Front End (accdb) [daily backup]
COPY /b /v "F:\Buying\AccessDatabase\Merchandising.accdb" "C:\Merchandising MDB Backups\Merchandising_DAY_%date:~7,2%.accdb" >nul 2>&1
if %errorlevel% neq 0 set /a errorcount+=1
Title Backing up Merchandising Database... Front End (mdb) [daily backup]
COPY /b /v "F:\Buying\AccessDatabase\Merchandising.mdb" "C:\Merchandising MDB Backups\Merchandising_DAY_%date:~7,2%.mdb" >nul 2>&1
if %errorlevel% neq 0 set /a errorcount+=1
Title Backing up Merchandising Database... Back End [daily backup]
COPY /b /v "F:\Buying\AccessDatabase\Merchandising_be.mdb" "C:\Merchandising MDB Backups\Merchandising_BackEnd_DAY_%date:~7,2%.mdb" >nul 2>&1
if %errorlevel% neq 0 set /a errorcount+=1
if %errorcount% equ 0 (
echo Daily backup completed successfully.
) else (
echo Daily backup completed. Error copying 1 or more files.
)
echo.
set /p =Press any key to exit...<nul
pause>nul
echo.
ASKER
Hi!
That works very nicely. One thing - the old script showed a message after each file was copied. Can that be preserved?
(Basically - after each file is copied, echo either that it was copied successfully, or that it errored. Then, at the end, I want the message that your script gives, based on whether or not there were errors.)
Thanks for your help!
That works very nicely. One thing - the old script showed a message after each file was copied. Can that be preserved?
(Basically - after each file is copied, echo either that it was copied successfully, or that it errored. Then, at the end, I want the message that your script gives, based on whether or not there were errors.)
Thanks for your help!
No probs. Give me a mo...
Slightly re-worked... Please try and confirm...
@ECHO OFF
COLOR 03
SET "source=F:\Buying\AccessDatabase"
SET "destination=C:\Merchandising MDB Backups"
SET datestamp=%date:~7,2%
SET "errorcount=0"
ECHO Backing up Merchandising Database
ECHO.
TITLE Backing up Merchandising Database... Front End (accdb) [Daily Backup]
SET /P =Backing up Front End (accdb)... <NUL
COPY /B /V "%source%\Merchandising.accdb" "%destination%\Merchandising_DAY_%datestamp%.accdb" >NUL 2>&1
IF %ERRORLEVEL% EQU 0 (
ECHO SUCCESS
) ELSE (
ECHO FAILED!
SET errorcount+=1
)
TITLE Backing up Merchandising Database... Front End (mdb) [daily backup]
SET /P =Backing up Front End (mdb)... <NUL
COPY /b /v "%source%\Merchandising.mdb" "%destination%\Merchandising_DAY_%datestamp%.mdb" >NUL 2>&1
IF %ERRORLEVEL% EQU 0 (
ECHO SUCCESS
) ELSE (
ECHO FAILED!
SET errorcount+=1
)
TITLE Backing up Merchandising Database... Back End [daily backup]
SET /P =Backing up Back End... <NUL
COPY /b /v "%source%\Merchandising_be.mdb" "%destination%\Merchandising_BackEnd_DAY_%datestamp%.mdb" >NUL 2>&1
IF %ERRORLEVEL% EQU 0 (
ECHO SUCCESS
) ELSE (
ECHO FAILED!
SET errorcount+=1
)
ECHO.
IF %errorcount% EQU 0 (
ECHO Daily backup completed successfully.
) ELSE (
ECHO Daily backup completed. Error copying %errorcount% file^(s^).
)
ECHO.
SET /P =Press any key to exit...<NUL & PAUSE>NUL & ECHO.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Glad you liked my approach Paul, and recognized the value in my approach, thanks.
~bp
~bp
O absolutely bill.
I must admit, the repetitive code did niggle me a bit. I ignored it hoping asker would be satisfied with the layout of the code being somewhat closer to what he was already familiar with.
I must admit, the repetitive code did niggle me a bit. I ignored it hoping asker would be satisfied with the layout of the code being somewhat closer to what he was already familiar with.
ASKER
paultomasi: Your code did the trick. Thanks for all your help!
Wow...
~bp
~bp
etech0
Thank you
bill
The only difference I see is:
"Merchandising_be.mdb" --> "Merchandising_BackEnd_DAY_%datestamp%.mdb"
and the screen layout...
Whatever it was, it seems to have done the trick!
Thank you
bill
The only difference I see is:
"Merchandising_be.mdb" --> "Merchandising_BackEnd_DAY_%datestamp%.mdb"
and the screen layout...
Whatever it was, it seems to have done the trick!
XCOPY will continue copying even when errors occur. So you command would look something like this:
XCOPY "F:\Buying\AccessDatabase\
For success or failure, you can test ERRORLEVEL like this:
Open in new window