Solved

Script to create subdirectories

Posted on 2013-01-22
4
381 Views
Last Modified: 2013-01-22
Hi experts,
I use about  1000 directories for my documents etc. but now I want to create sub-directories in each one. Thanks for the help

I have :

client1
client2
client3
...

And i want :

client1
-subA
----sub1
-------subK
-------subL
----sub2
-------subM
-------subN
----sub3
-------subO
-------subP
-subB
----sub1
-------subW
-------subX
----sub2
-------subY
-------subZ

client2
-subA
----sub1
-------subK
-------subL
----sub2
-------subM
-------subN
----sub3
-------subO
-------subP
-subB
----sub1
-------subW
-------subX
----sub2
-------subY
-------subZ

..........
0
Comment
Question by:P6FER
  • 2
4 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 500 total points
ID: 38805562
Create a template folder in which you create the sub folder structure you want to have per client, for example under "C:\Template":
-subA
----sub1
-------subK
-------subL
----sub2
...

Then use the script below to copy this empty folder structure into all the client folders. It's currently in test mode and will only display the robocopy commands it would normally run. Remove the uppercase "ECHO" in front of robocopy to run it for real.
Try it with a test client structure first.
@echo off
setlocal
set Template=C:\Template
set Target=D:\Clients
for /d %%a in ("%Target%\*.*") do (
	echo Processing '%%~nxa' ...
	ECHO robocopy.exe "%Template%" "%%~a" /e /r:0 /w:0
)
echo Done.

Open in new window

robocopy.exe is a default part of Windows since Vista/W2k8; if you're still on W2k3, it's in the W2k3 Resource Kit Tools:
http://www.microsoft.com/downloads/details.aspx?FamilyID=9D467A69-57FF-4AE7-96EE-B18C4790CFFD&displaylang=en
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 38805619
Here's a reasonable approach.  Change the BaseDir to the directory that contains all the client folders.  Then run.

@echo off
setlocal

set BaseDir=c:\temp
set NewDirs="subA\sub1\subK",^
            "subA\sub1\subL",^
            "subA\sub2\subM",^
            "subA\sub2\subN",^
            "subA\sub3\subO",^
            "subA\sub3\subP",^
            "subB\sub1\subW",^
            "subB\sub1\subX",^
            "subB\sub2\subY",^
            "subB\sub2\subZ"

for /D %%D in ("%BaseDir%\*.*") do (
  for %%N in (%NewDirs%) do (
    md "%%~D\%%~N"
  )
)

Open in new window

~bp
0
 

Author Comment

by:P6FER
ID: 38805626
Just perfect obda. :-)
You just have to change :
for /d %%a in ("%Target%\*.*") do (

Open in new window

to :
for /d %%a in ("%Target%*.*") do (

Open in new window

Thank you
0
 
LVL 84

Expert Comment

by:oBdA
ID: 38805667
Only if you're setting Target to a mask including a part of the actual client folder name, like "D:\Clients\Client*.*"; Target was supposed to point to the root folder containing the client1, client2, ... folders (guess I should have mentioned that).
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question