Batch file powershell-在多个父文件夹中批量创建子文件夹

Batch file powershell-在多个父文件夹中批量创建子文件夹,batch-file,powershell,Batch File,Powershell,我正在尝试在“Folder1”到“Folder200”的所有文件夹中创建“FolderB”子文件夹。仅当“Folder1”到“Folder200”包含名为“FolderA”的子文件夹时,才会发生这种情况。然后将在与FolderA相同的级别创建FolderB。我可以创建FolderB,但是我不能让脚本跳过任何不包含FolderA的父文件夹 $root = "\\server\folder1" ForEach ($dir in (Get-Item -Path "$root\*\" | ?{$_.P

我正在尝试在“Folder1”到“Folder200”的所有文件夹中创建“FolderB”子文件夹。仅当“Folder1”到“Folder200”包含名为“FolderA”的子文件夹时,才会发生这种情况。然后将在与FolderA相同的级别创建FolderB。我可以创建FolderB,但是我不能让脚本跳过任何不包含FolderA的父文件夹

$root = "\\server\folder1"

ForEach ($dir in (Get-Item -Path "$root\*\" | ?{$_.PSIsContainer})){

If (!(Test-Path -Path "\*\FolderA")) {

New-Item -Path "$root\*\" -Name "FolderB" -ItemType Directory | Out-Null

}

}

在这里,这应该更适合你。它实际上使用路径在循环中测试,我修复了If语句,因为您正在检查是否存在FolderA,然后创建FolderB:

$root = "\\server\folder1"

ForEach ($dir in (Get-ChildItem -Path "$root\*\" | ?{$_.PSIsContainer} | Select -Expand Name)){

 If ((Test-Path "$dir\FolderA")) {

  New-Item -Path "$dir\FolderB" -ItemType Directory | Out-Null

 }

}

您是否注意到您在循环中说了
ForEach($dir in…
),但从未引用
$dir