Script to Create Sub Folders

I have a client (a school) and they have had some charming kids who have managed to run scripts that create folders, with hundreds of nested sub folders. so they cant be migrated or deleted because the file path is too long.

Im trying to replicate the problem on a test server to formulate a plan, on how to deleted them. My question is how are they doing this?

They are obviously using MD in a loop of some description, but my scripting knowledge is limited.
LVL 58
Pete LongTechnical ConsultantAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Travis MartinezStorage EngineerCommented:
I can't say as to how they are being created but you can delete and/or copy them with robocopy and the /MIR option.  Robocopy will mirror the file/folder directory structure even if the name is too long.  You can also use the mirror option to delete the folders; you just have to be careful what sub folder you start with.

Say you have a folder C:\Temp\temp that is empty with the target nested directory tree being however many levels deep.  Pick a level and use robocopy c:\temp\temp \\server\root_folder\Sub_folder /MIR /R:3 and it will remove everything from the sub folder down.  Be careful because it will not prompt you for confirmation with the mirror option it will delete everything.
Pete LongTechnical ConsultantAuthor Commented:
Hi Travis,

Yeah I've done a bit of reading on the subject I've got about 5 or six approaches to take, (Robocopy being one). I just don't want the first time I do this to be on the clients network. I'd like to replicate it on the test bed and make sure whatever solution I go with, I've tested it,before they are paying a premium for my time.

I just need the command(s)/Script to recreate what they are doing.
Regards,
Pete
Travis MartinezStorage EngineerCommented:
Gotcha,

This can be recreated with PowerShell using the "new-item" in a while loop.  I'll get the syntax down here in a min...
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Travis MartinezStorage EngineerCommented:
I'm admitting defeat.  I can't get the nesting variables to work right.

You could test with any path though but I understand wanting to know how they are doing it.

Apologies...
Pete LongTechnical ConsultantAuthor Commented:
Anyone?
NVITEnd-user supportCommented:
Here's a solution using robocopy:

md c:\test\empty_dir
robocopy c:\test\empty_dir c:\test\the_dir_to_delete /s /mir
rd c:\test\empty_dir
rd c:\test\the_dir_to_delete

Open in new window

Travis MartinezStorage EngineerCommented:
Okay, I think I was making it too hard.  You're looking for a script on how to do it, correct?  I tested this one on a virtual box machine and it worked as expected.  It made 5 directories deep of "temp", c:\temp\temp\temp\temp\temp.

Open PowerShell as Administrator

Change directories to whatever base folder or drive you'd like

$a = 1
Do
 {
"starting loop $a"
$a++
mkdir temp
cd temp
} while ($a -le 5)

Change the 5 to how many directories deep you want to create
Travis MartinezStorage EngineerCommented:
Well I just tried 300 times and powershell through an error about length being more than 248 characters.  I'll try with creat-item...
NVITEnd-user supportCommented:
BTW, my post at ID: 42454464 just deletes the problematic folders, it doesn't create them.
Travis MartinezStorage EngineerCommented:
$a = 1
Do
 {
"starting loop $a"
$a++
new-item -ItemType Directory -Name temp -Force
cd temp
} while ($a -le 49)

Most I can get is 49...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Travis MartinezStorage EngineerCommented:
And the final piece to the puzzle.  I shared the 49th "temp" directory and mapped it to the same system as a network drive then ran the same script again and it created 49 more.  At that level, I shared the folder again and ran the script, another 49.  On the root drive of c:\temp there were 49 X 3 temp directories.

This is the only way I can think they are doing it.
NVITEnd-user supportCommented:
I shared the 49th "temp" directory and mapped it to the same system as a network drive
Here's a way similar to that:
subst g: z:\network\folder1\folder2\folder3\folder4\folder5

Open in new window

Travis MartinezStorage EngineerCommented:
and this copied and pasted into powershell, not running as administrator, will create 147 directories named temp.  Just keep the loop going and adding the drive path.  Thanks NVIT!

cd c:\

$b = 1
Do
 {
"starting loop $b"
$b++
new-item -ItemType Directory -Name temp -Force
cd temp
} while ($b -le 49)

$dir = pwd

subst g: $dir
cd g:\

$c = 1
Do
 {
"starting loop $c"
$c++
new-item -ItemType Directory -Name temp -Force
cd temp
} while ($c -le 49)

$dir = pwd
subst h: $dir
cd h:\

$d = 1
Do
 {
"starting loop $d"
$d++
new-item -ItemType Directory -Name temp -Force
cd temp
} while ($d -le 49)
Pete LongTechnical ConsultantAuthor Commented:
Cheers Guys I will give these a test, your help is most appreciated!!

Pete
Pete LongTechnical ConsultantAuthor Commented:
Thanks again
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.