?
Solved

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

Posted on 2007-12-06
4
Medium Priority
?
909 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 85

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 85

Accepted Solution

by:
oBdA earned 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

800 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