• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3196
  • Last Modified:

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\Merchandising.accdb" "C:\Merchandising MDB Backups\Merchandising_DAY_%date:~7,2%.accdb"
0
etech0
Asked:
etech0
  • 10
  • 5
  • 3
2 Solutions
 
paultomasiCommented:
How about using XCOPY with the /C option?

XCOPY will continue copying even when errors occur. So you command would look something like this:

    XCOPY "F:\Buying\AccessDatabase\Merchandising.accdb" "C:\Merchandising MDB Backups\Merchandising_DAY_%date:~7,2%.accdb" /C /R /V /Y

For success or failure, you can test ERRORLEVEL like this:
XCOPY "F:\Buying\AccessDatabase\Merchandising.accdb" "C:\Merchandising MDB Backups\Merchandising_DAY_%date:~7,2%.accdb" /C /R /V /Y

IF %ERRORLEVEL% NEQ 0 (
  ECHO Failed copy: Merchandising.accdb
)

Open in new window

0
 
paultomasiCommented:
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
)

Open in new window

0
 
etech0Author Commented:
The second option looks more viable. Can I show one message if it works, and one message if it doesn't?
0
Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

 
paultomasiCommented:
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
)

Open in new window

0
 
etech0Author Commented:
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.
0
 
etech0Author Commented:
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

Open in new window

0
 
paultomasiCommented:
Sure, give me a mo...
0
 
paultomasiCommented:
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.

Open in new window

0
 
etech0Author Commented:
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!
0
 
paultomasiCommented:
No probs. Give me a mo...
0
 
paultomasiCommented:
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.

Open in new window

0
 
Bill PrewCommented:
Here's a slightly shortened and more modular approach, in case it helps at all.
@echo off

set FromDir=F:\Buying\AccessDatabase
set DestDir=C:\Merchandising MDB Backups
set ErrorCount=0

echo Backing up Merchandising Databases

call :DoCopy "Merchandising.accdb"
call :DoCopy "Merchandising.mdb"
call :DoCopy "Merchandising_be.mdb"

echo Backup completed with [%ErrorCount%] errors.
ECHO Press any key to exit
pause >NUL
goto :EOF

:DoCopy [filename]
  echo Copying file [%~1]

  copy /B /V "%FromDir%\%~1" "%DestDir%\%~n1_DAY_%DATE:~7,2%%~x1"

  if %ERRORLEVEL% EQU 0 (
    echo Copy succeeded.
  ) else (
    echo Copy failed.
    set /A ErrorCount+=1
  )
  goto :EOF

Open in new window

~bp
0
 
paultomasiCommented:
Hiya bill...

Not bad.
Using CALL I would have done it like this:
@echo off

set source=F:\Buying\AccessDatabase
set destination=C:\Merchandising MDB Backups

set errorcount=0
set datestamp=%date:~7,2%

echo.
echo Backing up Merchandising Databases
echo.

call :DScopy "Merchandising.accdb"  "Merchandising_DAY_%datestamp%.accdb"
call :DScopy "Merchandising.mdb"    "Merchandising_DAY_%datestamp%.mdb"
call :DScopy "Merchandising_be.mdb" "Merchandising_BackEnd_DAY_%datestamp%.mdb"

echo.
echo Backup completed with [%errorcount%] errors.
echo.

>con set /p =Press any key to exit... <nul & pause >nul
exit


:DScopy
  set /p =Copying: %~1... <nul

  copy /b /v "%source%\%~1" "%Destination%\%~2" >nul 2>&1

  if %errorlevel% equ 0 (
    echo OK
  ) else (
    echo FAILED
    set /a errorcount+=1
  )
goto :eof

Open in new window

CONSIDERATIONS

1) The '>con' at the start of line 21 is an afterthought; added in the event output from the batch file is redirected.

2) The '>nul 2>&1' at the end of line 28 is to ensure there is no output between 'set /p...<nul' and the copy status 'OK' or 'FAILED'.

3) Because renaming isn't consistent, I prefer to use CALL passing two parameters.

4) Using a datestamp rather than repeatedly using '%date:~7,2%' seemed to make more sense to me. There is a ramification doing it this way which has advantages.

5) There is no indication how large these files are nor how long it is likely to take copying and then verifying them. For this reason I prefer to use my good old method of sending output to the same line using 'set /p...<nul' on line 26 followed by a subsequent output to the console. This has the nice appearance of displaying the following:

    Copying: Merchandising.mdb...

while the file is being copied, and only then adding 'OK' or 'FAILED' onto the end of the line when the file has been verified, like this:

    Copying: Merchandising.mdb... OK

    Copying: Merchandising.mdb... FAILED

6) I feel presentation often makes a program stand out from the rest. For this reason I have added additional 'ECHO.' statements to output blank lines at carefully chosen times.

7) Using my method 'set /p...<nul' in the following line of code ensures the cursor waits at the end of the custom message rather than dropping down onto a new line:

    set /p =Press any key to exit... <nul & pause >nul

Those are just a few of my considerations. As you can see, much thought has gone into this from a design point of view.

Such is the creativeness of programming! :)
0
 
Bill PrewCommented:
Glad you liked my approach Paul, and recognized the value in my approach, thanks.

~bp
0
 
paultomasiCommented:
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.
0
 
etech0Author Commented:
paultomasi: Your code did the trick. Thanks for all your help!
0
 
Bill PrewCommented:
Wow...

~bp
0
 
paultomasiCommented:
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!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 10
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now