Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by gsgi
at 2024-07-18 16:04:32
Point:500 Replies:11 POST_ID:829024USER_ID:11929
Topic:
Microsoft Operating Systems;;
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
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
Author: gsgi replied at 2024-07-28 02:16:01
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
Expert: Modalot replied at 2024-07-23 04:09:49
Question re-opened per Asker's request in http://www.experts-exchange.com/R_32473.html to allow for different closure.
Modalot
Community Support Moderator
Modalot
Community Support Moderator
Expert: Gerwin Jansen replied at 2024-07-19 13:53:52
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 offSETLOCAL EnableDelayedExpansiondir /A:-D /T:C /O:D |find ":">junk.txtset temporary=""for /f "tokens=1 delims=" %%a in ('type junk.txt') do set newest=!temporary! && set temporary=%%aif not "%newest%"=="" echo Latest created file is "%newest%"del junk.txt 1:2:3:4:5:6:7:
Author: gsgi replied at 2024-07-19 13:13:28
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
@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
Expert: Gerwin Jansen replied at 2024-07-19 13:02:52
See my comment above, all needed corrections are there. I tested and it worked.
Assisted Solution
Author: gsgi replied at 2024-07-19 13:00:26
I didn't because I could read his logic; I assume his .bat file is fixable. Is there something about his bat file that would not be fixable? I do not have the tail command. I try to solve for solutions using native commands when possible.
If we put junk.txt somewhere else does it work? His .bat file makes a junk.txt file which becomes the latest file. Here I put junk.txt in c:
@echo off
dir /A:-D /T:C /O:D |find ":"> c:junk.txt
for /f "tokens=1 delims=" %%a in ('type c:junk.txt') do set newest=%%a
if not "%newest%"=="" echo Latest created file is "%newest%"
del junk.txt
The other way to fix this is to have it cycle through all but the last file in the listing.
-gsgi
If we put junk.txt somewhere else does it work? His .bat file makes a junk.txt file which becomes the latest file. Here I put junk.txt in c:
@echo off
dir /A:-D /T:C /O:D |find ":"> c:junk.txt
for /f "tokens=1 delims=" %%a in ('type c:junk.txt') do set newest=%%a
if not "%newest%"=="" echo Latest created file is "%newest%"
del junk.txt
The other way to fix this is to have it cycle through all but the last file in the listing.
-gsgi
Accepted Solution
Expert: Gerwin Jansen replied at 2024-07-19 12:51:13
300 points EXCELLENT
If you want to use the first comment as a solution, you need to change the following to make it work:
@echo offdir /A:-D /T:C /O:D |find ":">%TEMP%junk.txtfor /f "tokens=1 delims=" %%a in ('type %TEMP%junk.txt') do set newest=%%aif not "%newest%"=="" echo Latest created file is "%newest%"del %TEMP%junk.txt 1:2:3:4:5:
The temporary file junk.txt is now moved to %TEMP% - so no target for the script above. If you move the script above to %TEMP% as well and run it from there it is working.
So:
C:>%TEMP%findfile.cmd
would give you:
Latest created file is "02/13/2012 10:47 AM 131 echo.bat"
(assuming that your %TEMP% is not equal to C:)
Expert: Gerwin Jansen replied at 2024-07-19 12:46:47
@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.
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.
Author: gsgi replied at 2024-07-19 11:45:20
Thanks Everyone!!!!
Expert: Gerwin Jansen replied at 2024-07-19 05:06:33
Do you have the tail command available? If so you can do this:
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/
Assisted Solution
Expert: duncanb7 replied at 2024-07-18 17:37:18
200 points EXCELLENT
Please take a look at the following code for DOS command batch file and for reference only ,and it is NOT fully tested
Duncan
Duncan
@echo offdir /A:-D /T:C /O:D |find ":">junk.txtfor /f "tokens=1 delims=" %%a in ('type junk.txt') do set newest=%%aif not "%newest%"=="" echo Latest created file is "%newest%"del junk.txt 1:2:3:4:5: