robocopy file structure - rename file at destination if its newer

我与影子孤独终老i 提交于 2019-12-11 02:03:05

问题


I would like to robocopy a directory and it's subdirectories to another directory. If a file at source is newer then I would like to make a copy of this file by adding a date/time stamp at end of the filename at destination and do the copy to destination.

I do not see any switches in the robocopy to do this. Can someone guide me how to do this.


回答1:


Robocopy doesn't have a renaming switch, but you can use the rename command on the resulting files to add timestamps. Here's an example batch file:

@echo off
for /f "tokens=1-3 delims=. " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
for /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)

dir Directory1\ /b > list
robocopy Directory1\ Backup\
for /f %%f in (list) do rename Backup\%%f %%~nf%mydate%_%mytime%%%~xf

Note that you will need to change the delimiters of the date depending on which country standard you follow. You can get that by executing date /t



来源:https://stackoverflow.com/questions/21034925/robocopy-file-structure-rename-file-at-destination-if-its-newer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!