Find newest file in all subdirectories
Moderator: DosItHelp
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Find newest file in all subdirectories
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.
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
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.
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find newest file in all subdirectories
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
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:
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
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.
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find newest file in all subdirectories
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.
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find newest file in all subdirectories
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
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.
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
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
True, i've not thought about that (because my own system has the date format YYYY.MM.DD).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.
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
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.
Re: Find newest file in all subdirectories
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.
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?
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?
Re: Find newest file in all subdirectories
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
%%~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
So how about this JScript (batch/JScript) named "getNewest":
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".
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);
}
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.
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find newest file in all subdirectories
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
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.
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.
-
- Posts: 184
- Joined: 21 Feb 2013 15:54
Re: Find newest file in all subdirectories
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?