DIR and howto filter to find newest file only

Posted on 2005-04-16
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

Any suggestions?

Question by:Dodger42
    LVL 10

    Expert Comment

    what os ?
    LVL 10

    Expert Comment

    ls -t *.exe | head -1

    if it's some sort of unix

    LVL 10

    Expert Comment

    if its windows, install unix tools for windows and then

    dir /o-d *.exe | head 1

    LVL 22

    Expert Comment

    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:

    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:

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

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

    gawk is equivalent to awk in virtually all respects.

    LVL 22

    Accepted Solution

    ..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}'

    LVL 4

    Expert Comment

    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
    LVL 22

    Expert Comment

    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:)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Hello I read in a discussion about a person who configured a very simple mirror RAID with two hard drives; the system and data were on the same partition. He asked how to repair the system as it was not booting up anymore. In his case running …
    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…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now