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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
machone
Posts: 31
Joined: 01 Oct 2012 18:36

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

#1 Post by machone » 09 Dec 2012 19:25

I've got a directory structure with a main folder and several levels of subfolders. I need to be able to create a same-named folder (i.e. \temp\) in every subfolder (1 level down) but not in any sub-subfolders (2+ levels down). I've been trying different approaches with no luck so far.. Does anyone have a quick solution for this?

Basically, I want to create

\Main\SubFolder\temp\

inside every 'SubFolder', but I do not want it to create

\Main\SubFolder\Sub-SubFolder\temp\

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

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

#2 Post by Squashman » 09 Dec 2012 19:36

Code: Select all

@echo off
pushd c:\main
FOR /F "delims=" %%G in ('dir /ad /b') do mkdir "%%G\temp"
popd

machone
Posts: 31
Joined: 01 Oct 2012 18:36

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

#3 Post by machone » 09 Dec 2012 19:46

Thanks! I had come up with this, which seems to work so far:

@echo off
for /d %%a in ( ".\*" ) do (
md "%%a\temp" 2>nul
)

Do you see any potential issues with the way I arrived at the solution?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#4 Post by foxidrive » 09 Dec 2012 20:31

I would use Squashman's solution myself.

The /D and /R options of FOR do have bugs, and a FOR /F can always be used instead.

machone
Posts: 31
Joined: 01 Oct 2012 18:36

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

#5 Post by machone » 10 Dec 2012 08:35

Ok, thanks!

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

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

#6 Post by Aacini » 11 Dec 2012 05:55

foxidrive wrote:
The /D and /R options of FOR do have bugs, and a FOR /F can always be used instead.

Excuse me. I always suggest to avoid using FOR /F that execute another command (like DIR) if the base FOR (not /F) is enough; this is because the former is slower. However, if /D and /R options do have bugs, then this is not a good idea.

Could you describe the bugs and show a small example that reproduce they? Thanks!

Antonio

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#7 Post by foxidrive » 11 Dec 2012 07:29

Aacini wrote:
foxidrive wrote:
The /D and /R options of FOR do have bugs, and a FOR /F can always be used instead.


Could you describe the bugs and show a small example that reproduce they? Thanks!

Antonio


They involved path names with spaces/characters in some fashion, when specifying a path in the FOR command. I don't have an example - it was discovered some years ago and discussed in alt.msdos.batch.nt but I don't have a link to the discussion.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

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

#8 Post by Ed Dyreen » 11 Dec 2012 09:39

Hi foxi,
foxidrive wrote:They involved path names with spaces/characters in some fashion, when specifying a path in the FOR command. I don't have an example - it was discovered some years ago and discussed in alt.msdos.batch.nt but I don't have a link to the discussion.
I use For /D /R all the time, but a statement which can't be proven is not worth mentioning.

Hope you can recall it, thanks,
ed

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#9 Post by foxidrive » 11 Dec 2012 14:36

Ed Dyreen wrote:Hi foxi,
foxidrive wrote:They involved path names with spaces/characters in some fashion, when specifying a path in the FOR command. I don't have an example - it was discovered some years ago and discussed in alt.msdos.batch.nt but I don't have a link to the discussion.
I use For /D /R all the time, but a statement which can't be proven is not worth mentioning.


Find the discussion and you will find the proof you desire.

All I need to know is that I tested it at the time and verified the failure, so I avoid using /R with a specified path.

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

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

#10 Post by Liviu » 11 Dec 2012 15:54

Aacini wrote:
foxidrive wrote:The /D and /R options of FOR do have bugs, and a FOR /F can always be used instead.
Could you describe the bugs and show a small example that reproduce they?

I asked the same question a few times, and the only answers I got boiled down to "there have been issues reported elsewhere, don't remember the details, but trust us that it's well known that for/d/r loops are unreliable". Almost like a self-referential myth ;-)

I've run my own research on the topic, summarized at https://groups.google.com/group/alt.msdos.batch.nt/msg/a801b8a1904942cd?hl=en. In brief, there are a couple of corner cases which IMHO are not bugs, and one reported bug (in a rather arcane scenario where the body of the loop modifies the directory tree that for/r is operating on) which I was not able to duplicate.

Liviu

P.S. The continuing recommendation to, for example, use "for /f in ('dir /ad')" instead of the builtin "for /d" is one reason behind the widespread misconception that batch language cannot handle unicode pathnames.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

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

#11 Post by foxidrive » 11 Dec 2012 16:18

For /D doesn't have the issue as it cannot have a path specified. It was FOR /R with a specified path.

Maybe it is related to the short filename bug, as it doesn't affect every instance of it.

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

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

#12 Post by Liviu » 11 Dec 2012 19:55

foxidrive wrote:For /D doesn't have the issue as it cannot have a path specified. It was FOR /R with a specified path.

Yet a few posts above you advised against "for /d" and recommended "for /f in ('dir /ad /b')" instead.

To be clear, I am not saying that you are wrong about possible for/d/r issues, in fact I'd be very interested to find out what those are. But so far I have yet to see any tangible proof thereof.

foxidrive wrote:Maybe it is related to the short filename bug, as it doesn't affect every instance of it.

There is a longstanding SFN bug, indeed, partially but not fully fixed as of Win7 (http://groups.google.com/group/alt.msdos.batch.nt/browse_frm/thread/afb023e1792881b5/85379bd32ede38b6?hl=en#85379bd32ede38b6). However, that's irrelevant in most cases, including the one here, and doesn't warrant a blanket for/d/r censure.

Liviu

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

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

#13 Post by Squashman » 11 Dec 2012 20:43

What I find amusing is that Foxidrive and Liviu both posted in that thread.

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

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

#14 Post by carlos » 11 Dec 2012 21:53

In http://ss64.com/nt/for_d.html says:
The option /d /r is undocumented and somewhat buggy, it fails for root folders

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

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

#15 Post by Liviu » 11 Dec 2012 23:17

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.

Post Reply