powershell - 如果存在另一个子文件夹,则创建新的子文件夹

时间:2014-07-17 03:23:15

标签: batch-file powershell subdirectory

我试图创建一个powershell来在200多个父文件夹中创建一个新的子文件夹,但前提是父文件夹中存在另一个文件夹。到目前为止我的脚本在下面,没有错误,但它不会工作......任何帮助都会很好。

$foldername = "Folder1"
$sourceFolder = "C:\x\y\z"
foreach ($folder in (Get-ChildItem  -Directory $sourceFolder)) {
if (!$foldername) 
md FolderA
}

好的,我从头开始。我现在可以在所有子文件夹中创建'FolderB',但如果'FolderA'不存在,我就不能创建'FolderB'。

$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

}

}

1 个答案:

答案 0 :(得分:0)

不是100%清楚你在追求什么,但也许是这样的?

ls $sourceFolder -Directory                         # get all directories
    | ? { Test-Path (Join-Path $_.FullName $folderName) -PathType Container } # where a directory path of this folder+folderName exists
    | % { mkdir (Join-Path $_.FullName FolderA) } #build up a path and make that directory

不确定如果在多行上分解它是否会起作用,你可能必须将它全部放在一行上或者在一行的末尾放一个`来强制它

相关问题