Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Most recently updated file within a directory structure

174 views
Skip to first unread message

Jonny

unread,
Apr 14, 2005, 6:30:42 AM4/14/05
to
Hi,

I'm having problems finding the most recently updated file within a
directory structure. I have tried simply to list the files in order of
date last written, by using:

dir /S /OD /A-D /B "C:\TopDir"

where (supposedly):

/S = search subdirectories
/OD = order files by date/time
/A-D = don't list directories
/B = bare format (no heading information or summary)

but it doesn't work; it just seems to display the filenames according to
the creation date of the directories in which they reside.

Please can you help.

Thanks,
Jonny

foxidrive

unread,
Apr 14, 2005, 6:45:37 AM4/14/05
to

dir /?

/T Controls which time field displayed or used for sorting
timefield C Creation
A Last Access
W Last Written

foxidrive

unread,
Apr 14, 2005, 6:49:56 AM4/14/05
to
On Thu, 14 Apr 2005 10:30:42 GMT, Jonny wrote:

Ignore my post re /T - I didn't notice you were using /S

You can output the single most recent file in each folder to a file, and
then parse that list again and test %~tI (from for /?) for the most recent
file.

Timo Salmi

unread,
Apr 14, 2005, 8:21:32 AM4/14/05
to
Jonny <www....@ntlworld.com> wrote:
> I'm having problems finding the most recently updated file within a
> directory structure. I have tried simply to list the files in order of

No guarantees, but for some ideas you might in the meanwhile wish to
look at
RECENT.CMD CMD shell for RECENT.VBS
RECENT.VBS Search folders for recent files
in
115380 Mar 30 23:31 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi

One first gets the recent files with date/time stamps and then sorts
them.

E.g. it shows as output that the most recent four files (sorted) on
my (date part) opf my disk are right now

20050414 100007 324096 C:\_F\VPP\DAT\PAINE65.XLS
20050414 103605 327680 C:\_F\VPP\DAT\PLANO65.DOC
20050414 103606 1366 C:\_D\WORD\TIMOSUOM.DIC
20050414 112130 12548 C:\_F\CHYDE\VIESTI3.TXT

But this indeed revies on knowing VBS. Havan't looked (yet?) at
solving your exact problem with straight script. However, a one
liner DIR might be insufficient. Someone will probably solve this.
We'll see.

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland
Timo's FAQ materials at http://www.uwasa.fi/~ts/http/tsfaq.html

billious

unread,
Apr 14, 2005, 1:23:20 PM4/14/05
to

"Jonny" <www....@ntlworld.com> wrote in message
news:mDr7e.737$v82...@newsfe5-gui.ntli.net...

The short answer is that the /b option overrides the /o option.

Unfortunately, the NT date-display format depends on version (NT4, 2K or XP)
and display-date-format-setting and doesn't include seconds.

Having said that, here's a batch that might give you a start...run as LATEST
rootdirname

--- latest.bat begin --------------

[01]@echo off
[02]:: establish a tempfile nameset
[03]set ytf=t
[04]:tfnloop
[05]set ytf=%ytf%n
[06]if exist %temp%\%ytf%* goto tfnloop
[07]set ytf=%temp%\%ytf%
[08]:: establish a file of all filenames
[09]dir /s/b/a-d %1 >>%ytf%.1
[10]:: establish a file of all details
[11]for /f "tokens=*" %%i in (%ytf%.1) do dir /-c "%%i" >>%ytf%.2
[12]:: extract just the file characteristics (lines not beginning with a
space)
[13]for /f "tokens=*" %%i in ('findstr /v /b /c:" " %ytf%.2') do echo %%i
>>%ytf%.3
[14]:: set line count processed
[15]set ylc=0
[16]:: process each line of characteristics file - first 18 chars may be
significant
[17]for /f "tokens=*" %%i in (%ytf%.3) do set ydd=%%i & call :process
[18]set ydd=
[19]for /f "tokens=* skip=%yls%" %%i in (%ytf%.1) do if not defined ydd set
ydd=y&echo %%i
[20]if exist %ytf%* del %ytf%*
[21]for %%i in (ylc ydd yds yls ypd ytf) do set %%i=
[22]
[23]goto :eof
[24]
[25]:: process first few characters of YDD to CCYYMMDDHHMM
[26]:: precisely how will vary according to date format used
[27]::
[28]:: assuming mm/dd/yyyy hh:mmam format,
[29]:: * For am/pm, the A or P is at column 17 (counting from column 0)
[30]:: * We have to watch 12:xx am and 12:xx pm
[31]:: ** sequence for constant ccyymmdd is 12:xxam .. 06:xxam .. 12:xx pm
.. 06:xx pm
[32]:: ** force to 012xx, a06xx, d12xx, p06xx respectively
[33]::
[34]:process
[35]set ypd=%ydd:~17,1%%ydd:~12,2%
[36]if /i %ypd%==p12 set ypd=d12
[37]if /i %ypd%==a12 set ypd=012
[38]set ypd=%ydd:~6,4%%ydd:~0,2%%ydd:~3,2%%ypd%%ydd:~15,2%
[39]if /i "%yds%" lss "%ypd%" set yds=%ypd%&set yls=%ylc%
[40]set /a ylc+=1
[41]goto :eof

