batch file to Copy FOLDER with newest time stamp from network drive to local

时间:2016-10-20 13:12:48

标签: batch-file

I need to write a batch file to copy a folder, not file, from a network drive, that has been the most recently updated, to my local machine.

This is what I have, but it cannot find anything to copy because I want to copy a folder, not a file.

for /f "delims=" %%a in ('dir S:\Development\NightlyBuilds /B /A-D /O-D') do copy "%%a" U:\PWJ

2 个答案:

答案 0 :(得分:0)

您可以使用copy而不是xcopy。使用/s标志递归复制。确保使用\终止PWJ文件夹,以指示目标是文件夹。您也可以使用robocopy。它更加强大。您可以在robocopy /?

中查找其参数

答案 1 :(得分:0)

您使用了错误的FOR和DIR命令选项,为最新到最旧的日期中的每个文件执行复制。

@ECHO OFF

(SET SrcDir=S:\Development\NightlyBuilds)
(SET DstDir=U:\PWJ)

IF NOT EXIST "%SrcDir%\" EXIT/B
IF NOT EXIST "U:\PWJ\" EXIT/B
IF /I NOT "%CD%"=="%SrcDir%" PushD "%SrcDir%"

FOR /F "DELIMS=" %%A IN ('DIR/B/AD-L/OD *') DO SET Latest="%%A"
ROBOCOPY %Latest% "%DstDir%\%Latest%" /E

EXIT/B