Link to home
Start Free TrialLog in
Avatar of Joao Silva
Joao Silva

asked on

Try / Exception

How to make a "Try / exception" from the code below?

1) "running successfully", send an email.
To: xxx@hotmail.com
Subject: Backup completed successfully.
Body: Backup performed successfully.

2) In the exception, "not executed", send an email.
To: xxx@hotmail.com
Subject: Backup failed.
Body: Backup not performed.


---Code

@echo off
setlocal

REM Get day of week number, Sunday = 1
for /f "skip=2 tokens=2 delims=," %%a in ('WMIC Path Win32_LocalTime Get DayOfWeek /Format:csv') do set /a DowNum=%%a + 1

REM Call subroutine for this days logic
call :Day%DowNum%

REM Exit this script
exit /b

:Day1
  REM Add logic for day here (Sunday)
  echo Day=1
       XCOPY C:\BNET_CIT\ATI\*.*                                                     C:\BNET_BACKUP\BKP1-DOM\ATI\ /S /Y
       XCOPY C:\BNET_CIT\DADOS\SIS\DB_BNET_CITSIS.mdf      C:\BNET_BACKUP\BKP1-DOM\DADOS\SIS\ /S /Y
       XCOPY C:\BNET_CIT\DADOS\SIS\DB_BNET_CITSIS_log.ldf  C:\BNET_BACKUP\BKP1-DOM\DADOS\SIS\ /S /Y
  exit /b

:Day2
  REM Add logic for day here (Monday)
  echo Day=2
       XCOPY C:\BNET_CIT\ATI\*.*                                                     C:\BNET_BACKUP\BKP2-SEG\ATI\ /S /Y
       XCOPY C:\BNET_CIT\DADOS\SIS\DB_BNET_CITSIS.mdf      C:\BNET_BACKUP\BKP2-SEG\DADOS\SIS\ /S /Y
       XCOPY C:\BNET_CIT\DADOS\SIS\DB_BNET_CITSIS_log.ldf  C:\BNET_BACKUP\BKP2-SEG\DADOS\SIS\ /S /Y
  exit /b

:Day3
  REM Add logic for day here (Tuesday)
  echo Day=3
       XCOPY C:\BNET_CIT\ATI\*.*                                                    C:\BNET_BACKUP\BKP3-TER\ATI\ /S /Y
       XCOPY C:\BNET_CIT\DADOS\SIS\DB_BNET_CITSIS.mdf      C:\BNET_BACKUP\BKP3-TER\DADOS\SIS\ /S /Y
       XCOPY C:\BNET_CIT\DADOS\SIS\DB_BNET_CITSIS_log.ldf  C:\BNET_BACKUP\BKP3-TER\DADOS\SIS\ /S /Y
  exit /b

:Day4
  REM Add logic for day here (Wednesday)
  echo Day=4
       XCOPY C:\BNET_CIT\ATI\*.*                                                     C:\BNET_BACKUP\BKP4-QUA\ATI\ /S /Y
       XCOPY C:\BNET_CIT\DADOS\SIS\DB_BNET_CITSIS.mdf      C:\BNET_BACKUP\BKP4-QUA\DADOS\SIS\ /S /Y
       XCOPY C:\BNET_CIT\DADOS\SIS\DB_BNET_CITSIS_log.ldf  C:\BNET_BACKUP\BKP4-QUA\DADOS\SIS\ /S /Y
  exit /b

:Day5
  REM Add logic for day here (Thursday)
  echo Day=5
       XCOPY C:\BNET_CIT\ATI\*.*                                                    C:\BNET_BACKUP\BKP5-QUI\ATI\ /S /Y
       XCOPY C:\BNET_CIT\DADOS\SIS\DB_BNET_CITSIS.mdf      C:\BNET_BACKUP\BKP5-QUI\DADOS\SIS\ /S /Y
       XCOPY C:\BNET_CIT\DADOS\SIS\DB_BNET_CITSIS_log.ldf  C:\BNET_BACKUP\BKP5-QUI\DADOS\SIS\ /S /Y
  exit /b

:Day6
  REM Add logic for day here (Friday)
  echo Day=6
       XCOPY C:\BNET_CIT\ATI\*.*                                                    C:\BNET_BACKUP\BKP6-SEX\ATI\ /S /Y
       XCOPY C:\BNET_CIT\DADOS\SIS\DB_BNET_CITSIS.mdf      C:\BNET_BACKUP\BKP6-SEX\DADOS\SIS\ /S /Y
       XCOPY C:\BNET_CIT\DADOS\SIS\DB_BNET_CITSIS_log.ldf  C:\BNET_BACKUP\BKP6-SEX\DADOS\SIS\ /S /Y
  exit /b

:Day7
  REM Add logic for day here (Saturday)
  echo Day=7
       XCOPY C:\BNET_CIT\ATI\*.*                                                     C:\BNET_BACKUP\BKP7-SAB\ATI\ /S /Y
       XCOPY C:\BNET_CIT\DADOS\SIS\DB_BNET_CITSIS.mdf      C:\BNET_BACKUP\BKP7-SAB\DADOS\SIS\ /S /Y
       XCOPY C:\BNET_CIT\DADOS\SIS\DB_BNET_CITSIS_log.ldf  C:\BNET_BACKUP\BKP7-SAB\DADOS\SIS\ /S /Y
  exit /b

Open in new window

Avatar of Bill Prew
Bill Prew

