How to create a folder inside all first-level subfolders?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to create a folder inside all first-level subfolders

#16 Post by dbenham » 12 Dec 2012 02:11

I also have a hard time accepting lore without reproducible evidence.

The only mildly odd behavior I have seen is FOR /D /R will not list hidden folders, but does list un-hidden folders that are children of hidden folders.

I plan to continue to use the options until someone can give me examples demonstrating how they are unreliable.


Dave Benham

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to create a folder inside all first-level subfolders

#17 Post by dbenham » 19 Dec 2012 21:09

Liviu wrote:
The option /d /r is undocumented and somewhat buggy, it fails for root folders

My "for/d/r" notation was just shorthand for "for/d or for/r" as I hoped was clear in the context of this thread.

Besides, I wish ss64 provided some details on how "for /d /r" exactly fails for root folders.


I posted the question on SS64, Is FOR /D /R really bugged?, and Simon agrees he can't find any evidence to support the bug theory. He supposes it might have been an old pre-XP issue, but thinks he might also have simply gotten it wrong. He has updated his site to remove any reference to a FOR /R /D bug.

I'm wondering if people may be confusing a feature of FOR, FOR /R, and FOR /D with a bug. All three forms begin returning results before the command has iterated all matching files (or directories). They read x amount into a buffer, and then the iterations are read from the buffer. Once the buffer is exhausted, the command goes back to the disk to pick up where it left off. But confusing things can happen if the DO clause modifies the directory listing while the FOR is still executing. The iteration results can be modified by the DO clause.

This is very different from how FOR /F with a ('dir') command works. The FOR /F does not begin iterating until the command has completed and all results are buffered. The DO clause cannot modify the iteration results.

So the various forms of FOR have their own advantages and disadvantages:

FOR, FOR /D, FOR /R, FOR /D /R
- advantage - Can handle unicode
- disadvantage - The DO clause can modify the iteration results

FOR /F
- advantage - The DO clause cannot modify the iteration results
- disadvantage - Cannot handle unicode


Dave Benham

carlos
Expert
Posts: 503
Joined: 20 Aug 2010 13:57
Location: Chile
Contact:

Re: How to create a folder inside all first-level subfolders

#18 Post by carlos » 19 Dec 2012 22:50

Please Dave, you can post some code examples for more understand??

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: How to create a folder inside all first-level subfolders

#19 Post by Liviu » 20 Dec 2012 01:14

dbenham wrote:I posted the question on SS64, Is FOR /D /R really bugged?, and Simon agrees he can't find any evidence to support the bug theory.
Thanks for the followup. P.S. I am not oblivious to the other worthy batch related forums and sites, just running short on discretionary time to keep up.

carlos wrote:Please Dave, you can post some code examples for more understand??
Pipes and redirection kill unicode by "narrowing" it down to the current codepage. This has been long noted, and the "for /f in ('...')" is one case of that.

As for the "iterative" vs. "snapshot" behavior, the following is one example, verified under xp.sp3

Code: Select all

@echo off

>aaa.txt echo aaa
>bbb.txt echo bbb

echo. & echo :: for %%a in (*.txt)
for %%a in (*.txt) do (echo %%a & if exist *.txt del *.txt)

>aaa.txt echo aaa
>bbb.txt echo bbb

echo. & echo :: for /f %%a in ('dir /b *.txt')
for /f %%a in ('dir /b *.txt') do (echo %%a & if exist *.txt del *.txt)
which outputs

Code: Select all

:: for %a in (*.txt)
aaa.txt

:: for /f %a in ('dir /b *.txt')
aaa.txt
bbb.txt

Liviu

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: How to create a folder inside all first-level subfolders

#20 Post by dbenham » 20 Dec 2012 06:51

Liviu answered the question from Carlos.

Here is my test case for the "snapshot" vs "iterative" behavior. It demonstrates the buffered nature of the iterative FOR.

Code: Select all

@echo off
setlocal

call :test "for /f %%%%F in ('dir /b *')"
call :test "for %%%%F in (*)"
exit /b


:test
:: Create test environment
md forTest
pushd forTest
for /l %%N in (10 1 60) do copy nul %%N >nul

:: Run test
echo Begin %1 test
set "done="
%~1 do (
  echo %%F
  if not defined done (
    del /q *
    copy nul 99 >nul
    set done=1
  )
)
echo End %1 test
pause

:: Clean up
popd
rd /s /q forTest
exit /b

Results on Vista 64

Code: Select all

Begin "for /f %F in ('dir /b *')" test
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
End "for /f %F in ('dir /b *')" test
Press any key to continue . . .
Begin "for %F in (*)" test
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
99
End "for %F in (*)" test
Press any key to continue . . .


Dave Benham

Post Reply