Advertisement

There's no such thing as a stupid question, but they're the easiest to answer.
Login
Search

Advertisement

DOS/Other DOS/Other
Search Search
Search for:
Tech Support Guy > > >

Finding first file name in dir


(!)

bkolb's Avatar
bkolb bkolb is offline
Junior Member with 2 posts.
THREAD STARTER
 
Join Date: Mar 2010
Experience: Advanced
11-Mar-2010, 04:13 PM #1
Question Finding first file name in dir
I have a directory with several files in it that look alike except for the date that's part of the filename. Like this:

text_file_2010-03-01.txt
text_file_2010-02-28.txt
text_file_2010-03-02.txt
text_file_2010-02-27.txt
text_file_2010-02-24.txt
text_file_2010-02-02.txt
text_file_2010-02-21.txt
text_file_2010-02-08.txt

In effect, I want to sort these in decreasing order and return the top filename. So they'd be sorted like this:

text_file_2010-03-02.txt
text_file_2010-03-01.txt
text_file_2010-02-28.txt
text_file_2010-02-27.txt
text_file_2010-02-24.txt
text_file_2010-02-21.txt
text_file_2010-02-08.txt
text_file_2010-02-02.txt

and only the file name "text_file_2010-03-02.txt" would be returned, which I would then use for other processing.

any ideas?
bkolb's Avatar
bkolb bkolb is offline
Junior Member with 2 posts.
THREAD STARTER
 
Join Date: Mar 2010
Experience: Advanced
11-Mar-2010, 04:15 PM #2
Forgot to mention, I know how to sort them so the file I want is on top (dir file_name_*.txt /O:-N) but how do I pull only that first name off and return it?
Squashman's Avatar
Trusted Advisor with 19,633 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
11-Mar-2010, 04:27 PM #3
Couple of ways to do it.
Output the list to a temp file then redirect the file to a variable.
dir file_name_*.txt /a-d /b /O-N>temp.txt
set /p _FirstFile=<temp.txt
del temp.txt

You could also put it into a for loop and then kill the for loop immediately with a Goto.
__________________
.
Squashman's Avatar
Trusted Advisor with 19,633 posts.
 
Join Date: Apr 2003
Location: 1265 Lombardi Ave
11-Mar-2010, 06:41 PM #4
Or as TheOutCaste pointed out, you can do a normal alphabetical list and the last file will get assigned in a loop.
Code:
For /F "Tokens=*" %%I In ('dir file_name_*.txt /a-d /b /ON') do Set _FName=%%I
As Seen On

BBC, Reader's Digest, PC Magazine, Today Show, Money Magazine
WELCOME TO TECH SUPPORT GUY!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.


(clock)
THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


WELCOME
You Are Using: Server ID
Trusted Website Back to the Top ↑