Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the most recent file using a batch file?

I have a list of zip files with date and time appended like yyyymmdd_hhmmss_Demos.zip. Now how to get the most recently added zip file in the source dir. I need to copy this file in the target using copy command.

I found some info about forfiles, but do not have an idea on how to get it done for seconds.

like image 222
azzaxp Avatar asked Jul 18 '12 10:07

azzaxp


3 Answers

You can use

pushd D:\a
for /f "tokens=*" %%a in ('dir /b /od') do set newest=%%a
copy "%newest%" D:\b
popd
like image 73
Bali C Avatar answered Oct 23 '22 21:10

Bali C


set Path="D:\hello\abc\old"
for /f "tokens=*" %%a in ('dir /A:-D /B /O:-D /S %Path%') do set NEW=%%a&& goto:n 
:n
echo %NEW%
like image 20
vishal Avatar answered Oct 23 '22 23:10

vishal


pushd \\ryap\CONTROL_DATOS
for /f "tokens=*" %%a in ('dir \\ryap\CONTROL_DATOS /b /od') do set newest=%%a
Xcopy/Y "\\ryap\CONTROL_DATOS\%newest%" "D:\TXT_SOURCES\"
popd
like image 38
Mauro Avatar answered Oct 23 '22 23:10

Mauro