Batch script to Copy most recently created nonzero byte file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
san
Posts: 2
Joined: 29 Dec 2010 13:31

Batch script to Copy most recently created nonzero byte file

#1 Post by san » 29 Dec 2010 13:53

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

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch script to Copy most recently created nonzero byte

#2 Post by aGerman » 29 Dec 2010 14:50

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

san
Posts: 2
Joined: 29 Dec 2010 13:31

Re: Batch script to Copy most recently created nonzero byte

#3 Post by san » 30 Dec 2010 13:21

Thanks a lot.....that was helpful!
:)

Post Reply