--- latest.bat end --------------

Each line begins [number]. Lines will be wrapped in transmission and need to
be rejoined. The [number] at the beginning of each line needs to be removed.

Since I use XP and dd/mm/yyyy hh:mm (24hr.) and NT dd/mm/yy hh:mm (24hr.)
formats, I haven't fully tested the above - but the theory seems sound.

HTH

...Bill


Timo Salmi

unread,
Apr 14, 2005, 3:21:54 PM4/14/05
to
Jonny <www....@ntlworld.com> wrote:
> I'm having problems finding the most recently updated file within a
> directory structure. I have tried simply to list the files in order of

Earlier today I posted a VBS hint to solve this problem. I then
predicted that it can be solved with straight script as well. This
is the how:

DRAFT

90) How do I get the most recent file within a directory structure

@echo off & setlocal enableextensions enabledelayedexpansion
::
:: Ensure that the auxiliary directory file does not alreaady exist
if exist %temp%\temp.dir del %temp%\temp.dir
::
:: Make a directory with the date/time as YYYYMMDD HH:MM
:: Assumes a DD.MM.YYYY date and a 24 time, i.e. on am/pm
for /f "tokens=*" %%f in ('dir C:\_D\ /s /b /-c /a:-d-s-h /o:d') do (
set fdate_=%%~tf
set fRevisedDate_=!fdate_:~6,4!!fdate_:~3,2!!fdate_:~0,2!
set ftime_=!fdate_:~11,2!!fdate_:~14,2!
echo !fRevisedDate_! !ftime_! %%f>>%temp%\temp.dir)
::
:: Sort the directory listing by the date/time stamp
sort %temp%\temp.dir>%temp%\tempsort.dir
::
:: Get the last line of the sorted directory listing
for /f "delims=" %%r in ('type %temp%\tempsort.dir') do set LastLine_=%%r
echo %LastLine_%
::
:: Clean up
for %%f in (temp.dir tempsort.dir) do if exist %temp%\%%f del %temp%\%%f
endlocal & goto :EOF

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland

Useful script files and tricks ftp://garbo.uwasa.fi/pc/link/tscmd.zip

Jonny

unread,
Apr 14, 2005, 4:27:54 PM4/14/05
to
I wrote:

> I'm having problems finding the most recently updated file within a
> directory structure.

Thanks to Timo and Bill for your effort.

Bill's point about the time not including seconds is a bit of a problem.

When there are several files which are created within the same minute,
then it doesn't seem to be possible to determine which one was updated
the most recently.

Interestingly, in the above scenario, Timo and Bill's solutions returned
different answers (i.e. the full path to different files), though both
are arguably valid given the lack of a seconds value.

Timo's solution also displays the date and time for the file. I found
that this didn't work in Win2K's cmd.exe because for some reason %%~tf
returns only a two-digit year in Win2K's cmd.exe. But the date and time
displayed correctly in Win XP's cmd.exe (even when running Win XP's
cmd.exe on Win2K).

Thanks again for two excellent solutions.

Regards,
Jonny

Timo Salmi

unread,
Apr 14, 2005, 4:41:30 PM4/14/05
to
Jonny <www....@ntlworld.com> wrote:
> > I'm having problems finding the most recently updated file within a
> > directory structure.
> Thanks to Timo and Bill for your effort.

> When there are several files which are created within the same minute,


> then it doesn't seem to be possible to determine which one was updated
> the most recently.

Unfortunately, one probably can't with the pure script solutions.
However, at least if one uses the VBS solution in my FAQ collection
(the option I initially pointed to), then the seconds are included.

