The for command can be used to list the directory and apply (a) specific commands for each item. With /R you can do the same for a complete directory tree.
What happens when the contents of a numbered directory (tree) are changed by the command (s) in the body of the for command?
Suppose we have a D:\data directory with the following contents:
file1.txt file2.txt file3.txt
The output for %F in ("*.txt") do echo %F when executed in the specified directory explicitly displays the above list.
However, what is the result of the for loop when a command in the for body modifies the contents of a directory? For example, one of the files in the list is deleted, say file3.txt , before it is actually repeated? Or if a new file is created, for example file4.txt , before the loop ends?
How does for /R behave in this context? It is assumed that there are several subdirectories sub1 , sub2 , sub3 , each of which contains the above list of files; for /R sub2 through sub2 , sub1 already processed, but sub3 is not there yet; the contents of sub1 and sub3 change at this point (when passing through sub2 , as indicated); What will be listed then? I think sub1 content sub1 will not be recognized, but what about sub3 ?
Finally, is there a difference in the behavior of for or for /R when executed on the command line or from a batch file? Are there differences in different versions of Windows?
Note:
See also my similar question about the forfiles .
source share