1.

Solve : How to create Batch file to search for latest files in a folder??

Answer»

Hi all,

I got this Folder that consists of the following files:

A_B_C_2012-03-29-09-20-21.xls
A_B_C_2012-03-28-09-20-12.xls
A_B_C_2012-03-28-09-20-05.xls
D_E_F_2012-03-29-09-10-22.xls
D_E_F_2012-03-28-09-10-11.xls
D_E_F_2012-03-28-09-10-04.xls

I want to create a batch file to copy out the latest ABC and DEF Files in the same folder.

I have created a batch file
@echo off
setlocal
set source=z:
set destdir=c:\test
pushd "%source%"

for /f "tokens=*" %%a ('dir A_B_C_*.* /b /a-d /o:e 2^>NUL') do (set lfile=%%a)
echo copying "%source%\%lfile%" to "%dest%"
copy /y "%source%\%lfile" "%dest%"

This code works FINE to extract out the latest A_B_C_ file, but when i create another batch file to extract D_E_F file, i always got an error prompt, File Not Found.
Codes for D_E_F:
@echo off
setlocal
set source=z:
set destdir=c:\test
pushd "%source%"

for /f "tokens=*" %%a ('dir D_E_F_*.* /b /a-d /o:e 2^>NUL') do (set lfile=%%a)
echo copying "%source%\%lfile%" to "%dest%"
copy /y "%source%\%lfile" "%dest%"

Any advises?When posting code please USE the code tags to increase readability. ALSO it might be better to copy/paste your actual code. The code you posted has numerous syntax errors.

The only real flaw in your logic was the sort sequence. You were sorting on extension rather than file name. Other than that, it works like a CHAR,m.

Code: [Select]@echo off
setlocal
set source=z:
set destdir=c:\test
pushd "%source%"

for /f "tokens=*" %%a in ('dir A_B_C_*.* /b /a:-d /o:n 2^>nul') do set lfile=%%a
echo copying "%source%\%lfile%" to "%destdir%"
copy /y "%source%\%lfile%" "%destdir%"

for /f "tokens=*" %%a in ('dir D_E_F_*.* /b /a:-d /o:n 2^>nul') do set lfile=%%a
echo copying "%source%\%lfile%" to "%destdir%"
copy /y "%source%\%lfile%" "%destdir%"

I combined the code into a single batch file but you can separate it if necessary.

Good luck. You are MISSING a percent sign in your copy command.



Discussion

No Comment Found