2017-08-22 65 views
1

我們當前的文件夾結構\服務器的\ usr \所有客戶端\與每一個客戶(諮詢,財務,工資,永久的,稅務)在多個文件夾中當前\ CLIENT_NAME。在多個特定父文件夾的子文件夾創建

我需要創建子文件夾,在短短的財務,工資和稅收稱爲2017年和2018年。

有超過2000的客戶,所以我想使用PowerShell腳本來做到這一點。我找到了以下示例,但它在財務下的所有文件夾中創建了2017子文件夾。

foreach ($folder in (Get-ChildItem '\\server\Usr\All Clients\Current\*\Financials' -Directory)) 
{ 
    new-item -ItemType directory -Path ($folder.fullname+"\2017") 
} 

我如何才能在特定文件夾中創建2017?

回答

0

您將需要一個地方對象來選擇要在

# 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 
} 

創建文件夾使用New-Item -WhatIf的文件夾,如果你想看到正在創建的文件夾,其中的輸出。我無法完全測試,因爲我無法訪問您的特定環境。

-1

試試這個。這是未經測試,但如果它不工作100%,這將讓你非常接近。

#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\$_" 
    } 
}) 
+0

奏效!感謝Adam – e1mariem

+0

太棒了!你能將它標記爲已回答嗎?不知道當我幫助解決你的問題時我爲什麼會低調,但是哦。 :) –

1

你可以使用一個數組來存儲的目錄在其中創建2017年和2018年

$ParentDirectories = @("Financials", "Payroll", "Tax") 

然後,過濾與陣列創建子目錄的文件夾。

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") 
    } 
} 

希望它有幫助!

編輯:測試和工程!

1

爲什麼不直接堆放一些的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 
    } 
    } 
} 

如果輸出看起來不錯,除去-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 
相關問題