We help IT Professionals succeed at work.

Scripting the creation of subfolders IF a condition exists

Hallidays
Hallidays asked
on
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.
Comment
Watch Question

Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
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

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
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.

Author

Commented:
Brilliant thank you, i'll test this and let you know.

Author

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
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
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

Author

Commented:
Ha, sorry, just formatted my PC - new one installing.

Author

Commented:
Excellent thank you, this worked a treat!