Bel*_*igh 0 powershell create-directory
我试图解析文件夹中的文件名并将部分文件名存储在变量中.校验!然后,我想获取其中一个变量并检查该文件夹名称是否存在于其他位置,以及是否不创建它.如果我使用Write-Host文件夹名称是有效路径,并且文件夹名称不存在,但在执行脚本时,仍未创建文件夹.
如果文件夹不存在,我该怎么办?
$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
}
}
Run Code Online (Sandbox Code Playgroud)
Dav*_*ant 10
if (!(Test-Path $FolderToCreate -PathType Container)) {
New-Item -ItemType Directory -Force -Path $FolderToCreate
}
Run Code Online (Sandbox Code Playgroud)
尝试使用该-Force标志 - 它会检查每个子目录,当它们不存在时,它会简单地创建它并转到下一个,并且永远不会抛出错误。
在下面的示例中,您需要 7 个嵌套子目录,只需一行即可创建任何不存在的子目录。
您还可以根据需要多次重新运行它,并且它被设计为永远不会抛出错误!
New-Item -ItemType Directory -Force -Path C:\Path\That\May\Or\May\Not\Exist
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13745 次 |
| 最近记录: |