> Timo's solution also displays the date and time for the file. I found
> that this didn't work in Win2K's cmd.exe because for some reason %%~tf
> returns only a two-digit year in Win2K's cmd.exe. But the date and time
> displayed correctly in Win XP's cmd.exe (even when running Win XP's
> cmd.exe on Win2K).

Also true. All my cmd.exe script solutions are for XP, since about
two years ago I hopped directly from Windows 95 to XP skipping all
the intermediate steps. That is the platform I assume and use both
in writing and testing my FAQ scripts.

> Thanks again for two excellent solutions.

You are most welcome. It is nice to hear that the solutions had at
least some usefulness in your case.

Mark V

unread,
Apr 14, 2005, 5:20:54 PM4/14/05
to
In alt.msdos.batch.nt Jonny wrote:

> I wrote:
>
>> I'm having problems finding the most recently updated file
>> within a directory structure.
>
> Thanks to Timo and Bill for your effort.
>
> Bill's point about the time not including seconds is a bit of a
> problem.
>
> When there are several files which are created within the same
> minute, then it doesn't seem to be possible to determine which
> one was updated the most recently.

W2K does sort them correctly using /od and /o-d (in a single
directory anyway).

Thought: Some TOUCH utilities can display files(s) times with
greater granularity. Steve P. Miller's touch.exe for one
Such as:
C:\temp2>touch /v second

Processing: C:\temp2\second

second
Modified: 4/14/2005 5:17:56.963 PM Thursday

C:\temp2>touch /v first

Processing: C:\temp2\first

first
Modified: 4/14/2005 5:17:50.254 PM Thursday

If that's any help.

Dr John Stockton

unread,
Apr 14, 2005, 3:13:45 PM4/14/05
to
JRS: In article <mDr7e.737$v82...@newsfe5-gui.ntli.net>, dated Thu, 14
Apr 2005 10:30:42, seen in news:alt.msdos.batch.nt, Jonny
<www....@ntlworld.com> posted :

>
>I'm having problems finding the most recently updated file within a
>directory structure.

If you are not afraid of using imported code and short file names, then,
starting at the root of the aforesaid structure :

HUNT * s p q e | sort /r | COLS 1- .
-> 2005/04/14-19:34:24 JS-BOXES.HTM 4466 .....a.. via .\

