【问题标题】:Creating subfolders in specific, multiple parent folders在特定的多个父文件夹中创建子文件夹
【发布时间】:2017-08-22 15:16:52
【问题描述】:

我们当前的文件夹结构是 \server\Usr\All Clients\Current\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?

【问题讨论】:

    标签: powershell directory directory-structure


    【解决方案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\$_" 
        }
    })
    

    【讨论】:

    • 太棒了!你能把它标记为已回答吗?不知道为什么当我帮助解决您的问题时我被否决了,但是哦,好吧。 :)
    【解决方案2】:

    您可以使用数组来存储创建 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")
        }
    }
    

    希望对你有帮助!

    编辑:经过测试并且有效!

    【讨论】:

      【解决方案3】:

      为什么不直接堆叠一些 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
      

      【讨论】:

        【解决方案4】:

        您将需要一个 where 对象来选择要在其中创建文件夹的文件夹

        # 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。我无法完全测试,因为我无法访问您的特定环境。

        【讨论】:

          最近更新 更多