We help IT Professionals succeed at work.

Copy 'Last Modified File' into new File

deharvy
deharvy asked
on
Medium Priority
636 Views
Last Modified: 2010-04-21
Via the CMD line, Is there a way to locate the last modified file in a directory? I keep all my syslog_*.log and my sql_*.log files in the same directory. I want to be able to run a CMD command that will copy the 'last modified' sql_*.log into the file last.log (and disregard all syslog).

Is this possible?
Comment
Watch Question

Top Expert 2007
Commented:
You could do something like this. You'd want to set the srcDir equal to where the files exist:
@echo off
 
setlocal
 
set srcDir=C:\MyLogs
 
pushd "%srcDir%"
 
set lastLog=
 
for /f "tokens=*" %%a in ('dir /b /od sql_*.log 2^>NUL') do set lastLog=%%a
 
if "%lastLog%"=="" echo Could not locate log files.&goto CLEANUP
 
copy "%lastLog%" last.log
 
:CLEANUP
 
popd

Open in new window

Author

Commented:
Excellent. The script works perfectly. No need to elaborate.

Author

Commented:
The script works perfectly. No need to elaborate.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.