HUNT * s p q e | sort /r | COLS ` 58- 22+12 .
.\JS-BOXES.HTM

where in this case JS-BOXES.HTM is latest. If you can put an upper
bound on the age of the file, then you can reduce the size of the
intermediate lists; with F#-7 HUNT only finds files from after seven
days before today, with F2004-12 only since the start of December last
year, etc.

If you happen to want the latest one as at noon today, use B#12.

Be careful if there is any chance of several files having the same DOS
date-stamp - i.e. within the same 2-second interval - you'll get shown
one of them. At least some versions of 'DOS' DIR give a similar effect.

To see all files with the newest date-stamp,

HUNT * s p q0 n | COLS & 56- . | STOW envvar
HUNT * s p q e w%envvar%

HUNT, COLS, STOW (PAS EXE ZIP [TXT]) via sig line 3, except that in NT-
class systems you have to use something other than STOW to get the
output of COLS into the environment. It's a well-known trick, explained
somewhere in NOWMINUS.TXT, same place, but you probably know it already.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20; Win98. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

Timo Salmi

unread,
Apr 15, 2005, 1:24:03 AM4/15/05
to
In article <d3mfsi$j...@poiju.uwasa.fi>, Timo Salmi <t...@UWasa.Fi> wrote:
> Jonny <www....@ntlworld.com> wrote:
> > I'm having problems finding the most recently updated file within a
> > directory structure. I have tried simply to list the files in order of

> DRAFT


> 90) How do I get the most recent file within a directory structure
> @echo off & setlocal enableextensions enabledelayedexpansion

(snip)

How about getting the last five most recent files instead of the
latest only? One option is


@echo off & setlocal enableextensions enabledelayedexpansion
::
:: Ensure that the auxiliary directory file does not alreaady exist
if exist %temp%\temp.dir del %temp%\temp.dir
::
:: Make a directory with the date/time as YYYYMMDD HH:MM

:: Assumes a DD.MM.YYYY date and a 24 time, i.e. no am/pm
for /f "tokens=*" %%f in ('dir C:\_D\BAS\ /s /b /-c /a:-d-s-h /o:d') do (


set fdate_=%%~tf
set fRevisedDate_=!fdate_:~6,4!!fdate_:~3,2!!fdate_:~0,2!
set ftime_=!fdate_:~11,2!!fdate_:~14,2!
echo !fRevisedDate_! !ftime_! %%f>>%temp%\temp.dir)
::
:: Sort the directory listing by the date/time stamp

sort /r %temp%\temp.dir /o %temp%\tempsort.dir


if exist %temp%\temp.dir del %temp%\temp.dir
::

:: Get the first five lines of the sorted directory listing
for /f "delims=" %%r in ('type %temp%\tempsort.dir') do (
set lineText=%%r
set /a lineCount+=1
if !lineCount! LEQ ^5 echo !lineText!>>%temp%\temp.dir)
sort %temp%\temp.dir

billious

unread,
Apr 15, 2005, 9:42:17 AM4/15/05
to

"Jonny" <www....@ntlworld.com> wrote in message
news:mDr7e.737$v82...@newsfe5-gui.ntli.net...
[problem snipped]

Hmm - here's a solution that lists all files with the same date/time as the
earliest, given the no-seconds restriction. I avoid the use of
enabledelayedexpansion mainly because it's not available on NT4, but also
because of its cobolesque verbosity :)

--- latest2.bat begin --------------


[01]@echo off
[02]:: establish a tempfile nameset
[03]set ytf=t
[04]:tfnloop
[05]set ytf=%ytf%n
[06]if exist %temp%\%ytf%* goto tfnloop
[07]set ytf=%temp%\%ytf%
[08]:: establish a file of all filenames
[09]dir /s/b/a-d %1 >>%ytf%.1
[10]:: establish a file of all details
[11]for /f "tokens=*" %%i in (%ytf%.1) do dir /-c "%%i" >>%ytf%.2
[12]:: extract just the file characteristics (lines not beginning with a
space)
[13]for /f "tokens=*" %%i in ('findstr /v /b /c:" " %ytf%.2') do echo %%i
>>%ytf%.3
[14]:: set line count processed
[15]set ylc=0
[16]:: process each line of characteristics file - first 18 chars may be
significant
[17]for /f "tokens=*" %%i in (%ytf%.3) do set ydd=%%i & call :process

[18]for /f %%j in (%ytf%.2) do call :report %%j
[19]if exist %ytf%* del %ytf%*
[20]for %%i in (ylc ydd yds yls ypd ytf) do set %%i=
[21]
[22]goto :eof
[23]
[24]:: process first few characters of YDD to CCYYMMDDHHMM
[25]:: precisely how will vary according to date format used
[26]::
[27]:: assuming mm/dd/yyyy hh:mmam format,
[28]:: * For am/pm, the A or P is at column 17 (counting from column 0)
[29]:: * We have to watch 12:xx am and 12:xx pm
[30]:: ** sequence for constant ccyymmdd is 12:xxam .. 06:xxam .. 12:xx pm
.. 06:xx pm
[31]:: ** force to 012xx, a06xx, d12xx, p06xx respectively
[32]::
[33]:process
[34]set ypd=%ydd:~17,1%%ydd:~12,2%
[35]if /i %ypd%==p12 set ypd=d12
[36]if /i %ypd%==a12 set ypd=012
[37]set ypd=%ydd:~6,4%%ydd:~0,2%%ydd:~3,2%%ypd%%ydd:~15,2%
[38]if /i "%yds%" LSS "%ypd%" set yds=%ypd%&set yls=%ylc%&echo\>%ytf%.2
[39]if /i "%yds%" EQU "%ypd%" echo %ylc% >>%ytf%.2


[40]set /a ylc+=1
[41]goto :eof

[42]
[43]:report
[44]set ydd=
[45]if %1==0 for /f "tokens=*" %%i in (%ytf%.1) do if not defined ydd set
ydd=y&echo %%i
[46]if not %1==0 for /f "tokens=* skip=%1" %%i in (%ytf%.1) do if not

defined ydd set ydd=y&echo %%i

[47]goto :eof

--- latest2.bat end --------------

Each line begins [number]. Lines will be wrapped in transmission and need to
be rejoined. The [number] at the beginning of each line needs to be removed.

Since I use XP and dd/mm/yyyy hh:mm (24hr.) and NT dd/mm/yy hh:mm (24hr.)

formats, I haven't fully tested the above - but it did appear to select
files with date/time matching on both XP and NT4.

HTH

...Bill


Dr John Stockton

unread,
Apr 15, 2005, 8:29:00 AM4/15/05
to
JRS: In article <enA7e.1090$v82...@newsfe5-gui.ntli.net>, dated Thu,
14 Apr 2005 20:27:54, seen in news:alt.msdos.batch.nt, Jonny
<www....@ntlworld.com> posted :

>I wrote:
>
>> I'm having problems finding the most recently updated file within a
>> directory structure.

>Bill's point about the time not including seconds is a bit of a problem.


>
>When there are several files which are created within the same minute,
>then it doesn't seem to be possible to determine which one was updated
>the most recently.

Not possible with a DIR that gives HH:MM followed by SORT, obviously.

The DOS-compatible date-stamp has 2 second resolution (as my HUNT shows)
and I guess that most DIR /o:d will use that, though ISTR that early
(DOS) ones did not. It will often be adequate.

However, 2 seconds is a long time nowadays, and at least from Win98
there is a more precise stamp available using Win API - perhaps precise
enough for the immediate future, though 100 ns allows more CPU ops than
it used to ... .

Use code that does not use OS-formatted display output to be independent
of variations in the OS display; use ISO 8601 formats to be reliably
understandable everywhere. My HUNT does that.


TS : Draft #90 :
> :: Assumes a DD.MM.YYYY date and a 24 time, i.e. on am/pm

ISTM that in DD.MM.YYYY the dots are in fact ignored by your code, in
which case your code, as is, should work everywhere that FFF and ISO
8601 are not chosen for dates - worth mentioning?. And, for time above,
I suggest "24-h" and "on" -> "no".

A WinNT+ system has many files of diverse ages, and sorting N items
takes time > o(N). If it is known that the file being sought is recent,
it mat be possible to pre-process the list before sorting to remove all
elderly files from the list, in time o(N). Cf. TS's first post in this
thread.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

Timo Salmi

unread,
Apr 15, 2005, 2:53:03 PM4/15/05
to
Dr John Stockton wrote:
> TS : Draft #90 :
>> :: Assumes a DD.MM.YYYY date and a 24 time, i.e. on am/pm

> ISTM that in DD.MM.YYYY the dots are in fact ignored by your code, in
> which case your code, as is, should work everywhere that FFF and ISO
> 8601 are not chosen for dates - worth mentioning?.

Or anything else used as the separator. I wrote in that format
simply because that is the presentation at my locality. As is usual
in these types of cases, customization may be needed to localize.

> And, for time above,
> I suggest "24-h" and "on" -> "no".

Both typos, already corrected. But I might have missed them.

> A WinNT+ system has many files of diverse ages, and sorting N items
> takes time > o(N). If it is known that the file being sought is recent,
> it mat be possible to pre-process the list before sorting to remove all
> elderly files from the list, in time o(N). Cf. TS's first post in this
> thread.

It is a good suggestion for actual production runs, but in the FAQ
I'll not take it up, since it rather is a next-level issue. And not
necessarily an easy to to make effeciently.

All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance ; University of Vaasa
mailto:t...@uwasa.fi <http://www.uwasa.fi/~ts/> ; FIN-65101, Finland

Timo Salmi

unread,
Apr 15, 2005, 11:11:48 PM4/15/05
to
Jonny wrote:
> Thanks to Timo and Bill for your effort.
> When there are several files which are created within the same minute,
> then it doesn't seem to be possible to determine which one was updated
> the most recently.

As a related seconds issue you might be interested in the following
scrips included in


115380 Mar 30 23:31 ftp://garbo.uwasa.fi/pc/link/tscmd.zip
tscmd.zip Useful NT/2000/XP script tricks and tips, T.Salmi

FILEINFO.CMD CMD shell for FILEINFO.VBS 09-29-04 05:49:10
FILEINFO.VBS FileInfo Visual Basic Script 02-26-04 16:09:12

It produces output like
C:\_M>fileinfo C:\_D\TEST\CMDFAQ.CMD
+--------------------------------+
| FILEINFO.VBS File Information |
| By Prof. Timo Salmi (c) 2004 |
| Last modified Thu 26-Feb-2004 |
+--------------------------------+

C:\_D\TEST\CMDFAQ.CMD
Size: 1114
Attributes: A
Created: 03.12.2004 20:59:50 134 days ago
Last modified: 15.04.2005 23:51:34 1 days ago
Last accessed: 15.04.2005 23:51:34 1 days ago

As you see, the seconds are present.

billious

unread,
Apr 15, 2005, 11:57:55 PM4/15/05
to

"Jonny" <www....@ntlworld.com> wrote in message
news:mDr7e.737$v82...@newsfe5-gui.ntli.net...

OK, and my third try is this. No VBS, NT-compatible - but you need to adjust
for your date/time-format. Should give the one and only newest file on the
tree.

[01]@echo off
[02]:: establish a tempfile nameset
[03]set ytf=t
[04]:tfnloop
[05]set ytf=%ytf%n
[06]if exist %temp%\%ytf%* goto tfnloop
[07]set ytf=%temp%\%ytf%
[08]:: establish a file of all filenames
[09]dir /s/b/a-d %1 >>%ytf%.1

[10]:: establish a temporary directory
[11]md %ytf%.tmp
[12]:: establish a file of all details
[13]for /f "tokens=*" %%i in (%ytf%.1) do dir /-c "%%i" >>%ytf%.2
[14]:: extract just the file characteristics (lines not beginning with a
space)
[15]for /f "tokens=*" %%i in ('findstr /v /b /c:" " %ytf%.2') do echo %%i
>>%ytf%.3
[16]:: set line count processed
[17]set ylc=0
[18]:: process each line of characteristics file - first 18 chars may be
significant
[19]for /f "tokens=*" %%i in (%ytf%.3) do set ydd=%%i & call :process
[20]for /f %%j in (%ytf%.2) do call :tempcopy %%j
[21]set ydd=
[22]for /f "tokens=4 skip=5" %%j in ('dir /-c/o:-d/a-d %ytf%.tmp') do call
:report %%j
[23]if exist %ytf%* del %ytf%*
[24]rd /q /s %ytf%.tmp
[25]for %%i in (ylc ydd yds yls ypd ytf) do set %%i=
[26]
[27]goto :eof
[28]
[29]:: process first few characters of YDD to CCYYMMDDHHMM
[30]:: precisely how will vary according to date format used
[31]::
[32]:: assuming mm/dd/yyyy hh:mmam format,
[33]:: * For am/pm, the A or P is at column 17 (counting from column 0)
[34]:: * We have to watch 12:xx am and 12:xx pm
[35]:: ** sequence for constant ccyymmdd is 12:xxam .. 06:xxam .. 12:xx pm
.. 06:xx pm
[36]:: ** force to 012xx, a06xx, d12xx, p06xx respectively
[37]::
[38]:process
[39]set ypd=%ydd:~17,1%%ydd:~12,2%
[40]if /i %ypd%==p12 set ypd=d12
[41]if /i %ypd%==a12 set ypd=012
[42]set ypd=%ydd:~6,4%%ydd:~0,2%%ydd:~3,2%%ypd%%ydd:~15,2%
[43]if /i "%yds%" LSS "%ypd%" set yds=%ypd%&set yls=%ylc%&echo\>%ytf%.2
[44]if /i "%yds%" EQU "%ypd%" echo %ylc% >>%ytf%.2
[45]set /a ylc+=1
[46]goto :eof
[47]
[48]:tempcopy
[49]set ydd=
[50]if %1==0 for /f "tokens=*" %%i in (%ytf%.1) do if not defined ydd set
ydd=y&copy "%%i" %ytf%.tmp\%1 1>nul
[51]if not %1==0 for /f "tokens=* skip=%1" %%i in (%ytf%.1) do if not
defined ydd set ydd=y&copy "%%i" %ytf%.tmp\%1 1>nul
[52]goto :eof
[53]
[54]:report
[55]if defined ydd goto :eof
[56]if %1==0 for /f "tokens=*" %%i in (%ytf%.1) do if not defined ydd set
ydd=y&echo %%i
[57]if not %1==0 for /f "tokens=* skip=%1" %%i in (%ytf%.1) do if not

defined ydd set ydd=y&echo %%i

[58]goto :eof

Each line begins [number]. Lines will be wrapped in transmission and need to
be rejoined. The [number] at the beginning of each line needs to be removed.

...Bill


billious

unread,
Apr 17, 2005, 2:18:55 AM4/17/05
to

"Jonny" <www....@ntlworld.com> wrote in message
news:mDr7e.737$v82...@newsfe5-gui.ntli.net...
> [snipped for the snippy people]

Here's a date-format-independent version. I'll not say it's fast, but it
appears to work on NT and XP.

-- dilatest.bat starts ---

[01]@echo off
[02]:: save our current dir
[03]pushd .
[04]:: establish a tempfile nameset
[05]set ytf=t
[06]:tfnloop
[07]set ytf=%ytf%n
[08]if exist %temp%\%ytf%* goto tfnloop
[09]set ytf=%temp%\%ytf%


[10]:: establish a temporary directory
[11]md %ytf%.tmp

[12]:: establish a file of all dirnames prefixed by a dummy non-blank line
[13]echo dummy>%ytf%.d
[14]echo dummy>%ytf%.1
[15]for /f "tokens=2*" %%d in ('dir %1^|find ":\"') do echo %%e>>%ytf%.d
[16]dir /s/b/ad %1 >>%ytf%.d
[17]set ylc=0
[18]for /f "tokens=* skip=1" %%d in (%ytf%.d) do cd "%%d" &call :procdir
[19]:: find youngest-of-all
[20]set ydd=
[21]set yfn=
[22]for /f "tokens=*" %%i in ('dir %ytf%.tmp /b/a-d/od') do set ylc=%%i
[23]for /f "tokens=* skip=%ylc%" %%i in (%ytf%.d) do if not defined ydd set
ydd=y&for /f "tokens=* skip=%ylc%" %%j in (%ytf%.1) do if not defined yfn
set yfn=y&echo %%i\%%j
[24]:: restore our start dir
[25]popd
[26]rd /s /q %ytf%.tmp
[27]del %ytf%*
[28]for %%i in (ydd ytf ylc yfn) do set %%i=
[29]goto :eof
[30]
[31]:: process the current directory
[32]:procdir
[33]set /a ylc+=1
[34]:: copy the youngest file to the temporary directory
[35]set ydd=
[36]dir /b/a-d/o-d >%ytf%.f 2>nul
[37]for /f "tokens=*" %%i in (%ytf%.f) do if not defined ydd set ydd=y&copy
"%%i" %ytf%.tmp\%ylc% >nul& echo %%i>>%ytf%.1
[38]if not defined ydd echo empty>>%ytf%.1
[39]goto :eof

-- dilatest.bat ends ---


Each line begins [number]. Lines will be wrapped in transmission and need to
be rejoined. The [number] at the beginning of each line needs to be removed.

A couple of odd points I observed:

1) "DIR /o:" sequence - which controls which file is reported as "newest"

ON NT, the sequence returned by dir /o(-)d appears to be random-order of
files with the same date & time (times have a 2-sec. granularity)

On XP, the sequence seemed to be in ACTUAL order of file-creation, but
perhaps this is caused by the position within the directory, so it may not
be reliable.

2) Behaviour of the "PUSHD/POPD" commands.

Since I use them rarely, I haven't intensively investigated them, nor do I
propose to do so.

Given that I have deliberatly kept to a default setup as far as possible, so
the "Extended features" setup on my XP machine has CMD /E set to ON (both
machine and user setting in the registry = 1) and this is the default, I was
surprised to find that

PUSHD
many CDs
POPD

Did NOT restore the original directory, whereas

PUSHD .
many CDs
POPD

did (note the "." argument to PUSHD)

I even explicitly set /E:on in the properties of the DOS shortcut, to no
beneficial effect (as I had expected...)

I'm sure that it USED to work, since I've used it before, or is this another
breakage brought on by the infamous XPSP2?


HTH

...Bill


Dr John Stockton

unread,
Apr 17, 2005, 2:19:17 PM4/17/05
to
JRS: In article <enA7e.1090$v82...@newsfe5-gui.ntli.net>, dated Thu,
14 Apr 2005 20:27:54, seen in news:alt.msdos.batch.nt, Jonny
<www....@ntlworld.com> posted :
>
>When there are several files which are created within the same minute,
>then it doesn't seem to be possible to determine which one was updated
>the most recently.

My program FILGEN - use FILFEN32.EXE (FILFEN.ZIP, via sig line 3) for
this - has a vague resemblance to DIR, but can output any available
information in any order and format within some approximation to reason;
in particular, times can be shown to the millisecond.

I could, I suppose, add a date-time format of, say, daycount to about
eight decimals, or seconds to three decimals.

--

0 new messages