Weirdness of CMD shell ('FOR')

Weirdness of CMD shell ('FOR')

Post by w.. » Wed, 23 Mar 1994 16:47:13



I encountered an weird behavior of OS/2 cmd shell's FOR command.
Whenever I get *.zip files from ftp sites, I usually reset the zip file dates
by using a batch file, but I found an interesting behavior that may or may
not be a bug in CMD.EXE.

Here is the example:
Suppose we have several x*.zip files, say x1.zip, x2.zip, x3.zip.
In a OS/2 window (i.e. OS/2 CMD.EXE shell), suppose that the following line


lists
        x1.zip
        x2.zip
        x3.zip
files in this order.

Then,

will process files
        x1.zip
        x2.zip
        x3.zip
        x1.zip
i.e., the first file that was found by 'for ... in (...)' is repeated at the
last stage.  This weird behavior is due to the fact that zip rewrites the
original zip file, and thus it affects the directory info.  But, I couldn't
figure out the hidden mechanism; why only the first file is repeated?
Why not the second, or why not the last one?

If 'for' reads dir info only once at the beginning of execution, the
repetition would not occur.  So, 'for' seems to read dir info more than
once, possibly at every iteration steps.  The original x1.zip file and the
rewritten x1.zip file by zip program should have different file info, and it
could make a confusion to 'for' command.  But, why only for the first file?

Is it a known behavior or bug of CMD shell?

Thanks

Wonkoo Kim