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

Nested For loop (For /F calling another For)

313 views
Skip to first unread message

silly

unread,
Sep 23, 2003, 8:22:30 AM9/23/03
to
Could someone please let me know why the second attempt below doesnt work: -
Thanks...

::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~
:: custom directory listing without doing a parse of the output of 'dir/s/a
C:\'
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~
:: This works....
::~~~~~~~~~~
FOR %%I IN (*) DO @echo %%~tI "%%I" [%%~zI] >> $fred.txt

::rearrange date field..

FOR /F "tokens=1,2,3,4* delims=/ " %%i IN ($fred.txt) DO @echo %%k-%%j-%%i
%%l %%m >> $elma.txt
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:: This doesn't....
::~~~~~~~~~~
:: attempt to nest the two for loops

FOR /F "usebackq tokens=1,2,3,4* delims=/ " %%i IN (`FOR %%F IN (*) DO
@echo %%~tF "%%F" [%%~zF]`) DO @echo %%k-%

%j-%%i %%l %%m >> $elma2.txt

:: gives error : "The system cannot find the file `FOR %F IN (*."
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Ted Davis

unread,
Sep 23, 2003, 8:45:52 AM9/23/03
to

There may be many reasons, but the most obvious is that all the
variables have to be evaluated at the same time, and that is
impossible.

The best way to do this is to put the second FOR in a subroutine

for %%a in (foo) do call :pass2 %%a
goto :EOF
:pass2
for %%b in (%1) do bar

T.E.D. (tda...@gearbox.maem.umr.edu)
SPAM filter: Messages to this address *must* contain "T.E.D."
somewhere in the body or they will be automatically rejected.

Ritchie

unread,
Sep 23, 2003, 8:48:12 AM9/23/03
to
"silly" <dont_...@me.com> wrote in message news:3f70...@news.greennet.net...

> Could someone please let me know why the second attempt below doesnt work: -
> FOR /F "usebackq tokens=1,2,3,4* delims=/ " %%i IN (`FOR %%F IN (*) DO

You need to escape the brackets, like:-

...=1,2,3,4* delims=/ " %%i IN (`FOR %%F IN ^(*^) DO...

Anyway, I think this will give you the output you're after:-

@echo off & setlocal ENABLEEXTENSIONS
for %%a in (*) do (
for /f "tokens=1-4* delims=/ " %%i in ('echo/%%~ta "%%a" [%%~za]') do (
echo/%%k-%%j-%%i %%l %%m
)
)

--
Ritchie, undo for mail

Ritchie

unread,
Sep 23, 2003, 9:04:48 AM9/23/03
to
"Ted Davis" <tda...@gearbox.maem.umr.edu> wrote in message news:1uf0nvk30j93b04oi...@4ax.com...

> There may be many reasons, but the most obvious is that all the

The brackets just need escaping:-

@echo off & setlocal ENABLEEXTENSIONS

for /f "delims=" %%a in ('for %%b in ^(*^) do @echo/%%b') do echo/%%a

silly

unread,
Sep 26, 2003, 8:11:58 AM9/26/03
to
Thanks for those swift replies.
I may need to resort to C++ though as it takes ages for my 10gb disk to be
scanned.


Ted Davis

unread,
Sep 26, 2003, 8:01:41 PM9/26/03
to

There may well be an easy way to generate the report you want with a
report generation language, but to do that you are going to have to
describe the task, not code that doesn't do what you want. It's a
question of being able to give you what you want instead of what you
asked for.


T.E.D. (tda...@gearbox.maem.umr.edu - e-mail must contain "T.E.D." or my .sig in the body)

silly

unread,
Sep 27, 2003, 8:37:59 PM9/27/03
to
>"Ted Davis" <tda...@gearbox.maem.umr.edu> wrote in message
news:tlk9nvsbcl9m7efbg...@4ax.com...

fair point!

Im looking for 2 things:

A) a way of generating output like dir/s/b except with with date, time and
size included so i can sort on them.
I would generate listings for entire disks.

B) a bonus would be a way of identifying subtrees within that listing which
are the same.


Ted Davis

unread,
Sep 27, 2003, 10:49:51 PM9/27/03
to

I have written on this topic before, but I can't find it. Well, maybe
I can reconstruct it - this will assume a US English, default install
of an NT series OS, that is that the DIR listing is in this form:

Volume in drive E has no label.
Volume Serial Number is EC73-855E

Directory of e:\myfiles

08/28/2003 07:08 PM 143 1not2.tx2
08/28/2003 07:08 PM 190 2not1.txt
09/01/2003 01:04 PM 264 cat.comics.txt
........
07/13/2003 11:41 AM 3,868 WS_FTP.LOG
25 File(s) 1,296,795 bytes
3 Dir(s) 7,128,535,040 bytes free

Since you want to sort on date, it is necessary to reconfigure the
date stamp into yyyymmdd (with or without delimiters, but with leading
zeros), and since you want to sort on size as well it is expedient to
remove any commas and right justify them in a fixed size field. Since
the length of the filespec is unknowable and its starting column
depends on the size of the largest file, an assumption must be made
about the largest possible file - I will assume ten digits.

Another point is that to sort on time, the time stamp will have to be
converted to 24 hour format - I'll omit the colon so that date, time
and size are pure numbers, rather than punctuated strings. There is a
potential issue with leading zeros: in some situations, numbers with
leading zeros might be interpreted as octal rather than decimal
numbers.

In addition to the above assumptions, it will also be assumed that the
filename begins in column 40, that the directory name begins in column
15 on the line beginning " Directory of", and that any other line not
containing a / is to be ignored.

By now you are probably beginning to realize that done right, this is
a non-trivial task.

The following batch file and script generates a report in this format:
20031405 0814 91 e:\myfiles\awktest.tawk
20030403 2404 18 e:\myfiles\awktest.tawk.bak
20032103 1121 0 e:\myfiles\bar
20030202 2102 1459 e:\myfiles\bar.txt.0
......
20032028 1820 214 e:\myfiles\x2
20033311 1933 70 e:\myfiles\foo\infile
20033911 1939 74 e:\myfiles\foo\foo\infile
20033111 1931 15228 e:\myfiles\index_files\checking.jpg
20033111 1931 18415 e:\myfiles\index_files\snowball.jpg

DIR_FORMAT.AWK

BEGIN{
FS = "[ /:]+"
Directory = ""
Fname = ""
}

{
if( $2$3 ~ /Directoryof/ ) {
Directory = substr( $0, 15 )
sub( /\\$/, "", Directory )
}
if( $0 ~ /\// ) {
Fname = substr( $0, 40 )
yyyy = $3
mm = $1
dd = $2
hh = $4
mm = $5
if( $6 ~ /PM/ ) hh += 12
Fsize = $7
gsub( /,/, "", Fsize )
printf("%04d%02d%02d %02d%02d %10d %s\\%s\n", yyyy, mm, dd,
hh, mm, Fsize, Directory, Fname)
}
}

DIR_FORMAT.CMD

@echo off
dir %1 /a-d /s | gawk -fdir_format.awk > target.txt


Real lines are indented at least two spaces - anything against the
margin wrapped from the line above.

That batch file generated a report file on 16734 files in about 10
seconds.

You can get a suitable version of gawk from
<http://sourceforge.net/project/showfiles.php?group_id=23617> - it may
want a library DLL which is on the same page. There are a lot of
goodies on that page - all are Win32 ports of popular (free) GNU Unix
utilities. Get the -bin and -doc files, you don't need the -src
(source code) file.

>
>B) a bonus would be a way of identifying subtrees within that listing which
>are the same.

The above represents at least $40 worth of code if you had to pay for
it (probably really much more than that, I wasn't keeping close watch
on the clock). The B) question looks like it will take a lot more
time, and I'm fresh out of free time for tonight. I'll leave that as
an exercise for the reader. Hint: "subtree" has to be closely defined
to keep the complexity and running time within reason.

Phil Robyn

unread,
Sep 27, 2003, 11:19:25 PM9/27/03
to
silly wrote:

If you download FORFILES.EXE (free from Microsoft), you can do something like
the following:

- - - - - - - - - - begin screen capture - - - - - - - - - -
<Win2000> c:\cmd>forfiles -pc:\cmd -s -mx*.cmd -c"CMD /C echo @FDATE @FTIME @FSIZE @PATH\@FILE"
20010801 180708 512 c:\cmd\XTRLINES.CMD
20020630 204942 216 c:\cmd\DEMO\xmydata.cmd
20020620 182704 298 c:\cmd\DEMO\xdate.cmd
20010914 160726 583 c:\cmd\DEMO\xdaysold.cmd
20010926 64326 11901 c:\cmd\DEMO\xarray.cmd
20021127 44138 793 c:\cmd\DEMO\xh.cmd
20020826 194200 2125 c:\cmd\DEMO\xmonthcal.cmd
20011207 53040 197 c:\cmd\DEMO\xasql.cmd
20011219 213852 118 c:\cmd\DEMO\xforfile.cmd
20020119 181940 491 c:\cmd\DEMO\xemailmsg.cmd
20030605 82004 462 c:\cmd\DEMO\XSeries.cmd
20030717 71020 450 c:\cmd\DEMO\xnames.cmd
20000725 55940 322 c:\cmd\TEST\xdatemmm.cmd
20000728 165102 420 c:\cmd\TEST\xunixdir.cmd
20000824 61036 159 c:\cmd\TEST\xmyparms.cmd
20000924 54010 779 c:\cmd\TEST\xmerge01.cmd
20000927 231810 265 c:\cmd\TEST\xlength.cmd
20001129 211310 450 c:\cmd\TEST\xyymm01.cmd
20001129 233358 1480 c:\cmd\TEST\xyymm02.cmd
20010506 63610 3088 c:\cmd\TEST\xxxupper.cmd
20010922 32318 595 c:\cmd\TEST\xbignums.cmd
19990724 51706 693 c:\cmd\TEST\xtract.cmd
19990724 71006 171 c:\cmd\TEST\xtract2.cmd
20010218 31008 337 c:\cmd\TEST\xemailarm.cmd
20010218 35720 507 c:\cmd\TEST\xemailarm1.cmd
20010226 233212 558 c:\cmd\TEST\xcombine.cmd
20010226 233514 589 c:\cmd\TEST\xcombine1.cmd
20010301 82644 377 c:\cmd\TEST\xplrcln1.cmd
20010303 71416 848 c:\cmd\TEST\xcombine2.cmd
20010312 215556 918 c:\cmd\TEST\xcombine3.cmd
20010919 44910 489 c:\cmd\TEST\xcombine4.cmd
20000207 11230 1024 c:\cmd\TEST\XSUBSTR.CMD
20000415 50942 223 c:\cmd\TEST\xdoright.cmd
20010419 44858 2742 c:\cmd\TEST\xfbanway.cmd
20010417 223830 3307 c:\cmd\TEST\xbydate.cmd
20020918 50750 734 c:\cmd\TEST\xhelp.cmd
20010921 53222 1619 c:\cmd\TEST\XIMPORT.CMD
20010424 233720 222 c:\cmd\TEST\xwyldirs.cmd
20010503 174936 830 c:\cmd\TEST\xuplow.cmd
20000610 70448 1857 c:\cmd\TEST\xplit001.cmd
20000614 54612 17 c:\cmd\TEST\xcd.cmd
20010830 233854 552 c:\cmd\TEST\xctmjobstats.cmd
20011212 74534 2852 c:\cmd\TEST\XSTRING.CMD
20011022 74344 862 c:\cmd\TEST\xc.cmd
20011022 72556 644 c:\cmd\TEST\xcmulti.cmd
20011112 73440 646 c:\cmd\TEST\xrecent.cmd
20011120 231818 221 c:\cmd\TEST\xbycolumns.cmd
20020213 214108 1264 c:\cmd\TEST\xCOA.cmd
20010708 74624 926 c:\cmd\TEST\xdirtree.cmd
20010714 65712 532 c:\cmd\TEST\xchange.cmd
20020925 306 2241 c:\cmd\TEST\xmailtoODBCUsers.cmd
20011024 180722 177 c:\cmd\UTIL\XLENGTH.CMD
20011024 180728 1480 c:\cmd\UTIL\XVERIFY.CMD
20011024 180736 1100 c:\cmd\UTIL\XFDATE.CMD
20020816 173744 582 c:\cmd\UTIL\xnow.cmd
- - - - - - - - - - end screen capture - - - - - - - - - -

Since the date is already in yyyymmdd format, it is easy to sort
the output. . . . FORFILES.EXE can be downloaded from
ftp://ftp.microsoft.com/reskit/y2kfix/x86/

--
Phil Robyn
Univ. of California, Berkeley

u n z i p m y a d d r e s s t o s e n d e - m a i l

0 new messages