我正在试图解析文件夹中的文件名并将文件的部分存储在变量中。检查!然后我想要采取其中一个变量,并检查该文件夹名称是否存在于不同的位置,以及它是否不创建它。如果我使用Write-Host
,文件夹名称是有效的路径,并且文件夹名称不存在,但在执行脚本时仍不会创建文件夹。Powershell创建文件夹如果不存在
如果文件夹不存在,我应该怎么做?
$fileDirectory = "C:\Test\"
$ParentDir = "C:\Completed\"
foreach ($file in Get-ChildItem $fileDirectory){
$parts =$file.Name -split '\.'
$ManagerName = $parts[0].Trim()
$TwoDigitMonth = $parts[1].substring(0,3)
$TwoDigitYear = $parts[1].substring(3,3)
$FolderToCreate = Join-Path -Path $ParentDir -ChildPath $ManagerName
If(!(Test-Path -path "$FolderToCreate\"))
{
#if it does not create it
New-Item -ItemType -type Directory -Force -Path $FolderToCreate
}
}
试试这个: 新建项目-ItemType目录-Force -Path C:\路径\这\ \可能还是\ \可能不存在\ –