Finding newest file in all subdirectories

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

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

Re: Finding newest file in all subdirectories

#16 Post by foxidrive » 24 Apr 2012 17:56

That's good work, Dave.

I tried the three solutions on my program files folder for time taken and here are the results:

Solution A - Dave's Batch solution - 50 seconds
Solution B - Foxi's VBS solution - 1 minute 30 seconds
Solution C - Dave's WMIC solution - 5 minutes 10 seconds

Solution A points to note: it is region dependent so needs to be edited if you use a different date/time format
Solution B points to note: it is not region dependent, it is not as quick as batch.
Solution C points to note: it is not region dependent, it has better time precision, it loses the capitalisation of the path\filename, it's slower by far for large folders.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Finding newest file in all subdirectories

#17 Post by dbenham » 24 Apr 2012 19:19

Extending Foxidrive's summary:

Solution A has only minute precision. Not normally good enough in my opinion. Too bad, because it is definitely the fastest.

Solution B has second precision. Often times good enough.

Solution C - not much more to say other than what foxidrive already said.


Dave Benham

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Finding newest file in all subdirectories

#18 Post by dbenham » 24 Apr 2012 19:52

FORFILES is an alternate pure batch option for getting the last modified date and time to the second, but it is probably not worth using because it is SLOW.

Dave Benham

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

Re: Finding newest file in all subdirectories

#19 Post by foxidrive » 25 Apr 2012 07:08

Powershell is another alternative. If I can I'll look into a solution using that.

smccaffr39
Posts: 7
Joined: 14 Apr 2011 06:40

Re: Finding newest file in all subdirectories

#20 Post by smccaffr39 » 30 Apr 2012 10:20

thanks Dave!

brinda
Posts: 78
Joined: 25 Apr 2012 23:51

Re: Finding newest file in all subdirectories

#21 Post by brinda » 18 Jul 2013 22:24

dave and foxidrive ,

thanks for giving this script and method. was searching for a long time(especially the seconds precision by foxidrive).

saw this example in prof. timo wscript.echo eval where he use %* for all

replaced

Code: Select all

echo strPath = "%~1"
echo The most recent file in the "%~1" tree is


with

Code: Select all

echo strPath = "%*"
echo The most recent file in the "%*" tree is


example of old and new. no need to put "<directory> "

Code: Select all

D:\Documents\Downloads>i "d:\test files"
The most recent file in the "d:\test files" tree is
"D:\test files\a New Text Document.txt"

after replace %*

D:\Documents\Downloads>i d:\test files
The most recent file in the "d:\test files" tree is
"D:\test files\a New Text Document.txt"





now we(in my place) Could

1. build the entire flat database of files(with seconds precision),
2. than sort,
3. use antonio's findrepl or dave's repl to remove the first 16 character of each line
4. gzipped for small size
5. use gzip function to read the zip files without unzip and pipe out for search in win2000.


something like a mini sqlite in batch :D

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

Re: Finding newest file in all subdirectories

#22 Post by foxidrive » 19 Jul 2013 03:04

1 to 4 seem reasonable.

As for 5 - I haven't looked but does gzip uncompress the file (either to a temp file or streamed to STDOUT) for every search that you would perform?

That could be cpu intensive and be inefficient, but it might work. Depending on the size of the file and if you have several searches to perform, than extracting the archive to a file and searching the file would likely be faster.

brinda
Posts: 78
Joined: 25 Apr 2012 23:51

Re: Finding newest file in all subdirectories

#23 Post by brinda » 19 Jul 2013 06:18

foxidrive,

could not see any temp file but checking it is using as you have mentioned stdout

Total files amount to 194124 files(old archieve). Around 25 to 30MB. compress gzip -9 brings the file to 1.94MB size. Huge space saving for ease of transport.

computer used is win2000 pentium 3 1Ghz with 500MB memory. I have not noticed the lag till you have mentioned and on trial run the CPU goes to 100 for a few seconds only. This search gives around 150 files output on grab.txt

this is the command using. %%f is the serial number

Code: Select all

gzip -c -d data.gz | findstr /c:"%%f" >> grab.txt


gzip help file

Code: Select all

gzip 1.2.4 (18 Aug 93)
 -c --stdout      write on standard output, keep original files unchanged
 -d --decompress  decompress


was trying to use sqlite but not so good in that. Saw dostips forum have so many good scripts + alt.msdos.nt. Found gzip features while looking for an alternative. There was one script bash script for linux in where they use grep to get the text files compressed into usable form. That is the code which has the -c and -d. tried that with findstr and it works :D so made a mini database.

It more faster to grab files from network since only the known location are looked at. Than again it suffers on time sort till your helpful :D scripts is seen. thanks again.

funzip has the feature but never used before

Code: Select all

D:\Documents\Downloads>funzip
fUnZip (filter UnZip), version 3.95 of 20 January 2009
usage: ... | funzip [-password] | ...
       ... | funzip [-password] > outfile
       funzip [-password] infile.zip > outfile
       funzip [-password] infile.gz > outfile
Extracts to stdout the gzip file or first zip entry of stdin or the given file.


link
ftp://ftp.info-zip.org/pub/infozip/win32/

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

