Reputation: 11
Our current folder structure is \server\Usr\All Clients\Current\client_name with multiple folders under each client (consulting, financials, payroll, permanent, tax).
I need to create subfolders called 2017 and 2018 in just financials, payroll, and tax.
There are over 2000 clients, so I'd like to use a PowerShell script to do it. I found the following example, but it creates the 2017 subfolders in all folders under financials.
foreach ($folder in (Get-ChildItem '\\server\Usr\All Clients\Current\*\Financials' -Directory))
{
new-item -ItemType directory -Path ($folder.fullname+"\2017")
}
How can I have it only create 2017 in specific folders?
Upvotes: 1
Views: 1875
Reputation:
Why not just stack some ForEach:
ForEach ($Client in (Get-ChildItem "\\server\Usr\All Clients\Current\*" -Directory)){
ForEach ($Depth in 'Financials','Payroll','Tax') {
ForEach ($Year in '2017','2018') {
New-Item -ItemType Directory -Path ("{0}\{1}\{2}" -f $($Client.fullname),$Depth,$Year ) -Whatif
}
}
}
If the output looks OK , remove the -WhatIf
Sample run on my Ramdrive A: with pseudo clients Baker,Miller,Smith:
> tree
A:.
├───Baker
│ ├───Financials
│ │ ├───2017
│ │ └───2018
│ ├───Payroll
│ │ ├───2017
│ │ └───2018
│ └───Tax
│ ├───2017
│ └───2018
├───Miller
│ ├───Financials
...
└───Smith
├───Financials
│ ├───2017
│ └───2018
├───Payroll
│ ├───2017
│ └───2018
└───Tax
├───2017
└───2018
Upvotes: 1
Reputation: 101
You could use an array to store the directories in which to create 2017 and 2018.
$ParentDirectories = @("Financials", "Payroll", "Tax")
And then, filter the folders with the array creating sub-directories.
Get-ChildItem -Path '\server\Usr\All Clients\Current\' | ForEach-Object {
$Client = $_.Name;
Get-ChildItem -Path $Client | Where-Object { $_.Name -in $ParentDirectories } | ForEach-Object {
New-Item -ItemType Directory @("$Client\$_\2017", "$Client\$_\2018")
}
}
Hope it helps !
Edit: Tested and works !
Upvotes: 1
Reputation: 4188
Try this. It's untested but if it doesn't work 100%, it'll get you REALLY close.
#requires -Version 5
$createFolders = '2017','2018'
@(Get-ChildItem -Path '\\server\Usr\All Clients\Current' -Recurse -Directory -Depth 1).where({ $_.Name -in 'financials','payroll','tax' }).foreach({
$clientFolder = $_.FullName;
$createFolders | foreach {
$null = mkdir -Path "$clientFolder\$_"
}
})
Upvotes: 0
Reputation: 2342
You will need a where object to select the folders that you want to create the folder in
# Get folders that are Financials, Payrol, or Tax
$Folders = Get-ChildItem '\\server\Usr\All Clients\Current\*' | Where-Object -Property Name -in -Value 'Financials','Payroll','Tax'
# Loop through those folders
foreach ($Folder in $Folders)
{
$2017Path = Join-Path -Path $Folder.FullName -ChildPath '2017' # Generate path to 2017 folder
$2018Path = Join-Path -Path $Folder.FullName -ChildPath '2018' # Generate path to 2018 folder
New-Item -Path $2017Path -Force # Create 2017 folder
New-Item -Path $2018Path -Force # Create 2018 folder
}
Use New-Item -WhatIf
if you want to see output of where the folders are being created. I am unable to completely test as I have no access to your specific environment.
Upvotes: 0