How to copy last modified folder from one location to another using batch script

Hi Experts,

As per  my daily job run , Daily one folder (with some files)  will get create in a specified path. I have to copy this folder from one location to another using batch script.

So Copying should happen based on last modified date as previous day's folders will also present in the same path.

I have a script which moves all the folders from one location to another.

set source="x"
set destination="Z"
xcopy %x% %Z% /E /Y

Open in new window


I need to modify this script according to my requirement.

Any help regarding this would be highly appreciated.

Thanks in Advance/.
Midhun 123Application developerAsked:
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.

QlemoBatchelor, Developer and EE Topic AdvisorCommented:
xcopy %x% %Z% /E /Y /D should do that, but it checks for all files, and copies over any file newer than or not existing at the destination.
0
Muhammad BurhanManager I.T.Commented:
you can use
robocopy D:\Source D:\Dest /E /MAXAGE:0

Open in new window


 /S :: copy Subdirectories, but not empty ones.
 /E :: copy subdirectories, including Empty ones.
 /XD dirs [dirs]... :: eXclude Directories matching given names/paths.
 /XO :: eXclude Older files.
 /MAXAGE:n :: MAXimum file AGE - exclude files older than n days/date.
2
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
If you only want to catch the most recently changed folder:
@echo off
set source=C:\Temp
set destination=D:\Temp
set folder=
for /F "delims=" %%D in ('dir %source% /b /T:W /O:-D /A:D') do if not defined folder set folder=%%D
xcopy %source%\%folder%\ %destination%\%folder%\ /E /Y /D

Open in new window

1
yo_beeDirector of Information TechnologyCommented:
I like the robocopy suggestions, but with what you are describing I would use these switches. These are more straight forward and will catch any changes between the source and dest location.

Robocopy "source" "dest" /e /xo /w:0 /r:0

With these options you can run it against the same source directories over and over again and not have to worry what has been modified or not

The /xo compares both locations and if the file is newer than the file in the dest location. It is copied.
1

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
Midhun 123Application developerAuthor Commented:
Thank you so much for the help. You people made my day.
It worked perfectly.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.