?
Solved

DIR and howto filter to find newest file only

Posted on 2005-04-16
8
Medium Priority
?
418 Views
Last Modified: 2012-05-05
Hey all,

I need to perform a dir style command on a directory, which would return only the newest file in the directory.

I need to be able to search based on wildcards also, so using dir and piping the results to another command may be possible (awk maybe?).

directory example:
test.exe 3/3/04
file.exe 2/10/04

dir *.exe | somefilter
results:
file.exe

Any suggestions?

0
Comment
Question by:Dodger42
  • 3
  • 3
7 Comments
 
LVL 10

Expert Comment

by:neteducation
ID: 13797388
what os ?
0
 
LVL 10

Expert Comment

by:neteducation
ID: 13797413
ls -t *.exe | head -1

if it's some sort of unix

0
 
LVL 10

Expert Comment

by:neteducation
ID: 13797455
if its windows, install unix tools for windows and then

dir /o-d *.exe | head 1

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:pjedmond
ID: 13808771
I'm guessing that this is a windows system due to the *.exe extensions.

You could of course go down the unix-tools, uwin, cygwin suites:

http://www.interopsystems.com/tools/
http://www.research.att.com/sw/tools/uwin/
http://www.cygwin.com/

route which installs all the tools required for this and more!.....or we can be a little more specific:)...

Lets just use the gawk (gnu awk as you seem to like that) tools package for windows:

http://gnuwin32.sourceforge.net/packages/gawk.htm

and then you can do:

dir /TC

Which sorts according to creation date.

Then pipe it through gawk:

dir /TC | gawk 'NR==3 {print $0}'

Which prints the whole line for the 3rd line in the directory listing (because the 1st and 2nd lines are . and ..)

http://www.student.northpark.edu/pemente/awk/awk1line.txt

will give you an idea of the 'classic' 1 line filters.

gawk is equivalent to awk in virtually all respects.

HTH:)
0
 
LVL 22

Accepted Solution

by:
pjedmond earned 750 total points
ID: 13808843
..and for the wildcards, theycan be as part of the dir command for say *.exe files:

dir /TC *.exe

or part of the gawk command for lines containing a t.

gawk '/t/ {print $0}'

HTH:)
0
 
LVL 4

Expert Comment

by:riotz
ID: 13811381
you pipe in NT based systems with |

so to list some files with a special date use the following example

dir *.exe | findstr 28.01.2005
0
 
LVL 22

Expert Comment

by:pjedmond
ID: 14058729
Hmm - being slightly biased!, I'd say points to me:) I'd say I've provided the fully comprehensive links to unixtools, uwin, and cygwin, but I've also provided the simplest 'minimalistic' solution using the suggested awk approach....and provided ample expansion on the link, not only for wildcards as suggested, but also, direction on the 'classic' oneliners that gives awk the power that it has:)
0

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

Many people tend to confuse the function of a virus with the one of adware, this misunderstanding of the basic of what each software is and how it operates causes users and organizations to take the wrong security measures that would protect them ag…
As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Suggested Courses

755 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