Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Finding the last modified file through DOS command

Posted on 2004-03-21
5
Medium Priority
?
5,036 Views
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:
c:\folder\textfile-05.txt
c:\folder\textfile-12.txt
c:\folder\textfile-03.txt
c:\folder\textfile-43.txt

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 ...?

Thank-you.
0
Comment
Question by:liorde
  • 3
  • 2
5 Comments
 
LVL 10

Accepted Solution

by:
pbarrette earned 320 total points
ID: 10644026
Hi liorde,

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

Hope this helps,
pb
0
 

Author Comment

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

0
 
LVL 10

Expert Comment

by:pbarrette
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.

pb
0
 

Author Comment

by:liorde
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.
0
 
LVL 10

Expert Comment

by:pbarrette
ID: 10660168
Hi Liorde,

If you need this to work in Win9x, you should have a look here:
http://www.ericphelps.com/batch/samples/recent.txt

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
cls
------------------------------------

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,
pb
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

926 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