如果复制文件的创建日期晚于目标目录,则将其复制到复制目录中 [英] Batch file to copy directory if it has a later created date than the target directory

查看:88
本文介绍了如果复制文件的创建日期晚于目标目录,则将其复制到复制目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个文件共享,该文件共享的目录包含这样命名的所有构建版本目录>

WebApp_20140702.1第一个数字为日期,第二个数字为该日期的构建计数

然后将它们包含在以下目录中

\\server\share\product\

我需要在虚拟机上执行的操作是创建一个批处理文件,该文件可以检查我的VM上的目标位置. c:\product\通过比较文件名或日期来查看它是否具有网络共享中的最新版本,然后复制新版本并在必要时删除旧版本.

到目前为止,我可以使用xcopy复制该文件夹,但这大约是我的dos/批处理文件知识的程度iv环顾了一会儿,但看不到我可以使用的任何东西

这是我到目前为止所拥有的,如您所见,尽管我不知道如何如上所述比较两个目录.

xcopy "\\server\share\webapp" "c:\users\username\desktop\webapp" /E /K

我确实尝试只使用/D作为结尾,并且只是从\\server\share\product\复制目录,该目录的日期比目标日期晚,但最终只是复制整个目录.

让我自己清楚

我需要确定我是否具有最新的子目录,但是无论我做什么,它总是从\\server\share\product\

复制所有子目录

即. \\server\share\WebApp目录将具有以下子目录

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

我的虚拟机将具有目录

c:\product\WebApp_20140628.2\

现在,我需要能够进入文件共享,看到它具有更新的子目录,我需要将该目录复制到我的虚拟机,然后从我的虚拟机中删除较旧的目录,这样我就可以了

c:\product\WebApp_20140703.1\

解决方案

好吧,我终于找到了另一个想做类似事情的问题,而答案却完全符合我的要求

可以在以下位置找到问题:

我不确定我是否真的需要popd命令,因为我相信它会返回到pushd命令中设置的目录

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

\\server\share\product\

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 "\\server\share\webapp" "c:\users\username\desktop\webapp" /E /K

I did try to use just /D at the end and just copying the directories from \\server\share\product\ 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 \\server\share\product\

ie. the \\server\share\WebApp directory will have the following sub dirs

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

and my vm will have the directory

c:\product\WebApp_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:\product\WebApp_20140703.1\

解决方案

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

这篇关于如果复制文件的创建日期晚于目标目录,则将其复制到复制目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