Avatar of sglee
sglee

asked on 

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?
Windows BatchMicrosoft DOSWindows OS

Avatar of undefined
Last Comment
Bill Prew
Avatar of NVIT
NVIT
Flag of United States of America image

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

Avatar of sglee
sglee

ASKER

Do I just repeat the following with 1,2,3,4,5,6?
If %dow%==6(
)
Do I need ELSE statement anywhere?
ASKER CERTIFIED SOLUTION
Avatar of NVIT
NVIT
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
SOLUTION
Avatar of Bill Prew
Bill Prew

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of NVIT
NVIT
Flag of United States of America image

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
Avatar of sglee
sglee

ASKER

Ok. Let me try and post results.
Avatar of sglee
sglee

ASKER

@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!
Avatar of sglee
sglee

ASKER

@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.
Avatar of Bill Prew
Bill Prew

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
Avatar of Qlemo
Qlemo
Flag of Germany image

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!
Avatar of sglee
sglee

ASKER

User generated image@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%
Avatar of Qlemo
Qlemo
Flag of Germany image

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

ASKER

Duh!  
You are right. Let me try again.
Avatar of sglee
sglee

ASKER

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!
SOLUTION
Avatar of Bill Prew
Bill Prew

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Windows OS
Windows OS

This topic area includes legacy versions of Windows prior to Windows 2000: Windows 3/3.1, Windows 95 and Windows 98, plus any other Windows-related versions including Windows Mobile.

129K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo