DO statement reversed sort order seems not to work

May 23, 2010
36
1
Seattle
I store backups of critical files in a given directory. Each backup produces a unique subdirectory of that directory.

For a potential restore, I want to find the most recent file with a given name in that directory. It makes sense to use a DO loop on that filename, with the output sorted descending by date, so that I can process the first file found and then quit the loop. However, the list of files found is always in ascending order.

This holds true if I sort instead by name or size or ...

What am I missing here?

Here's some sample code:
Code:
del d:\tmp\list1.txt d:\tmp\list2.txt >& NUL

  :: Find all files named MeConfig.db.  Sort the output list ASCENDING by date.
  do file in /d"D:\croot.backup" /s /o:d  "MeConfig.db"
    echo file is %_cwd\%file %@filedate[%file,c] >> d:\tmp\list1.txt
  enddo

  :: Find all files named MeConfig.db.  Sort the output list DESCENDING by date.
  do file in /d"D:\croot.backup" /s /o:-d "MeConfig.db"
    echo file is %_cwd\%file %@filedate[%file,c] >> d:\tmp\list2.txt
  enddo

  :: Compare the two lists that were created.  (Invoke your favorite compare utility.)  They are the same.
  diff d:\tmp\list1.txt d:\tmp\list2.txt
 
May 29, 2008
43
0
I think it's because the "do file ... /o:" defaults to file write time but you're using the file create time in the echo.
 
May 20, 2008
11,529
102
Syracuse, NY, USA
It's OK here; the files in each directory are sorted separately.

Code:
v:\> do f in /d"v:\" /s /o:d *.log  ( echo %@full[%f] )
V:\w32tm.log
V:\killoobe.log
V:\tailtest.log
V:\cpulog.log
V:\nocursor.log
V:\killers.log
V:\hold\tilog.log
V:\hold\timonitor.log

v:\> do f in /d"v:\" /s /o:-d *.log  ( echo %@full[%f] )
V:\killers.log
V:\nocursor.log
V:\cpulog.log
V:\tailtest.log
V:\killoobe.log
V:\w32tm.log
V:\hold\timonitor.log
V:\hold\tilog.log
 
May 23, 2010
36
1
Seattle
Sorry, Dennis, for my coding error. But I just tried changing my test code (both to %@filedate[%file,w] and to %@filedate[%file]) and got the same inexplicable result.
 
May 20, 2008
11,529
102
Syracuse, NY, USA
Off-topic ... but, it's possible to redirect all of DO's output at once (and it might save a very tiny amount of time)

Code:
v:\> type dotest.btm
>z:\file1 do f in /d"v:\" /s /o:d *.log
    echo %@full[%f] %@filedate[%f]
enddo

>z:\file2 do f in /d"v:\" /s /o:-d *.log
    echo %@full[%f] %@filedate[%f]
enddo

echo file1
type z:\file1
echo.
echo file2
type z:\file2

Code:
v:\> dotest.btm
file1
V:\w32tm.log 2021-03-14
V:\killoobe.log 2021-10-10
V:\tailtest.log 2021-10-11
V:\cpulog.log 2021-10-13
V:\nocursor.log 2021-10-14
V:\killers.log 2021-10-26
V:\hold\tilog.log 2019-07-16
V:\hold\timonitor.log 2019-08-16

file2
V:\killers.log 2021-10-26
V:\nocursor.log 2021-10-14
V:\cpulog.log 2021-10-13
V:\tailtest.log 2021-10-11
V:\killoobe.log 2021-10-10
V:\w32tm.log 2021-03-14
V:\hold\timonitor.log 2019-08-16
V:\hold\tilog.log 2019-07-16
 
May 23, 2010
36
1
Seattle
Vince:
the files in each directory are sorted separately
Then it's not really a sort by date, is it? Especially in my case, where there is at most one file in each directory with the same name. So that explains my problem.

I consider that a bug. Probably too much work to fix.

Thanks for the redirection tip. That is entirely new syntax to me.
 
Apr 2, 2011
1,461
10
54
North Carolina, USA
I store backups of critical files in a given directory. Each backup produces a unique subdirectory of that directory.

[omitted]

Great to see you post Brett. Glad you got a profile name to work or did ??? get your old one back? Was going to post that I think I know what editor you use - then looked your signature! Lol....
 
May 20, 2008
11,529
102
Syracuse, NY, USA
And if you wanted the whole list to be sorted by date, you can also pipe DO's output (you CAN'T combine that with redirection). But you can ...

Code:
v:\> type dotest.btm
do f in /d"v:\" /s /o:d *.log | sort /o z:\file1
    echo %@filedate[%f] %@filetime[%f] %@full[%f]
enddo

do f in /d"v:\" /s /o:d *.log | sort /r /o z:\file2
    echo %@filedate[%f] %@filetime[%f] %@full[%f]
enddo

type z:\file1
echo.
type z:\file2

Code:
v:\> dotest.btm

2019-07-16 13:41 V:\hold\tilog.log
2019-08-16 16:26 V:\hold\timonitor.log
2021-03-14 14:01 V:\w32tm.log
2021-10-10 21:31 V:\killoobe.log
2021-10-11 16:07 V:\tailtest.log
2021-10-13 22:08 V:\cpulog.log
2021-10-14 14:41 V:\nocursor.log
2021-10-26 17:22 V:\killers.log

2021-10-26 17:22 V:\killers.log
2021-10-14 14:41 V:\nocursor.log
2021-10-13 22:08 V:\cpulog.log
2021-10-11 16:07 V:\tailtest.log
2021-10-10 21:31 V:\killoobe.log
2021-03-14 14:01 V:\w32tm.log
2019-08-16 16:26 V:\hold\timonitor.log
2019-07-16 13:41 V:\hold\tilog.log
 
May 23, 2010
36
1
Seattle
| sort /o z:\file1
Just what I was looking for to eliminate a second workfile. Thanks again.
 
May 20, 2008
11,529
102
Syracuse, NY, USA
Actually, you could make it a one-liner. [I haven't tried it but you could probably do the sort | tee | sort thing with DO as well.] PDIR is nice for picking out the info you want in a desired order.

Code:
v:\> pdir /s /(@filedate[*] @filetime[*,,s] @full[*]) v:\*.log | sort | tee z:\file1 | sort /r /o z:\file2

v:\> type z:\file1
2019-07-16 13:41:10 V:\hold\tilog.log
2019-08-16 16:26:44 V:\hold\timonitor.log
2021-03-14 14:01:15 V:\w32tm.log
2021-10-10 21:31:30 V:\killoobe.log
2021-10-11 16:07:03 V:\tailtest.log
2021-10-13 22:08:41 V:\cpulog.log
2021-10-14 14:41:44 V:\nocursor.log
2021-10-26 17:22:15 V:\killers.log

v:\> type z:\file2
2021-10-26 17:22:15 V:\killers.log
2021-10-14 14:41:44 V:\nocursor.log
2021-10-13 22:08:41 V:\cpulog.log
2021-10-11 16:07:03 V:\tailtest.log
2021-10-10 21:31:30 V:\killoobe.log
2021-03-14 14:01:15 V:\w32tm.log
2019-08-16 16:26:44 V:\hold\timonitor.log
2019-07-16 13:41:10 V:\hold\tilog.log
 

Similar threads