[英]Batch script to copy a file from one network share to another based on lastmodified or creation date
I have two network shared drives: lets call them: DriveA and DriveB . 我有两个网络共享驱动器:让我们称之为: DriveA和DriveB 。
There are two files which get generated at DriveA on a daily basis, and I want to copy the latest files to DriveB/Folder1/fileA and DriveB/Folder2/fileB everyday. 每天在DriveA上生成两个文件,我想每天将最新文件复制到DriveB / Folder1 / fileA和DriveB / Folder2 / fileB 。
Can anyone please provide me solution wherein I can copy the files based on creation date: 任何人都可以向我提供解决方案,其中我可以根据创建日期复制文件:
I did come up with a solution but, it copies the file based on "today's date". 我确实提出了一个解决方案,但是它根据“今天的日期”复制文件。
SET usernameID=useremail@domain.com SET usernameDOMAIN=11111111 SET password=pwd@1234 net use "\\\\111.444.222.777\\FOLDER" /USER:%usernameID% %password% /persistent:no set source=\\\\222.111.333.444\\FOLDER\\FOLDER2\\Report_ net use "\\\\DOMAIN\\storage\\2012-0151_hms_ux\\Hadoop\\LiveDataLandingZoneDWH" /user:usermail@domain.com pwd@1234 /persistent:no set dest=\\\\code1\\storage\\2012-0151_hms_ux\\Hadoop\\LiveDataLandingZoneDWH\\Input\\Report\\Report_ for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x set today=%MyDate:~0,4%%MyDate:~4,2%%MyDate:~6,2% echo %today% :copy copy "%source%%today%0700.csv" "%dest%%today%0700.csv" IF ERRORLEVEL 0 goto disconnect goto end pause :disconnect goto end pause :end pause
EDIT 1: 编辑1:
Now I'm using the below command to copy the files on the date 16/Jul/2017: xcopy C:\\Users\\Desktop\\report\\ActualHRC_*.csv C:\\Users\\Downloads /D:07-16-2017 pause
现在,我使用以下命令在2017年7月16日复制文件:
xcopy C:\\Users\\Desktop\\report\\ActualHRC_*.csv C:\\Users\\Downloads /D:07-16-2017 pause
How can I provide the /D:mdy
dynamically? 如何动态提供
/D:mdy
? I mean, the variables should take the m
, d
and y
from the system. 我的意思是,变量应从系统中获取
m
, d
和y
。
Thanks in advance! 提前致谢!
I think you have made things way too complicated. 我认为您使事情变得太复杂了。 There is a very simple solution:
xcopy /Y/D
. 有一个非常简单的解决方案:
xcopy /Y/D
If you just type xcopy /?
如果只输入
xcopy /?
you can see all the options. 您可以看到所有选项。 For the
/D
option, it says: 对于
/D
选项,它表示:
Copies files changed on or after the specified date.
复制在指定日期或之后更改的文件。 If no date is given, copies only those files whose source time is newer than the destination time.
如果没有给出日期,则仅复制源时间比目标时间新的那些文件。
There is also robocopy
, which is also built into windows, offers even more options, and is allegedly capable of copying from network share to network share using UNC pathnames. 还有
robocopy
,它也内置在Windows中,提供了更多选项,并且据称能够使用UNC路径名从网络共享复制到网络共享。 (so, no drive mapping necessary.) (因此,不需要驱动器映射。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.