Avatar of Hallidays
Hallidays
 asked on

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.
PowershellWindows OS

Avatar of undefined
Last Comment
Hallidays

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
oBdA

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Qlemo

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.
Hallidays

ASKER
Brilliant thank you, i'll test this and let you know.
Hallidays

ASKER
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
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
oBdA

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

Hallidays

ASKER
Ha, sorry, just formatted my PC - new one installing.
Hallidays

ASKER
Excellent thank you, this worked a treat!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.