Find newest file in all subdirectories

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Find newest file in all subdirectories

#1 Post by pditty8811 » 27 Dec 2013 16:03

Find newest file in all subdirectories?

I know this question has been asked before. Could someone post a vbs script or batch that does this. If you do, please tell me how many parameters it takes and what kind. So I know how to execute it.

Thanks.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Find newest file in all subdirectories

#2 Post by Squashman » 27 Dec 2013 16:30

Just two nested For Loops. First one gets the directory name. Second one gets the newest file in the directory. Look at the help for the DIR command and use that with the FOR loops.

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Find newest file in all subdirectories

#3 Post by pditty8811 » 27 Dec 2013 16:48

Squashman wrote:Just two nested For Loops. First one gets the directory name. Second one gets the newest file in the directory. Look at the help for the DIR command and use that with the FOR loops.


Perhaps you didn't understand me. I'm looking for the newest file in all subdirectories. That is, one file total.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Find newest file in all subdirectories

#4 Post by penpen » 27 Dec 2013 18:20

If last write access defines what you have called the newest file, then this cmd(.exe) shell one-liner should
echo you the newest file in the actual directory and all of its subdirectories:

Code: Select all

@(for /F "tokens=* delims=" %F in ('dir /B /S') do @(echo %~tF#%~dpnxF))|sort /R|findstr /N "^"| for /F "tokens=1* delims=#" %a in ('findstr "^1:"') do @echo found newest file: %b

penpen

Edit: This works only for the date format YYYY.MM.DD (i have set it up on my system, and haven't thought about that).
A batch solution with the date format MM.DD.YYYY can be found below in my next post.
Edit2: Even the alternate batch solution is not reliable, better use the batch/JScript solution here: http://www.dostips.com/forum/viewtopic.php?p=31030#p31030
Last edited by penpen on 28 Dec 2013 11:36, edited 2 times in total.

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Find newest file in all subdirectories

#5 Post by pditty8811 » 27 Dec 2013 18:44

penpen wrote:If last write access defines what you have called the newest file, then this cmd(.exe) shell one-liner should
echo you the newest file in the actual directory and all of its subdirectories:

Code: Select all

@(for /F "tokens=* delims=" %F in ('dir /B /S') do @(echo %~tF#%~dpnxF))|sort /R|findstr /N "^"| for /F "tokens=1* delims=#" %a in ('findstr "^1:"') do @echo found newest file: %b

penpen


Yes I'm sorry, the latest modified file.

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Find newest file in all subdirectories

#6 Post by pditty8811 » 27 Dec 2013 18:46

pditty8811 wrote:
penpen wrote:If last write access defines what you have called the newest file, then this cmd(.exe) shell one-liner should
echo you the newest file in the actual directory and all of its subdirectories:

Code: Select all

@(for /F "tokens=* delims=" %F in ('dir /B /S') do @(echo %~tF#%~dpnxF))|sort /R|findstr /N "^"| for /F "tokens=1* delims=#" %a in ('findstr "^1:"') do @echo found newest file: %b

penpen


Yes I'm sorry, the latest modified file.


I can't get this to work. Windows xp

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find newest file in all subdirectories

#7 Post by foxidrive » 27 Dec 2013 19:10

It's not a batch file, but it returns a file from 2011 for me.

A problem is the format of date time field in the for command. It's not the same on every system, and doesn't sort properly in my system.

berserker
Posts: 95
Joined: 18 Dec 2013 00:51

Re: Find newest file in all subdirectories

#8 Post by berserker » 28 Dec 2013 00:22

pditty8811 wrote:Find newest file in all subdirectories?

I know this question has been asked before. Could someone post a vbs script or batch that does this. If you do, please tell me how many parameters it takes and what kind. So I know how to execute it.

Thanks.

can you install external tools? if yes, you might want to try GNU win32 find

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Find newest file in all subdirectories

#9 Post by penpen » 28 Dec 2013 07:37

foxidrive wrote:A problem is the format of date time field in the for command. It's not the same on every system, and doesn't sort properly in my system.
True, i've not thought about that (because my own system has the date format YYYY.MM.DD).

Here is a batch solution with the us date format MM.DD.YYYY:

Code: Select all

@echo off
@(
   for /F "tokens=* delims=" %%F in ('dir /B /S') do @(
      echo %%~tF#%%~dpnxF
   )
)|@(
   for /F "tokens=1-3* delims=,.-/: " %%d in ('findstr "^"') do @(
::      MM:=%%d, DD:=%%e, YYYY:=%%f rest_of_the_line:=%%g
      echo %%f.%%d.%%e %%g
   )
)|@(
   sort /R
)|@(
   findstr /N "^"
)|@(
   for /F "tokens=1* delims=#" %%a in ('findstr "^1:"') do @(
      echo found newest file: %%b
   )
)
goto :eof
If another date format is used the second for loop has to be costumized.

penpen

Edit: Not reliable, better use the batch/JScript solution here: http://www.dostips.com/forum/viewtopic.php?p=31030#p31030
Last edited by penpen on 28 Dec 2013 11:36, edited 1 time in total.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Find newest file in all subdirectories

#10 Post by foxidrive » 28 Dec 2013 09:12

It isn't reliable here penpen.

I tried it in a couple of folders and it returned some correct files (adjusted for DD/MM/YYYY setting)
and then it returned a file from 2011 but the name was corrupted too.

Code: Select all

12/01/2011  09:50               623 Donation to Premier’s Disaster Relief Appeal.txt


It returned this:

found newest file: c Premier's Disaster Relief Appeal.txt

The other thing is the AM PM indicators - will they sort properly, and with leading spaces in early AM times?

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Find newest file in all subdirectories

#11 Post by penpen » 28 Dec 2013 10:15

You are right, although i never had seen something like that:
%%~tF (within the "for /F %%F ..." loop) will not work and produce nothing on a file named "623 Donation to Premier’s Disaster Relief Appeal.txt" (curious thing).

The am vs pm should be no problem as %%~tF is creating 24 hours formated time (as far as i know).

But it may be a problem that on one of my WinXp's (the home version) it has no seconds and milliseconds, so it may produce wrong results.

Later i will try a jScript solution.

penpen

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Find newest file in all subdirectories

#12 Post by penpen » 28 Dec 2013 11:33

So how about this JScript (batch/JScript) named "getNewest":

Code: Select all

@if (true==false) /*
@echo off

cscript //nologo //e:JScript "%~f0" %*

goto :eof
*/
@end

function getNewerFile (file1, file2) {
   return (file1 == null) ? file2 :
      (file2 == null) ? file1 :
      (file1.dateLastModified < file2.dateLastModified) ? file2 :
      file1;
}

function getNewestFile (folder) {
   var newestFile = null;

   for (var subDirs = new Enumerator (folder.SubFolders); !subDirs.atEnd (); subDirs.moveNext ()) {
      newestFile = getNewerFile (newestFile, getNewestFile (subDirs.item ()));
   }

   for (var files = new Enumerator (folder.files); !files.atEnd (); files.moveNext ()) {
      newestFile = getNewerFile (newestFile, files.item ());
   }

   return newestFile;
}

var dir = (WScript.Arguments.Unnamed.Length == 1) ? WScript.Arguments.Unnamed.Item (0) : ".";
var fso = new ActiveXObject ("Scripting.FileSystemObject");
var newestFile = getNewestFile (fso.GetFolder ((fso.FolderExists (dir)) ? dir : "."));

if (newestFile != null) {
   WScript.Echo ("" + newestFile.Path);
   WScript.Quit (0);
} else {
   WScript.Quit (1);
}
It returns the newest file in the current directory and all subdirectories, if no path is specified,
else it returns the newest file in the given path (dir and subdirs).
If no file can be found then 1 is returned, else 0.

penpen

Edit: Replaced [dir] with [(fso.FolderExists (dir)) ? dir : "."] within the [var newestFile = ...] line, to use the actual path, if the specified path is not existing (/contains wrong characters).

Edit: Found and corrected a bug: inserted line "(file2 == null) ? file1 :" in function "getNewerFile".
Last edited by penpen on 28 Dec 2013 15:22, edited 1 time in total.

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Find newest file in all subdirectories

#13 Post by pditty8811 » 28 Dec 2013 13:56

penpen wrote:So how about this JScript (batch/JScript) named "getNewest":


This is telling me dateLastModified is null or not an object. Must be OS specific. I'm using xp.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Find newest file in all subdirectories

#14 Post by penpen » 28 Dec 2013 15:18

I'm using WinXP (home and prof, both on actual patch level), too, and it works fine there using NTFS, or FAT32,
so i think there must be another issue.

penpen

Edit: I've found a bug and corrected it, please retry with the changed code above; i haven't tested with empty subdirectories.

pditty8811
Posts: 184
Joined: 21 Feb 2013 15:54

Re: Find newest file in all subdirectories

#15 Post by pditty8811 » 28 Dec 2013 17:05

penpen wrote:I'm using WinXP (home and prof, both on actual patch level), too, and it works fine there using NTFS, or FAT32,
so i think there must be another issue.

penpen

Edit: I've found a bug and corrected it, please retry with the changed code above; i haven't tested with empty subdirectories.


Perhaps I'm implementing it wrong. I just copy and paste it in a .bat file and then launch it. It just opens and closes. Where do I put the pause command?

Post Reply