• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 624
  • Last Modified:

Copy 'Last Modified File' into new File

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?
  • 2
1 Solution
You could do something like this. You'd want to set the srcDir equal to where the files exist:
@echo off
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

Open in new window

deharvyAuthor Commented:
Excellent. The script works perfectly. No need to elaborate.
deharvyAuthor Commented:
The script works perfectly. No need to elaborate.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now