Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
648 views
in Technique[技术] by (71.8m points)

windows - Batch file to copy directory if it has a later created date than the target directory

We have a file share that has a directory containing all of our build version direcotries named like this

WebApp_20140702.1 first number being date second number being the build count for that date

these are then contained in the following directory

\servershareproduct

What i need to do from a virtual machine is create a batch file that can check the target location on my vm ie. c:product see if it has the latest version from the network share either by comparing the file names or dates, then copy the new version and delete the old if necessary.

So far i can copy the folder over using xcopy but that's about the extent of my dos/batch file knowledge iv had a look around for a while but haven't been able to see anything that i can use

This is what i have so far, as you can see though i dont know how to do the comparison between the two directories as explained above.

xcopy "\serversharewebapp" "c:usersusernamedesktopwebapp" /E /K

I did try to use just /D at the end and just copying the directories from \servershareproduct that had a later date than the target but it ended up just copying the whole directory.

EDIT : to make my self clear

i need to find out if i have the latest sub directory but no matter what i do it always copies all the sub directories from \servershareproduct

ie. the \servershareWebApp directory will have the following sub dirs

..WebApp_20140628.1
..WebApp_20140628.2
..WebApp_20140703.1

and my vm will have the directory

c:productWebApp_20140628.2

Now i need to be able to go into the file share see that it has a more up to date subdirectory i need to copy that directory to my vm and then delete the older one from my vm so i would then have

c:productWebApp_20140703.1

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

OK i eventually found another question that wanted to do a similar thing and the answer worked exactly as i wanted it

Question can be found here: How to get the most recent file using a batch script in windows

i had to use xcopy instead of the copy used in the answer for the above question. here is my solution as well in case anyone needs something similar (z is the mapped version of the network share i talk about in my question)

pushd "z:WebApp" for /f "tokens=*" %%a in ('dir/b /od') do set newest=%%a xcopy /e /k "%newest%" "c:product" popd

im not sure if i actually need the popd command as i believe it just goes back to the directory set in the pushd command


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...