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?
 
Travis MartinezSmoke JumperCommented:
$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...
0
 
Travis MartinezSmoke JumperCommented:
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.
0
 
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
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Travis MartinezSmoke JumperCommented:
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...
0
 
Travis MartinezSmoke JumperCommented:
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...
0
 
Pete LongTechnical ConsultantAuthor Commented:
Anyone?
0
 
NVITCommented:
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

0
 
Travis MartinezSmoke JumperCommented:
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
0
 
Travis MartinezSmoke JumperCommented:
Well I just tried 300 times and powershell through an error about length being more than 248 characters.  I'll try with creat-item...
0
 
NVITCommented:
BTW, my post at ID: 42454464 just deletes the problematic folders, it doesn't create them.
0
 
Travis MartinezSmoke JumperCommented:
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.
0
 
NVITCommented:
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

1
 
Travis MartinezSmoke JumperCommented:
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)
0
 
Pete LongTechnical ConsultantAuthor Commented:
Cheers Guys I will give these a test, your help is most appreciated!!

Pete
0
 
Pete LongTechnical ConsultantAuthor Commented:
Thanks again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.