bat file to copy files


I am using peersync software to syncrhonize my oracle data to remote location. Now I want to copy the synchronized data to 7 weekdays folders. I want to run a single bat file to do this.

i have 7 folders mon, tue...sun. The syncrhonized folder name is daily. Once the synchronize happed i want to run a script/bat file so that it should copy the files inside daily folder to mon,tue... depending on todays week day.

how Ican do this
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.

luc_roySystem AdminCommented:

Just xcopy /s c:\source d:\target

Here are all the commands

•/s/e - recursive copy, including copying empty directories.
•/v - add this to verify the copy against the original. slower, but for the paranoid.
•/h - copy system and hidden files.
•/k - copy read-only attributes along with files. otherwise, all files become read-write.
•/x - if you care about permissions, you might want /o or /x.
•/z - if you think the copy might fail and you want to restart it, use this. It places a marker on each file as it copies, so you can rerun the xcopy command to pick up from where it left off.

here is a link to someone who did a similar project
teksalahAuthor Commented:
this will copy only to one target.. But I have 7 targets D:\monday; D:\tuesday... D:\sunday.  So on daily basis I want to copy to specific folders. On monday I want to copy to D:\monday and so on using one bat file

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Could you please provide the output of this commands:

echo %date%
date /t

Depending on that, it might be necessary to use VBScript instead of a batch file.
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Farhan KaziSystems EngineerCommented:
Check following batch script, if this works for you!


SET MM=%DATE:~4,2%
SET DD=%DATE:~7,2%

SET SPath=C:\Daily
SET TPath=D:\WeekDays\%DAY%

IF NOT EXIST "%SPath%" ECHO Invalid source. &Goto :End
IF NOT EXIST "%TPath%" ECHO Invalid target. &Goto :End

XCOPY "%SPath%" "%TPath%" /E /C /H /R /Y


Open in new window

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
The locale settings may provide the weekday in %date% or not, that is why I asked for the output of the commands. The code above will not work on my system - no weekday here!
Steve KnightIT ConsultancyCommented:
You have it handled there between Qlemo andfarhankazi if your date format is OK but there is some more info. on getting date entries such as weekday here in my article:
Steve KnightIT ConsultancyCommented:
Once you have %weekday% etc. in a variable of course it is easy to do an xcopy c:\oldpath c:\%weekday% /s /d /y etc.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Yes, Steve, sorry I didn't think of including a link to this great article. Will you forgive me?
Bill PrewIT / Software Engineering ConsultantCommented:
A couple of thoughts:

First, is there no way in peersync to do this, it seems like a fairly robust tool, does it no allow intellignetly selecting a destination directory?  Or if you are running it in a batch file on the server, perhaps you should add logic to that existing batch file to pass in the destination folder on the command line, rather than having a new batch file?

Also, if you do need to proceed with the approach in this question, with a seperate batch file to run after the fact, I'd recommend going off of the date stamp of the actual file, rather than the current data.  This way if you have more than one days files in the collector folder, or for some reason the breakout script doesn't run on the same day as the peersync job, it still sends the files to the correct day of the week subfolder.  This approach feels more failsafe to me, and more robust, with not a lot more work.

teksalahAuthor Commented:
HI,  The below batch file will run after the peer-sync completes the synchronization and copies the synchronized files depending upon the weekdays. Only one issue is , it should run before 23.59Hrs, Otherwise it will copy to the next day.


for /f %%a in ('date /t') do set DAY=%%a
if %DAY%==Mon goto:mon
if %DAY%==Tue goto:tue
if %DAY%==Wed goto:wed
if %DAY%==Thu goto:thu
if %DAY%==Fri goto:fri
if %DAY%==Sat goto:sat
if %DAY%==Sun goto:sun


copy d:\daily\*.* d:\weekly\monday\



copy d:\daily\*.* d:\weekly\tuesday\



copy d:\daily\*.* d:\weekly\wednesday\



copy d:\daily\*.* d:\weekly\thursday\



copy d:\daily\*.* d:\weekly\friday\



copy d:\daily\*.* d:\weekly\saturday\



copy d:\daily\*.* d:\weekly\sunday\



any comments appreciated
Steve KnightIT ConsultancyCommented:

1. This depends upon the date format used by the user scheduling the script.  Be careful (which is why I suggested VBScript approach to grab the date).

2. Why not just use directories called Mon, Tue, Wed etc. or if you want to use the method you have but save some code then you could shorten it to:


for /f %%a in ('date /t') do set DAY=%%a
if %DAY%==Mon call :backup Monday
if %DAY%==Tue call :backup Tuesday
if %DAY%==Wed call :backup Wednesday
if %DAY%==Thu call :backup Thursday
if %DAY%==Fri call :backup Friday
if %DAY%==Sat call :backup Saturday
if %DAY%==Sun call :backup Sunday
exit /b

  copy d:\daily\*.* d:\weekly\%1\
exit /b


for /f %%a in ('date /t') do set DAY=%%a
if %DAY%==Mon copy d:\daily\*.* d:\weekly\Monday\
if %DAY%==Tue copy d:\daily\*.* d:\weekly\Tuesday\
if %DAY%==Wed copy d:\daily\*.* d:\weekly\Wednesday
if %DAY%==Thu copy d:\daily\*.* d:\weekly\Thursday
if %DAY%==Fri copy d:\daily\*.* d:\weekly\Friday
if %DAY%==Sat copy d:\daily\*.* d:\weekly\Saturday
if %DAY%==Sun copy d:\daily\*.* d:\weekly\Sunday
exit /b


for /f %%a in ('date /t') do set DAY=%%a
copy d:\daily\*.* d:\weekly\%day%
exit /b

Personally I'd put some error checking in there, probably look to see if the dir existed first, maybe email notifications.  Need to deal with what files may or not be there in the Monday directory say on the second Monday it runs etc.


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
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.