So my next simplification would be as follows.  No error checking added yet, still digesting that need...  DOS / BAT script have no way to send an email directly though, would you be able to add a third party utility like Blat to accomplish that?  Or potentially call out to a VBS script that can do the emailing?  Do you have a SMTP outgoing email server available?

@echo off
setlocal EnableDelayedExpansion

rem Set destination folders for each weekday, Sunday = 1
set Dest[1]=BKP1-DOM
set Dest[2]=BKP2-SEG
set Dest[3]=BKP3-TER
set Dest[4]=BKP4-QUA
set Dest[5]=BKP5-QUI
set Dest[6]=BKP6-SEX
set Dest[7]=BKP7-SAB

REM Get day of week number, Sunday = 1
for /f "skip=2 tokens=2 delims=," %%a in ('WMIC Path Win32_LocalTime Get DayOfWeek /Format:csv') do set /a DowNum=%%a + 1

rem Set todays destination folder
set DestDir=!Dest[%DowNum%]!

rem Copy todays files to their destination folder
XCOPY C:\BNET_CIT\ATI\*.*                           C:\BNET_BACKUP\%DestDir%\ATI\ /S /Y
XCOPY C:\BNET_CIT\DADOS\SIS\DB_BNET_CITSIS.mdf      C:\BNET_BACKUP\%DestDir%\DADOS\SIS\ /S /Y
XCOPY C:\BNET_CIT\DADOS\SIS\DB_BNET_CITSIS_log.ldf  C:\BNET_BACKUP\%DestDir%\DADOS\SIS\ /S /Y

Open in new window

~bp
Avatar of Joao Silva

ASKER

Dear Bill Prew and Qlemo
Thank you very much. Thanks for the tip of code simplification.
Thank you very much.
Thank you
As far as checking for errors here is a basic approach.  It will set a variable if errors occur during any copy, and then the variable can be checked after the copies.  Then the appropriate email can be sent.  For info and examples of email from BAT, take a look at this experts reference info:


@echo off
setlocal EnableDelayedExpansion

rem Set destination folders for each weekday, Sunday = 1
set Dest[1]=BKP1-DOM
set Dest[2]=BKP2-SEG
set Dest[3]=BKP3-TER
set Dest[4]=BKP4-QUA
set Dest[5]=BKP5-QUI
set Dest[6]=BKP6-SEX
set Dest[7]=BKP7-SAB

REM Get day of week number, Sunday = 1
for /f "skip=2 tokens=2 delims=," %%a in ('WMIC Path Win32_LocalTime Get DayOfWeek /Format:csv') do set /a DowNum=%%a + 1

rem Set todays destination folder
set DestDir=!Dest[%DowNum%]!

rem Copy todays files to their destination folder
set HadErrors=N
XCOPY C:\BNET_CIT\ATI\*.*                           C:\BNET_BACKUP\%DestDir%\ATI\ /S /Y        || (set HadErrors=Y)
XCOPY C:\BNET_CIT\DADOS\SIS\DB_BNET_CITSIS.mdf      C:\BNET_BACKUP\%DestDir%\DADOS\SIS\ /S /Y  || (set HadErrors=Y)
XCOPY C:\BNET_CIT\DADOS\SIS\DB_BNET_CITSIS_log.ldf  C:\BNET_BACKUP\%DestDir%\DADOS\SIS\ /S /Y  || (set HadErrors=Y)

rem Check for any errors during copies and send email
if "%HadErrors%" EQU "Y" (
  rem Send email indicating failure here
) else (
  rem Send email indicating success here
)

Open in new window

~bp
Dear Bill Prew
Thank you very much. Thank you for your help. It was great.
Thank you very much.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please give me one help
Error:
   How to send email by blat as per script below?


@echo off
setlocal EnableDelayedExpansion

rem Set destination folders for each weekday, Sunday = 1
set Dest[1]=BKP1-DOM
set Dest[2]=BKP2-SEG
set Dest[3]=BKP3-TER
set Dest[4]=BKP4-QUA
set Dest[5]=BKP5-QUI
set Dest[6]=BKP6-SEX
set Dest[7]=BKP7-SAB

REM Get day of week number, Sunday = 1
for /f "skip=2 tokens=2 delims=," %%a in ('WMIC Path Win32_LocalTime Get DayOfWeek /Format:csv') do set /a DowNum=%%a + 1

rem Set todays destination folder
set DestDir=!Dest[%DowNum%]!

rem Copy todays files to their destination folder
set HadErrors=N
   
    XCOPY C:\BNET_CIT\ATI\*.*                       C:\BNET_BACKUP\%DestDir%\ATI\   /S /Y  || (set HadErrors=Y)
    XCOPY C:\BNET_CIT\CIA\*.*                       C:\BNET_BACKUP\%DestDir%\CIA\   /S /Y  || (set HadErrors=Y)

rem Check for any errors during copies and send email
if "%HadErrors%" EQU "Y" (
  rem Send email indicating failure here

  REM * Uses blat.exe and body.txt as body of message:
     set dir=C:\BNET_BACKUP\1_Blat_SendMail_v3217\full
     set hostname=servername
     set subject="* O Backup Falhou. Verifique o erro e tome as devidas providencias."
     set toaddress=xxx@xxx.com.br
     set receipt=NO

) else (
  rem Send email indicating success here

  REM * Uses blat.exe and body.txt as body of message:
     set dir=C:\BNET_BACKUP\1_Blat_SendMail_v3217\full
     set hostname=servername
     set subject="* O Backup foi executado com sucesso."
     set toaddress=xxx@xxx.com.br
     set receipt=NO

)
exit