Link to home
Start Free TrialLog in
Avatar of seomaster1
seomaster1

asked on

How can I create a bat script to find the newest log file in a folder?

Hello,

How can I create a bat script to find the newest log file in a folder?

Also, how can I place the date in a file name with only two digets for the year

Thanks,

Asher.
Avatar of oBdA
oBdA

Try these:

@echo off
set Folder=C:\Temp
for /f "delims=" %%a in ('dir /o:d /b "%Folder%\*.log"') do set LatestLog=%%a
echo The latest log file is %LatestLog%
 
 
@echo off
for /f "tokens=1-3 delims=/" %%a in ("%Date%") do (
  set mm=%%a
  set dd=%%b
  set yyyy=%%c
)
set yy=%yyyy:~2%
set MyDate=%yy%-%mm%-%dd%
echo Date in format yy-mm-dd: %MyDate%

Open in new window

Avatar of seomaster1

ASKER

That's great! thank you so much!

How would I replicate the Lastest File part to look for the latest Folder beginning with W3SVC?

Thanks,

Asher.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks much for that, had a hard time finding this snipplet, and was very helpful.