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

asked on

xcopy If it's Monday

If it's Monday
XCOPY source [destination]


If it's Tuesday
XCOPY source [destination]

...
Avatar of Qlemo
Qlemo
Flag of Germany image

DOS itself cannot do that, it has no means to know the day of week (reliably). I'm certain Bill is preparing a mixed VBS/cmd solution at the moment ...
To be clear: Do you want to execute different commands each day, or just change the source and/or destination?
Avatar of Bill Prew
Bill Prew

Here is a sample solution from another question I responded to, should give you some ideas...

@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
  exit /b

:Day2
  REM Add logic for day here (Monday)
  echo Day=2
  exit /b

:Day3
  REM Add logic for day here (Tuesday)
  echo Day=3
  exit /b

:Day4
  REM Add logic for day here (Wednesday)
  echo Day=4
  exit /b

:Day5
  REM Add logic for day here (Thursday)
  echo Day=5
  exit /b

:Day6
  REM Add logic for day here (Friday)
  echo Day=1
  exit /b

:Day7
  REM Add logic for day here (Saturday)
  echo Day=7
  exit /b

Open in new window

~bp
Avatar of Joao Silva

ASKER

Dear Mr. Bill Prew and Mr. Qlemo

Thanks for the tip.
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

Am I correct that only the copy destination is different? Then the script should be simpliefied to use the same command each day, and just put the respective destination into a variable. That also simplifies the error trapping.
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
Dear Bill Prew and Qlemo
Thank you very much. Thanks for the tip of code simplification.
Thank you very much.
Thank you
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
Dear Bill Prew
Thank you very much. Thank you for your help. It was great.
Thank you very much.
Joao

Take a look at this article, it should help you with the question close process.


For future reference here some other items related to closing questions in case they are helpful.


And don't forget you can always "request attention" on a question if you are not sure how to proceed and a support person will be in touch to help you out.


»bp