Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Finding the last modified file through DOS command

Posted on 2004-03-21
Medium Priority
Last Modified: 2011-09-20
Hello all,
I would like to know how to recieve the name of the file that was last modified through a DOS command. Let's say I have this case:

The number does not have anything to do with the modification date.
Anyway, let's say that in this example, the file at hand is 'textfile-03.txt'. What command do I write in order to get a result of 1 line i.e. 1 file that meats this criteria I am looking for...? The output should of course contain the file's name.
I would like to redirect the command's output to a file on the system. How do I do this, if the file&folder exists or not ...?

Question by:liorde
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
  • 3
  • 2
LVL 10

Accepted Solution

pbarrette earned 320 total points
ID: 10644026
Hi liorde,

Assuming that you are using WinNT/2K/XP, you can use this:
:: ----------EXAMPLE.BAT-----------
FOR /F "DELIMS=" %%F IN ('DIR /B /A-D /OD /TW C:\FOLDER\textfile-??.txt') DO (
ECHO Most recently modified file is:
ECHO %FILE% > X:\OutputPath\Results.txt
:: ----------EXAMPLE.BAT-----------

Hope this helps,

Author Comment

ID: 10647344
Thank-you for the reply.
Is this solution not good for WinME/9x ?

LVL 10

Expert Comment

ID: 10649576
Hi Liorde,

No. This answer will only work for WinNT/2K/XP.

This is because Win9x/ME does not have the "FOR /F" option which allows us to parse the output of a command.


Author Comment

ID: 10655778
Thank-you for the enhancement.
It was very important for me to know this.
Is there a solution that will work under Win9x/Me ...?

Thanks again.
LVL 10

Expert Comment

ID: 10660168
Hi Liorde,

If you need this to work in Win9x, you should have a look here:

The relevent contents of that link are as follows:
@echo off
dir /o-d /a-d *.* | find "-" | find ":" > en#er.bat
fc en#er.bat nul /lb1 /n |date|find " 1: " > en#er.bat
echo copy /y %%5.%%6 recent.txt > enter.bat
call en#er.bat
del en?er.bat>nul

This will copy the most recently modified file to "recent.txt".

It doesn't set the filename as a variable, but it's a decent starting point.

Hope this helps,

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
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

670 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