Link to home
Start Free TrialLog in
Avatar of gsgi
gsgiFlag for United States of America

asked on

create file date time windows

Find the latest created file in a directory:
The below command would print the files in the order of creation date & time. In this list the file that is created very recently would be displayed in the last position.

dir /A:-D /T:C /O:D
Example:

C:\>dir /A:-D /T:C /O:D
02/06/2012  07:38 PM                 4 4.txt
02/06/2012  07:39 PM                 0 5.txt
02/06/2012  10:45 PM                13 10.txt
02/06/2012  10:47 PM                13 newfile.t
02/11/2012  08:24 PM                83 2.bat
02/11/2012  08:26 PM             5,219 data.txt
02/11/2012  08:27 PM             5,219 data2.txt
02/12/2012  11:28 PM                98 3.bat
02/13/2012  10:47 AM               131 echo.bat
SOLUTION
Avatar of duncanb7
duncanb7

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gerwin Jansen
Do you have the tail command available? If so you can do this:
dir /A:-D /T:C /O:D | findstr /r /c:"^[0-9]" | tail -1

Open in new window

The findstr command will filter only lines starting with a number and tail -1 will show you only the last line of that filtered output.

Tail is part of a set of WIn32 ported UNIX tools: http://unxutils.sourceforge.net/
Avatar of gsgi

ASKER

Thanks Everyone!!!!
@gsgi - Did you try the first comment that you've chosen as an answer? I did and I got this as reply:

Latest created file is "07/19/14  09:38 PM                 0 junk.txt"

So I believe you may have chosen the wrong answer, you can ask for attention using the "Request Attention" button above.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
See my comment above, all needed corrections are there. I tested and it worked.
Avatar of gsgi

ASKER

Would this work - I am not a .bat file scripter so my syntax is probably wrong...  By using temporary for the last line of the listing and setting newest to temporary each time through the loop, newest becomes the 2nd to last filename in the listing, so we do not grab junk.txt

@echo off
dir /A:-D /T:C /O:D |find ":">junk.txt
set temporary=""
for /f "tokens=1 delims=" %%a in ('type junk.txt') do {set newest=temporary set temporary=%%aa}
if not "%newest%"=="" echo Latest created file is "%newest%"
del junk.txt

-gsgi
The idea is good, with some more modifications, this is working as long as you don't have the script in the folder where you run it:
@echo off
SETLOCAL EnableDelayedExpansion
dir /A:-D /T:C /O:D |find ":">junk.txt
set temporary=""
for /f "tokens=1 delims=" %%a in ('type junk.txt') do set newest=!temporary! && set temporary=%%a
if not "%newest%"=="" echo Latest created file is "%newest%"
del junk.txt

Open in new window


but my suggestion above in http:#a40206573 is working as well.
Avatar of gsgi

ASKER

Thanks for participating in this question.  I originally selected duncanb7 as the best answer because I could follow his thinking and his answer used no extra tools or utilities.  Gerwin Jansen wrote to me asking why I would select an answer that did not work as the best solution and he fixed the solution so that it works.  To properly represent correct code in the EE database, I am changing the best answer to Gerwin Jansen's fix for the code originally offered by duncanb7.  Thanks, gsgi