开发者

Copy only newest folder via batch file

I need to copy over only the latest folder from 开发者_开发百科a folder. All the examples I see online are for copying just the newest file. Is it possible to copy over the latest folder via batch file?


CD folder
FOR /F "USEBACKQ tokens=*" %%A IN (`DIR /A:D /O:-D`) DO (
 XCOPY "%%A" "new folder" /E /H /K /Y
 GOTO:OUT
)

:OUT

Change directory to the folder in question. Use a FOR loop over the listing of all folders in current folder, arrange them by date with the newest on top, then copy all the contents of that folder to the new destination (/E Copies all directories and subdirectories even if empty, /H include all hidden and system files, /K keeps attribute settings, /Y suppresses the prompt to copy), then leave the loop after the first iteration (i.e. newest file)


Have you tried Robocopy?

Edit to address downvote

robocopy .\src .\dest /e /xo /dcopy:T

Options

/xo 
Excludes older files.

/e  
Copies subdirectories. Note that this option includes empty directories.

/dcopy:T    
Copies directory time stamps.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