Scripting the creation of subfolders IF a condition exists

Hi all, i know this has been asked a lot but i cannot find anything for my specific scenario. I have a Windows file share with 1,691 folders, these folders contain numerous subfolders. What i want is to scan each top folder and check for subfolders named A, B and C, if these exist, i then want to go into each of them and create a folder called NEW. What i should end up with is

Folder1 > A > 2018
Folder1 > B > 2018
Folder1 > C > 2018

Folder1 may also contain subfolders D, E and F but i don't want anything to happen to these. Also, Folder1 may only contain subfolder B, if that is the case I do want the 2018 folder to be created.

As i said, i have 1691 folders to scan and they could each contain 3 folders that i need to create a subfolder for so all in all a possible 5073 folder creations. I'd rather not do this manually.
LVL 1
HallidaysAsked:
Who is Participating?
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.

oBdACommented:
Not completely sure if I understood you correctly.
This will look for all subfolders of $Source. In each subfolder of $Source, it will check for all names defined in $NewFolderParents whether it has a subfolder named $NewFolder; if not, it will be created.
If one of the Parent folders is not found, nothing will happen.
If $NewFolder already exists under a Parent folder, nothing will happen, either.
This is in test mode and will only display the folders it would create normally. Remove the "-WhatIf" in line 8 to run it for real.
$Source = 'C:\Temp'
$NewFolderParents = 'A', 'B', 'C'
$NewFolder = '2018'
Get-ChildItem -Path $Source -Directory | ForEach-Object {
	ForEach ($Parent In $NewFolderParents) {
		If ((Test-Path -Path "$($_.FullName)\$($Parent)") -and (-not (Test-Path -Path "$($_.FullName)\$($Parent)\$($NewFolder)"))) {
			New-Item -Path "$($_.FullName)\$($Parent)" -Name $NewFolder -ItemType Directory -WhatIf | Out-Null
		}
	}
}

Open in new window

0

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
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
To clarify: "If one of the Parent folders is not found, nothing will happen" means that for each of the parents found the folder creation is performed. It does not mean that all three (or whatever) parents need to exist.
0
HallidaysAuthor Commented:
Brilliant thank you, i'll test this and let you know.
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

HallidaysAuthor Commented:
Ok, so i get

}
Get-ChildItem : A parameter cannot be found that matches parameter name 'Directory'.
At line:4 char:39
+ Get-ChildItem -Path $Source -Directory <<<<  | ForEach-Object {
    + CategoryInfo          : InvalidArgument: (:) [Get-ChildItem], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand
0
oBdACommented:
Powershell 2.0? From 2009? Seriously?
$Source = 'C:\Temp'
$NewFolderParents = 'A', 'B', 'C'
$NewFolder = '2018'

Get-ChildItem -Path $Source | Where-Object {$_.PsIsContainer} | ForEach-Object {
	ForEach ($Parent In $NewFolderParents) {
		If ((Test-Path -Path "$($_.FullName)\$($Parent)") -and (-not (Test-Path -Path "$($_.FullName)\$($Parent)\$($NewFolder)"))) {
			New-Item -Path "$($_.FullName)\$($Parent)" -Name $NewFolder -ItemType Directory -WhatIf | Out-Null
		}
	}
}

Open in new window

0
HallidaysAuthor Commented:
Ha, sorry, just formatted my PC - new one installing.
0
HallidaysAuthor Commented:
Excellent thank you, this worked a treat!
0
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
Powershell

From novice to tech pro — start learning today.

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.