batch file help

I would like a to create a batch file that does the following


robocopy f:\newback z:\newback

we have an exchange server named cohmail we could use to deliver messages.

i would like this to run every night at 1am
and if possible I would love it to send me a message that it completed or failed.
should I try to write this or should I try to find a third party app.
LawrencejrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

vikas_madhusudanaCommented:
at 01:00 cmd /c robocopy f:\newback z:\newback  

for more details check
http://support.microsoft.com/kb/313565
0
vikas_madhusudanaCommented:
at  01:00 /every:M,T,W,Th,F,S,Su cmd /c robocopy f:\newback z:\newback  

if you want it to happen daily
0
Steve KnightIT ConsultancyCommented:
Might aswell write it.  here is an example of mine already doing this for you in terms of the emailing:

http://scripts.dragon-it.co.uk/links/email-from-batch


You can check if there is an error or not using %errorlevel% so you can do something like:


@echo off
robocopy f:\newback Z:\newback (presume you have some options here already /mir etc perhaps?)
if errorlevel 1 call :sendemail
exit /b

:sendemail
REM Sending email stuff
exit /b

There is a (somewhat bigger than you need!) example backup script here:

http://scripts.dragon-it.co.uk/links/batch-backup-with-rotation-and-emails
0
LeeeRussellCommented:
The only thing I would add is that you could be slightly more clever and use the robocopy %errorlevel% to return a bit more information about the type of error.  I've altered the code from dragon-it slightly to allow you to add a bit more info to them email you send.
@echo off
robocopy f:\newback Z:\newback
if errorlevel 16 call:sendemail "***FATAL ERROR***"
if errorlevel 8 call:sendemail "**FAILED COPIES**"
if errorlevel 4 call:sendemail "*MISMATCHES*"
if errorlevel 2 call:sendemail "EXTRA FILES"
if errorlevel 1 call:sendemail "Copy successful"
if errorlevel 0 call:sendemail "–no change–"
exit /b

:sendemail
REM Sending email stuff using %1 to pass the error code
exit /b

Open in new window

0
Steve KnightIT ConsultancyCommented:
A very good point.  I tend to use something along these lines:

REM Errorlevel 1 = Sucessful copy, 2 is extra filed needed deleting, 3 = both
REM so Errorlevel 4 is another error
if errorlevel 4 goto error
if errorlevel 3 goto OKCopyAndDel
if errorlevel 2 goto OKDelete
if errorlevel 1 goto OKCopy
if errorlevel 0 goto OKNothing
as effectively "errors" 1-3 aren't really errors.

If not already using the robocopy options have a look at the docs but a good start is in this script (quite an old one of mine so might do bit different now but gives an idea):

@echo off
REM ----------------------------------------------------------------------------
REM Uses resource kit ROBOCOPY
REM
REM Steve Knight.  Feb 2006
REM ----------------------------------------------------------------------------

REM ROBOCOPY Options are:
REM =====================
REM /MIR - copy all files and subdirectories and remove files from dest. not in source
REM /R:1 - 1 retry for failed copies
REM /W:5 - Wait time 5 seconds between retries
REM /TEE - write output to log file and screen
REM /LOG+: - append output to filename
REM /NP - No Progress percentage as this messes up log file
REM /NJH - No Job header in log file

set TODAYDATE=%date:~7,2%-%date:~4,2%-%date:~10,4%
if "%date:~2,1%"=="/" set TODAYDATE=%date:~3,2%-%date:~0,2%-%date:~6,4%

SET logfile=z:\logs\%todaydate%.log
SET source=f:\newback
SET dest=z:\newback
SET filespec=*.*
SET options=/MIR /R:2 /W:10 /TEE /LOG+:%LOGFILE% /NP /NJH

echo Copying log files from %source% to %dest%
ROBOCOPY "%source%" "%dest%" %filespec% %options%

REM Errorlevel 1 = Sucessful copy, 2 is extra filed needed deleting, 3 = both
REM so Errorlevel 4 is another error
if errorlevel 4 goto error
if errorlevel 3 goto OKCopyAndDel
if errorlevel 2 goto OKDelete
if errorlevel 1 goto OKCopy
if errorlevel 0 goto OKNothing

:OKCopyAndDel
  echo %DATE% %TIME% - Backup Operation Successful from %SERVER% >> %LOGFILE%
goto end

:OKDelete
  echo %DATE% %TIME% - Backup Operation Successful from %SERVER% >> %LOGFILE%
goto end

:OKCopy
  echo %DATE% %TIME% - Backup Operation Successful from %SERVER% >> %LOGFILE%
goto end

:OKNothing
  echo %DATE% %TIME% - Backup Operation Successful from %SERVER% >> %LOGFILE%
goto end

:error
  echo %DATE% %TIME% - Backup Operation Failed from %SERVER% >> %LOGFILE%

REM do email sending here using %logfile% as the text file to send.

goto end

:end
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.