Command-line Script

sglee
sglee used Ask the Experts™
on
Hi,
  I currently have .BAT files (that run at scheduled time in Task Scheduler) and they backs up files and folders from the Server to the shared folder off the workstation PC. I have five .BAT files like below.

   Monday.bat
    Robocopy C:\Data      \\frontdesk2\ServerBackup\Mon\Data            /MIR /W:0 /R:0

  Tuesday.bat
    Robocopy C:\Data      \\frontdesk2\ServerBackup\Tue\Data            /MIR /W:0 /R:0

  Friday.bat
    Robocopy C:\Data      \\frontdesk2\ServerBackup\Fri\Data            /MIR /W:0 /R:0

  I like to be able to combine all these batch files into one by writing a script like below and have just one in TaskSchd.msc.

  If today is Monday then
    Robocopy C:\Data      \\frontdesk2\ServerBackup\Mon\Data            /MIR /W:0 /R:0
  else if today is Tuesday then
      Robocopy C:\Data      \\frontdesk2\ServerBackup\Tue\Data            /MIR /W:0 /R:0
   else if today is Friday
    Robocopy C:\Data      \\frontdesk2\ServerBackup\Fri\Data            /MIR /W:0 /R:0
endif

Is it possible?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
NVITEnd-user support

Commented:
Here's a way to get the day number. Expand the example to include each day.
Saturday = 6:

for /f "skip=2 tokens=2 delims=," %%A in ('WMIC Path Win32_LocalTime Get DayOfWeek /Format:csv') do set "DOW=%%A"
if %DOW% == 6 (
  REM Saturday robocopy code here...
  Robocopy C:\Data      \\frontdesk2\ServerBackup\Sat\Data            /MIR /W:0 /R:0
)

Open in new window

Author

Commented:
Do I just repeat the following with 1,2,3,4,5,6?
If %dow%==6(
)
Do I need ELSE statement anywhere?
End-user support
Commented:
for /f "skip=2 tokens=2 delims=," %%A in ('WMIC Path Win32_LocalTime Get DayOfWeek /Format:csv') do set "DOW=%%A"
if %DOW% == 1 (
  Robocopy C:\Data      \\frontdesk2\ServerBackup\Mon\Data            /MIR /W:0 /R:0
)
if %DOW% == 2 (
  Robocopy C:\Data      \\frontdesk2\ServerBackup\Tue\Data            /MIR /W:0 /R:0
)
if %DOW% == 3 (
  Robocopy C:\Data      \\frontdesk2\ServerBackup\Wed\Data            /MIR /W:0 /R:0
)
if %DOW% == 4 (
  Robocopy C:\Data      \\frontdesk2\ServerBackup\Thu\Data            /MIR /W:0 /R:0
)
if %DOW% == 5 (
  Robocopy C:\Data      \\frontdesk2\ServerBackup\Fri\Data            /MIR /W:0 /R:0
)
if %DOW% == 6 (
  Robocopy C:\Data      \\frontdesk2\ServerBackup\Sat\Data            /MIR /W:0 /R:0
)
if %DOW% == 7 (
  Robocopy C:\Data      \\frontdesk2\ServerBackup\Sun\Data            /MIR /W:0 /R:0
)

Open in new window

CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Bill PrewIT / Software Engineering Consultant
Top Expert 2016
Commented:
Here's how I would approach it, let me know if this doesn't make sense.

@echo off
setlocal EnableDelayedExpansion

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

REM Convert day of week number to text abbreviation
for /f "tokens=%DowNum%" %%a in ("Sun Mon Tue Wed Thu Fri Sat") do set DOW=%%a

REM Backup to todays folder
Robocopy C:\Data \\frontdesk2\ServerBackup\%DOW%\Data /MIR /W:0 /R:0

Open in new window

~bp
NVITEnd-user support

Commented:
I can't confirm right now but I believe Sunday = 0. Thus, please adjust each number accordingly.

Sun = 0, Mon = 1, Tue = 2, Wed = 3, Thu = 4, Fri = 5, Sat = 6

Author

Commented:
Ok. Let me try and post results.

Author

Commented:
@NVIT
I added it to the task scheduler, ran it manually and confirmed that files and folders are being copied to the shared destination.

Thanks for your help!

Author

Commented:
@Bill
Since the batch file is currently running and copying files to destination folder, let me wait until tomorrow and try your script tomorrow.
I will post the result.
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Okay, I tested it here and it seemed to work, hopefully you will get similar results,

I think it would be a little smaller, efficient, and easier to maintain if folder names changed in the future, or robocopy options, etc.

But as always it's your question and you decide what works best for you.

~bp
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
You should definiitvely go with Bill's code. Repeating almost the same code several times is bad style, prone to errors, and makes changes more difficult.

NVIT's approach to use WMI for DOW is great, have to remember that!

Author

Commented:
Backup Destination@BP
I ran this command - Robocopy C:\Data \\frontdesk2\ServerBackup\%DOW%\Data /MIR /W:0 /R:0
and it actually created a folder name "%DOW%
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
You can't just run that single command, you need the complete batch. Otherwise the DOW variable will not get set.

Author

Commented:
Duh!  
You are right. Let me try again.

Author

Commented:
That was it. It worked. Thank you all for your help.
BTW, where and how did you learn all these commands?
Back in the day I learned some DOS commands when I bought IBM PC with two FDs, but you guys are good!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial