Page 1 of 2
Find newest file in all subdirectories
Posted: 27 Dec 2013 16:03
by pditty8811
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.
Re: Find newest file in all subdirectories
Posted: 27 Dec 2013 16:30
by Squashman
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.
Re: Find newest file in all subdirectories
Posted: 27 Dec 2013 16:48
by pditty8811
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.
Re: Find newest file in all subdirectories
Posted: 27 Dec 2013 18:20
by penpen
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
Re: Find newest file in all subdirectories
Posted: 27 Dec 2013 18:44
by pditty8811
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.
Re: Find newest file in all subdirectories
Posted: 27 Dec 2013 18:46
by pditty8811
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
Re: Find newest file in all subdirectories
Posted: 27 Dec 2013 19:10
by foxidrive
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.
Re: Find newest file in all subdirectories
Posted: 28 Dec 2013 00:22
by berserker
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
Re: Find newest file in all subdirectories
Posted: 28 Dec 2013 07:37
by penpen
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
Re: Find newest file in all subdirectories
Posted: 28 Dec 2013 09:12
by foxidrive
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.txtThe other thing is the AM PM indicators - will they sort properly, and with leading spaces in early AM times?
Re: Find newest file in all subdirectories
Posted: 28 Dec 2013 10:15
by penpen
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
Re: Find newest file in all subdirectories
Posted: 28 Dec 2013 11:33
by penpen
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".
Re: Find newest file in all subdirectories
Posted: 28 Dec 2013 13:56
by pditty8811
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.
Re: Find newest file in all subdirectories
Posted: 28 Dec 2013 15:18
by penpen
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.
Re: Find newest file in all subdirectories
Posted: 28 Dec 2013 17:05
by pditty8811
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?