我有大量的根文件夹(非连续命名),我需要一个脚本来在每个根文件夹内创建一组重复的子文件夹,如下所示:
例子
- 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
...等等。
所有根文件夹的子文件夹和子子文件夹的命名都是一致的,但是根文件夹名称会有所不同。
我能够通过以下方式使用 TXT 文件的输出在 D:\TEMP 目录中创建根文件夹:
for /F "usebackq delims=" %i in (FolderNames.txt) do md "%i"
我还可以通过一系列 PowerShell 命令在每个根文件夹中创建第一级子文件夹:
$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
}
这给了我:
- Root Folder 1
- Sub Folder A
- Sub Folder B
- Sub Folder C
- Root Folder 2
- Sub Folder A
- Sub Folder B
- Sub Folder C
我正在努力的是一个单独的命令 - 或者一个嵌套的命令 - 它将创建子文件夹 A 内的 5 个“子子文件夹”:
例子
- 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 5
不幸的是,这涉及到一个我无法确定的嵌套级别;到目前为止,我只是在里面创建了“子子文件夹”全部文件夹,这会造成更大的问题。
非常感谢有关如何修改我现有的 PowerShell 命令的任何建议!
答案1
对于这个特定的例子,我将创建一个辅助函数来按照您的需要创建每个文件夹结构。
像这样:
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
}
}
}
使用该功能,您可以快速创建每个文件夹结构
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
答案2
如果所有子文件夹都有相同的子子文件夹,则只需要嵌套 ForEach。溅射使代码更具可读性:
$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
}
}
}
答案3
尽管有 PowerShell。
最佳实践是,在操作系统文件系统上,避免在文件、文件夹和变量名称中使用空格。如果你这样做,你只会自找麻烦。始终使用下划线作为分隔符,或使用 CamelCasedNames 等,以免出现不必要的麻烦、挫败感和烦恼。
一个非常粗略的简单步骤,可以为您的用例提供更多的动态,可能是这样的(当然,修改到您想要的深度 - 如果您希望它真正创建东西,请删除-WhatIf。):
# 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 ***
************************************************************
#>