I have a large number of root folders (non-sequentially named), and I need a script to create a duplicate set of subfolders within each root folder, like this:
EXAMPLE
- Root Folder 1 - Sub Folder A - Sub Sub Folder 1 - Sub Sub Folder 2 - Sub Sub Folder 3 - Sub Sub Folder 4 - Sub Sub Folder 5 - Sub Folder B - Sub Folder C
- Root Folder 2 - Sub Folder A - Sub Sub Folder 1 - Sub Sub Folder 2 - Sub Sub Folder 3 - Sub Sub Folder 4 - Sub Sub Folder 5 - Sub Folder B - Sub Folder C...And so on.
The subfolders and sub-subfolders are consistently named for all root folders, but the root folder names will vary.
I'm able to create the root folders within the D:\TEMP directory with output from a TXT file, via the following:
for /F "usebackq delims=" %i in (FolderNames.txt) do md "%i"I'm also able to create the first level of subfolders within each root folder, via a series of PowerShell commands:
$folders = get-childitem "C:\TEMP\WORKING\FOLDERS"
foreach ($folder in $folders ) { New-Item -Name "Daily Completion Paperwork" -Path $folder.FullName -ItemType Directory -verbose; New-Item -Name "Final Completion Documents" -Path $folder.FullName -ItemType Directory -verbose; New-Item -Name "Site Documents" -Path $folder.FullName -ItemType Directory -verbose
}This gives me:
- Root Folder 1 - Sub Folder A - Sub Folder B - Sub Folder C
- Root Folder 2 - Sub Folder A - Sub Folder B - Sub Folder CWhat I'm struggling with is a separate command - or a nested command - that will create the 5 "Sub Sub Folders" inside Sub Folder A:
EXAMPLE
- Root Folder 1 - Sub Folder A - Sub Sub Folder 1 - Sub Sub Folder 2 - Sub Sub Folder 3 - Sub Sub Folder 4 - Sub Sub Folder 5
- Root Folder 2 - Sub Folder A - Sub Sub Folder 1 - Sub Sub Folder 2 - Sub Sub Folder 3 - Sub Sub Folder 4 - Sub Sub Folder 5Unfortunately this involves a level of nesting that I haven't been able to nail down; so far I'm just creating "Sub Sub Folders" inside all folders, which creates a bigger problem.
Any suggestions regarding how to modify my existing PowerShell commands would be greatly appreciated!
53 Answers
For this specific example, I would create a helper function to create each folder structure as you would like it.
Something like this:
function New-FolderTree { [CmdletBinding()] Param( [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline = $true)] [string]$Rootfolder, [string]$LevelOneName, # name pattern for level 1 subfolders [string]$LevelTwoName, # name pattern for level 2 subfolders [int]$LevelOneCount, # howmany level 1 subfolders [int]$LevelTwoCount # howmany level 2 subfolders ) # step 1: create the rootfolder including the level one subfolders for ($i = 1; $i -le $LevelOneCount; $i++) { # LevelOne foldernames get a suffix of _A, _B, _C, etc. $path = Join-Path -Path $Rootfolder -ChildPath ('{0}_{1}' -f $LevelOneName, [char](64 + $i)) if (!(Test-Path -Path $path -PathType Container)) { $null = New-Item -Path $path -ItemType Directory } } # step 2: create all level 2 subfolders in the first level 1 subfolder $subfolderA = Join-Path -Path $Rootfolder -ChildPath ($LevelOneName + '_A') for ($i = 1; $i -le $LevelTwoCount; $i++) { # LevelTwo foldernames get a suffix of _1, _2, _3, etc. $path = Join-Path -Path $subfolderA -ChildPath ('{0}_{1}' -f $LevelTwoName, $i) if (!(Test-Path -Path $path -PathType Container)) { $null = New-Item -Path $path -ItemType Directory } }
}Using that function, you can quickly create each folder structure with
New-FolderTree -Rootfolder 'D:\RootFolder1' -LevelOneName 'SubFolder' -LevelOneCount 3 -LevelTwoName 'SubSubFolder' -LevelTwoCount 5
New-FolderTree -Rootfolder 'D:\RootFolder2' -LevelOneName 'SubFolder' -LevelOneCount 3 -LevelTwoName 'SubSubFolder' -LevelTwoCount 5 1 If all the sub-folers have the same sub-sub folders, you just need nested ForEach. Splatting makes the code much more readable:
$Level1 = 'Daily Completion Paperwork', 'Final Completion Documents', 'Site Documents'
$Level2 = 1..5 Get-ChildItem -Directory "C:\TEMP\WORKING\FOLDERS"| ForEach{ $Splat =@{ 'Path' = $_.FullName 'ItemType' = 'Directory' } $Level1 | FOrEach{ $Splat2 = @{ 'Path' = (New-Item @Splat -Name $_).FullName 'ItemType' = 'Directory' } $Level2 | ForEach{ New-Item @Splat2 -Name $_ | Out-Null } }
} PowerShell notwithstanding.
Best practice, on OS file systems, avoid spaces in file, folder and variable names. You are just asking for trouble if you do this. Always use underscores as separators, or CamelCasedNames, or the like to save yourself unnecessary headaches, frustration, and hair-pulling.
A very rough simple step to be a bit more dynamic for your use case could be something like this (of course modify to the depth you are after - remove the -WhatIf if you want this to actually create stuff.):
# Using a single nested loop
Clear-Host
# Pass in a defined root folder array list to create root folders.
1..3 |
ForEach { "Creating new folder root named RootFolder$PSItem" New-Item -Path 'D:\Temp' -ItemType Directory -Name "RootFolder$PSItem" -WhatIf $RootFolderPath = "D:\Temp\RootFolder$PSItem" # Ask for a end boundary for sub folder array list $FolderCount = Read-Host -Prompt " For the new rootFolder $RootFolderPath, how many sub folders do you need. Enter a number." # Pass in a defined root folder araay list to crete child folders. 1..$FolderCount | ForEach { "Using a sub folder count of $FolderCount Creating sub folder 1 - SubFolder$FolderCount" New-Item -Path $RootFolderPath -ItemType Directory -Name "SubFolderr$PSitem" -WhatIf } "`n" '*'*60 "*** Finished creating scaffold for $RootFolderPath ***" '*'*60 "`n"
}
# Results
<#
Creating new folder root named RootFolder1
What if: Performing the operation "Create Directory" on target "Destination: D:\Temp\RootFolder1". For the new rootFolder D:\Temp\RootFolder1, how many sub folders do you need. Enter a number.: 5
Using a sub folder count of 5 Creating sub folder 1 - SubFolder5
What if: Performing the operation "Create Directory" on target "Destination: D:\Temp\RootFolder1\SubFolderr1".
Using a sub folder count of 5 Creating sub folder 1 - SubFolder5
What if: Performing the operation "Create Directory" on target "Destination: D:\Temp\RootFolder1\SubFolderr2".
Using a sub folder count of 5 Creating sub folder 1 - SubFolder5
What if: Performing the operation "Create Directory" on target "Destination: D:\Temp\RootFolder1\SubFolderr3".
Using a sub folder count of 5 Creating sub folder 1 - SubFolder5
What if: Performing the operation "Create Directory" on target "Destination: D:\Temp\RootFolder1\SubFolderr4".
Using a sub folder count of 5 Creating sub folder 1 - SubFolder5
What if: Performing the operation "Create Directory" on target "Destination: D:\Temp\RootFolder1\SubFolderr5".
************************************************************
*** Finished creating scaffold for D:\Temp\RootFolder1 ***
************************************************************
Creating new folder root named RootFolder2
What if: Performing the operation "Create Directory" on target "Destination: D:\Temp\RootFolder2". For the new rootFolder D:\Temp\RootFolder2, how many sub folders do you need. Enter a number.: 5
Using a sub folder count of 5 Creating sub folder 1 - SubFolder5
What if: Performing the operation "Create Directory" on target "Destination: D:\Temp\RootFolder2\SubFolderr1".
Using a sub folder count of 5 Creating sub folder 1 - SubFolder5
What if: Performing the operation "Create Directory" on target "Destination: D:\Temp\RootFolder2\SubFolderr2".
Using a sub folder count of 5 Creating sub folder 1 - SubFolder5
What if: Performing the operation "Create Directory" on target "Destination: D:\Temp\RootFolder2\SubFolderr3".
Using a sub folder count of 5 Creating sub folder 1 - SubFolder5
What if: Performing the operation "Create Directory" on target "Destination: D:\Temp\RootFolder2\SubFolderr4".
Using a sub folder count of 5 Creating sub folder 1 - SubFolder5
What if: Performing the operation "Create Directory" on target "Destination: D:\Temp\RootFolder2\SubFolderr5".
************************************************************
*** Finished creating scaffold for D:\Temp\RootFolder2 ***
************************************************************
Creating new folder root named RootFolder3
What if: Performing the operation "Create Directory" on target "Destination: D:\Temp\RootFolder3". For the new rootFolder D:\Temp\RootFolder3, how many sub folders do you need. Enter a number.: 5
Using a sub folder count of 5 Creating sub folder 1 - SubFolder5
What if: Performing the operation "Create Directory" on target "Destination: D:\Temp\RootFolder3\SubFolderr1".
Using a sub folder count of 5 Creating sub folder 1 - SubFolder5
What if: Performing the operation "Create Directory" on target "Destination: D:\Temp\RootFolder3\SubFolderr2".
Using a sub folder count of 5 Creating sub folder 1 - SubFolder5
What if: Performing the operation "Create Directory" on target "Destination: D:\Temp\RootFolder3\SubFolderr3".
Using a sub folder count of 5 Creating sub folder 1 - SubFolder5
What if: Performing the operation "Create Directory" on target "Destination: D:\Temp\RootFolder3\SubFolderr4".
Using a sub folder count of 5 Creating sub folder 1 - SubFolder5
What if: Performing the operation "Create Directory" on target "Destination: D:\Temp\RootFolder3\SubFolderr5".
************************************************************
*** Finished creating scaffold for D:\Temp\RootFolder3 ***
************************************************************
#> 2