Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

batch file help

Status
Not open for further replies.

mossbs

Programmer
Joined
Aug 19, 2008
Messages
102
Location
GB
Hi guys,

Couldn't really find a .bat related forum so have stuck in here as seems most relevant!

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\

The renaming, moving etc etc - i can do but only know how to do it with a given filename...

Unsure how to find the newest file - and then i'm guessing i need to store that file name in some sort of varialble as it shall need to be referenced throughout the rest of the code.

have tried the following - taken off of a blog somewhere on how to find newest file and store in variable....but doesn't seem to be working...

Code:
::Find newest file and save it in a variable
FOR /F "delims=|" %%I IN ('\\s90\Downloads\PART WKLY1\"*.*" /B /O:D') DO SET NewestFile=%%I 
::Rename 
ren \\s90\Downloads\PART WKLY1\%NewestFile%   \\s60\PART WKLY\%NewestFile%.zip

any help greatly appreciated!

Cheers,
Dan
 
It's a bit ugly, but I think something like this would work:
Code:
@echo off

REM We assume that all these folders exist as mentioned in
REM   the discussion thread, otherwise, we'd need to add
REM   some error checking to make sure the folders exist

set basepath=\\s90\Downloads\PART WKLY
set /a fldrno=1

:loop
if %fldrno%==4 goto :done
for /f %%a in ('dir "%basepath%%fldrno%\*.*" /b /o:d') do (
  set filename=%%a
  )

REM there will be a problem here if these files have the same name
copy "%basepath%%fldrno%\%filename%" "%basepath%\%filename%.zip"
set /a fldrno=%fldrno%+1

goto loop

:done
echo Task complete
pause
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top