Re: Finding newest file in all subdirectories

#24 Post by foxidrive » 19 Jul 2013 06:47

I wasn't sure if you'd managed to assemble a script but it seems that you have. :)

I think unzip can also extract to STDOUT - I have this version here.

Code: Select all

Info-ZIP             17 February 2002 (v5.5)                    2

UNZIP(1L)                                               UNZIP(1L)

       -c     extract files  to  stdout/screen  (``CRT'').   This
              option  is similar to the -p option except that the
              name of each file is printed as  it  is  extracted,
              the  -a option is allowed, and ASCII-EBCDIC conver-
              sion is  automatically  performed  if  appropriate.
              This  option  is  not  listed  in  the  unzip usage
              screen.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Finding newest file in all subdirectories

#25 Post by Samir » 19 Jul 2013 15:56

I think my approach to this would be to use dir's ability to sort files and then pull off the top/bottom one as that would be the most recent. And then nest this in a for loop to hit all the directories and pipe the result to another file.

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

Re: Finding newest file in all subdirectories

#26 Post by foxidrive » 19 Jul 2013 18:30

That only gives you 'minutes' granularity when comparing all the files using %%~tx which may or may not be sufficient, depending on the task.

rodrigolima
Posts: 21
Joined: 19 Jul 2013 11:35
Location: Brazil

Re: Finding newest file in all subdirectories

#27 Post by rodrigolima » 22 Jul 2013 07:04

Hello smccaffr39

I saw your post and I got the solution...

It Follows:

@echo off
cls

:: Author Rodrigo Sant Anna Lima
:: July 22th 2013
:: Sao Jose dos Campos, Brazil
:: Version 1.0.0

title Get the newest file

::Clear the environment
if exist listOfFiles.txt del listOfFiles.txt
if exist listOfFilesClear.txt del listOfFilesClear.txt
if exist TheNewestFile.dat del TheNewestFile.dat

::Make suplly file to search the newest file
dir /S /A:-D /O:D > listOfFiles.txt

::Do not read lines that begins with space
for /F "tokens=* delims= eol= " %%i in (listOfFiles.txt) do (
if not "%%d"=="listOfFiles.txt" echo %%i >> listOfFilesClear.txt
)

sort listOfFilesClear.txt > clear.txt

del listOfFilesClear.txt
del listOfFiles.txt

ren clear.txt listOfFilesClear.txt
ren listOfFilesClear.txt listOfFiles.txt

:: Remove line reference aboout listOfFiles.txt
findstr /V "listOfFiles.txt" listOfFiles.txt > Aux1.txt

:: Remove line reference about Reader.bat
findstr /V "Reader.bat" Aux1.txt > Aux2.txt

del listOfFiles.txt
ren Aux2.txt listOfFiles.txt

del Aux1.txt

::Get the newest file - last line
for /f "tokens=1-4" %%a in (listOfFiles.txt) do (
set newestFile=%%d
)


:: My windows is set up to Brazilian Portuguese
:: So, I wrote Pasta instead of FOLDER in English

:: Get the newest file path
for /f "tokens=1-3" %%d in ('dir /s %newestFile% ^| find "Pasta"') do (
set fileLoc=%%f
)

:: Show The newest file and its path
cls
echo.
echo The newest file is: %newestFile%
echo.
echo.
echo Stored at:
echo.
echo. %fileLoc%
echo.

pause

echo FILENAME: %newestFile% > TheNewestFile.dat
echo FILEPATH: %fileLoc% >> TheNewestFile.dat

del listOfFiles.txt

goto:eof


In the end it works successfully.
The Batch generates a file called TheNewestFile.dat.It contains the the newest File name and Path.

Try it and return me.

Thank you.

rodrigolima
Posts: 21
Joined: 19 Jul 2013 11:35
Location: Brazil

Re: Finding newest file in all subdirectories

#28 Post by rodrigolima » 23 Jul 2013 09:25

Hi everyone

I check my solution and I found a problem with sort.

For Example Raw data
23/07/2013
31/03/2010

The correct sort must be

Sorted data(Expected)
31/03/2010
23/07/2013

But the answer is:
Real Sort
23/07/2013
31/03/2010

I applied sort /+10 data

At this moment, I am going to test other mode to sort it correctly.

See you

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

Re: Finding newest file in all subdirectories

#29 Post by foxidrive » 23 Jul 2013 09:28

parsing the date into yyyymmdd will sort it easily. Add hhmmss too.

rodrigolima
Posts: 21
Joined: 19 Jul 2013 11:35
Location: Brazil

Re: Finding newest file in all subdirectories

#30 Post by rodrigolima » 24 Jul 2013 10:35

Hello foxidrive

Thank you so much for your post.
Now, I am trying to create a parse that converts date format "ddmmyyyy" to format "yyyymmdd".

I am testing this parse with FOR /F.

The concept is:

Create for/f loop, with 4 tokens ( date hour length Filename )

I got the token number 1 and extract day,month and year using %var:~Start,length% (like substring)

After extract, I will create the new line like this:

%year%%month%%day% %hour% %length% %filename% > archive

Tomorrow morning I will post the result if my test is sucessfull or not ...

See you

Post Reply