1.

Solve : Find newest folder?

Answer»

Hello - I am trying to write a batch file to find the newest folder under a CERTAIN directory. I then want to cd to that dir and get a list of files. I'm having trouble with the first part....finding the newest folder. Does anybody have any tips?

Thanks,
JenI can see a way to do this in a mix of batch and C++, however I'd just keep it all in C++ with system calls. The mix of batch and C++ would be to use the C++ program to read in from a text file the output of DIR>readthis.txt and compare date/time stamps on folders to find the newest. Then dynamically write the batch file to CD to that directory. Not sure if this can be done in pure batch.I will be watching this to see if anyone can do this in batch.If you filter out the directories and sort them descending, the newest one will be the first in the list. Used last modified date (default) as the sort criteria but could be changed to creation date by adding the /tc switch:

CODE: [Select]@echo off
for /f "tokens=* delims=" %%i in ('dir /ad /b /o-d ^| find /v ". "') do (
cd %%~dpni
dir
goto :eof
)

Quote from: Sidewinder on May 20, 2011, 04:26:42 PM

If you filter out the directories and sort them descending, the newest one will be the first in the list.

Code: [Select]dir /ad /b /o-d

Using /O-D, the newest one will be the last in the list. It's /OD which sorts dates from earlier to later. /O-D sorts later to earlier. Using this, you can iterate through all the directories with FOR, assigning each name in turn to a variable. After the loop (which can be a one liner) the variable will hold the name of the last in the list, which will be the newest by date. Thus the Goto is avoided.

Does the OP wish to filter out junction POINTS?

Something like the below would just get the subfolders under a folder

Code: [Select]@echo off
for /f "tokens=1-4* delims= " %%A in ( ' dir /tc /ad /od ^| find "&LT;DIR>" ^| find /v "<JUNCTION>" ' ) do if not "%%E"==".." if not "%%E"=="." set NewestFolder=%%E
Echo Newest folder is "%NewestFolder%"


Quote from: Salmon Trout on May 21, 2011, 01:41:51 AM
Using /O-D, the newest one will be the last in the list. It's /OD which sorts dates from earlier to later. /O-D sorts later to earlier. Using this, you can iterate through all the directories with FOR, assigning each name in turn to a variable. After the loop (which can be a one liner) the variable will hold the name of the last in the list, which will be the newest by date. Thus the Goto is avoided.

Does the OP wish to filter out junction points?

Something like the below would just get the subfolders under a folder

Code: [Select]@echo off
for /f "tokens=1-4* delims= " %%A in ( ' dir /tc /ad /od ^| find "<DIR>" ^| find /v "<JUNCTION>" ' ) do if not "%%E"==".." if not "%%E"=="." set NewestFolder=%%E
Echo Newest folder is "%NewestFolder%"

Must be me. But /o-d sorts descending by date with the newest folder first while /od sorts ascending by date with the newest folder at the bottom of the list. In any case, seems like a lot of code to eliminate a goto. NT batch code has a limited instruction set, might as well use them.



I see I was preempted by another post. This might be the answer to all of CH problemsQuote from: Sidewinder on May 21, 2011, 05:44:06 AM
In any case, seems like a lot of code to eliminate a goto.

It's not just "eliminating a goto". This would do that

Code: [Select]for /f "delims=" %%A in ('dir /b /ad /od') do set lastfolder=%%A
dir %%A
My code was also eliminating junction points which may or may not be required (The OP has not replied to my question - not surprising as it's very likely Billrich) and also the . and .. entries that the full DIR listing includes.


Quote from: Sidewinder on May 21, 2011, 05:44:06 AM
In any case, seems like a lot of code to eliminate a goto. NT batch code has a limited instruction set, might as well use them.

In any case, you only needed the goto because you got the sort order around the wrong way.
Quote from: Salmon Trout on May 21, 2011, 06:52:25 AM
In any case, you only needed the goto because you got the sort order around the wrong way.

I gotta stop falling into the rabbit hole. The sort order was purposely chosen to save the time of iterating the entire directory contents. Also curious why eliminate the junction points? Could be wrong, but don't they act as links to a directory, making the directory accessible by using an alias?

Seven hours till End Of Days (local time) when all this becomes meaningless. But just in case, I'll post this for prosperity or until the next End of Days is scheduled. Quote from: Sidewinder on May 21, 2011, 08:45:25 AM
The sort order was purposely chosen to save the time of iterating the entire directory contents.

OK, I'll concede that, but it's hardly going to be a big saving. Maybe on a P133 with 64 MB of RAM (min spec for Win2k) I guess.

Quote
Also curious why eliminate the junction points? Could be wrong, but don't they act as links to a directory, making the directory accessible by using an alias?

I showed elimination of junction points from the directory listing in case it was required; if not it is a simple matter to alter the code.

Quote from: Sidewinder on May 21, 2011, 08:45:25 AM
Seven hours till End Of Days

I need some new eyeglasses, I think, because I THOUGHT that 6 pm was going to be the "Rupture", and I had gathered together materials for hernia treatment for my whole family.
Quote from: Salmon Trout on May 21, 2011, 09:07:23 AM
I need some new eyeglasses, I think, because I thought that 6 pm was going to be the "Rupture", and I had gathered together materials for hernia treatment for my whole family.

Could you do a favour for those of us in a later time zone, and post a warning to us when it starts for you? I still have to get my disguise ready.I'm having my Guinness and Single Malt Scotches in vast amounts just in case...
I started a Tab since noone will be here tomorrow.ROL at what BC said to warn the later time zones that the end is happening in their zone...lol


Discussion

No Comment Found