Page 1 of 1

Find newest file

Posted: 13 Aug 2010 02:25
by mossbs
Hi guys,

Relatively new to batch files so bear with me....

Need to write one that shall find the newest file in 4 different folders....
\\s90\Downloads\PART WKLY1\
\\s90\Downloads\PART WKLY2\
\\s90\Downloads\PART WKLY3\
\\s90\Downloads\PART WKLY4\

the files that come into here have no extension, i then need to rename the file to have a .zip extension. and then move all four files to one single folder...
\\s60\PART WKLY\

any help greatly appreciated!

Cheers,
Dan

Re: Find newest file

Posted: 13 Aug 2010 03:59
by aGerman
Probably something like that

Code: Select all

@echo off &setlocal

for /f "delims=" %%a in ('dir /a-d /b /o-d "\\s90\Downloads\PART WKLY1"') do (set "name=%%a" &goto :exitFor1)
:exitFor1
move "\\s90\Downloads\PART WKLY1\%name%" "\\s60\PART WKLY\%name%.zip"

for /f "delims=" %%a in ('dir /a-d /b /o-d "\\s90\Downloads\PART WKLY2"') do (set "name=%%a" &goto :exitFor2)
:exitFor2
move "\\s90\Downloads\PART WKLY2\%name%" "\\s60\PART WKLY\%name%.zip"

for /f "delims=" %%a in ('dir /a-d /b /o-d "\\s90\Downloads\PART WKLY3"') do (set "name=%%a" &goto :exitFor3)
:exitFor3
move "\\s90\Downloads\PART WKLY3\%name%" "\\s60\PART WKLY\%name%.zip"

for /f "delims=" %%a in ('dir /a-d /b /o-d "\\s90\Downloads\PART WKLY4"') do (set "name=%%a" &goto :exitFor4)
:exitFor4
move "\\s90\Downloads\PART WKLY4\%name%" "\\s60\PART WKLY\%name%.zip"



Regards
aGerman

Re: Find newest file

Posted: 13 Aug 2010 04:18
by mossbs
Thanks for the reply - doing what i asked for.... one small thing... it seems to be working off of the 'date modified' date.... is there any way to get it to work off of the 'date created' instead?

Cheers,
Dan.

Re: Find newest file

Posted: 13 Aug 2010 04:58
by aGerman
Try to start all FOR loops this way:
for /f "delims=" %%a in ('dir /a-d /b /o-d /tc "\\s90...

Regards
aGerman

Re: Find newest file

Posted: 13 Aug 2010 08:33
by mossbs
Superb - thanks for your help!