Page 1 of 1

Batch script to Copy most recently created nonzero byte file

Posted: 29 Dec 2010 13:53
by san
Hello,

Could you please help me in scripting following scenario using batch programming:
1. There are report files generated every hour at C:\Report, example:
Report_1a.xls
Report_1b.xls
Report_0a.xls
Report_3b.xls
2. At times 0 KB report files get generated

I have following script for copying the most recently created file to C:\LatestReport
set srcDir=C:\Report
set destdir=C:\LatestReport
set latest=
pushd "%srcDir%"
for /f "tokens=*" %%a in ('dir /b /od 2^>NUL') do set latest=%%a
if "%latest%"=="" echo Could not locate files.&goto :eof
copy "%latest%" "%destDir%"

As you could see the script doesn't ignore the 0 KB files.
I would like to enhance this script to ignore 0 KB files & pick always non-zero byte file which is most recently created and move it to C:\LatestReport

Any help suggestion would be highly appreciated

Regards,
San

Re: Batch script to Copy most recently created nonzero byte

Posted: 29 Dec 2010 14:50
by aGerman
Try something like that:

Code: Select all

for /f "tokens=*" %%a in ('dir /b /od 2^>NUL') do (
  if "%%~za" neq "0" set latest=%%a
)


Regards
aGerman

Re: Batch script to Copy most recently created nonzero byte

Posted: 30 Dec 2010 13:21
by san
Thanks a lot.....that was helpful!
:)