Command-line Script

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?
LVL 1
sgleeAsked:
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.

NVITEnd-user supportCommented:
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

sgleeAuthor Commented:
Do I just repeat the following with 1,2,3,4,5,6?
If %dow%==6(
)
Do I need ELSE statement anywhere?
NVITEnd-user supportCommented:
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

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
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Bill PrewIT / Software Engineering ConsultantCommented:
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 supportCommented:
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
sgleeAuthor Commented:
Ok. Let me try and post results.
sgleeAuthor 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!
sgleeAuthor 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 ConsultantCommented:
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 AdvisorCommented:
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!
sgleeAuthor 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 AdvisorCommented:
You can't just run that single command, you need the complete batch. Otherwise the DOW variable will not get set.
sgleeAuthor Commented:
Duh!  
You are right. Let me try again.
sgleeAuthor 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!
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
Windows Batch

From novice to tech pro — start learning today.