We help IT Professionals succeed at work.

xcopy If it's Monday

Joao Silva
Joao Silva asked
on
127 Views
Last Modified: 2017-05-18
If it's Monday
XCOPY source [destination]


If it's Tuesday
XCOPY source [destination]

...
Comment
Watch Question

Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
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?
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
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

Author

Commented:
Dear Mr. Bill Prew and Mr. Qlemo

Thanks for the tip.

Author

Commented:
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

Qlemo"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015

Commented:
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.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
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

Author

Commented:
Dear Bill Prew and Qlemo
Thank you very much. Thanks for the tip of code simplification.
Thank you very much.
Thank you
Test your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Dear Bill Prew
Thank you very much. Thank you for your help. It was great.
Thank you very much.
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.