Solved

batch file copy catch errors

Posted on 2012-03-30
18
2,083 Views
Last Modified: 2012-04-05
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
Comment
Question by:etech0
  • 10
  • 5
  • 3
18 Comments
 
LVL 11

Expert Comment

by:paultomasi
ID: 37788100
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 37788151
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
 
LVL 10

Author Comment

by:etech0
ID: 37788274
The second option looks more viable. Can I show one message if it works, and one message if it doesn't?
0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
LVL 11

Expert Comment

by:paultomasi
ID: 37788299
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
 
LVL 10

Author Comment

by:etech0
ID: 37788463
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
 
LVL 10

Author Comment

by:etech0
ID: 37788472
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 37788757
Sure, give me a mo...
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37788860
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
 
LVL 10

Author Comment

by:etech0
ID: 37788906
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
 
LVL 11

Expert Comment

by:paultomasi
ID: 37789053
No probs. Give me a mo...
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37789178
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
 
LVL 53

Assisted Solution

by:Bill Prew
Bill Prew earned 50 total points
ID: 37791578
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
 
LVL 11

Accepted Solution

by:
paultomasi earned 450 total points
ID: 37791845
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
 
LVL 53

Expert Comment

by:Bill Prew
ID: 37791858
Glad you liked my approach Paul, and recognized the value in my approach, thanks.

~bp
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37791929
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
 
LVL 10

Author Closing Comment

by:etech0
ID: 37811815
paultomasi: Your code did the trick. Thanks for all your help!
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 37813109
Wow...

~bp
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37813854
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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question