?
Solved

Script to create subdirectories

Posted on 2013-01-22
4
Medium Priority
?
395 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 85

Accepted Solution

by:
oBdA earned 2000 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 59

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 85

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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

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