Solved

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

Posted on 2007-12-06
4
897 Views
Last Modified: 2011-10-03
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.
0
Comment
Question by:seomaster1
  • 2
4 Comments
 
LVL 83

Expert Comment

by:oBdA
ID: 20418845
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

0
 

Author Comment

by:seomaster1
ID: 20418942
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.
0
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 20418974
Like this (/a:d in a dir command will list only directories):
@echo off

set Folder=C:\Temp

for /f "delims=" %%a in ('dir /o:d /a:d /b "%Folder%\W3SVC*"') do set LatestFolder=%%a

echo The latest W3SVC-subfolder in %Folder% is %LatestFolder%

Open in new window

0
 

Expert Comment

by:JohnyStyles577
ID: 21700646
thanks much for that, had a hard time finding this snipplet, and was very helpful.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now