由于某种原因,if / else子句不起作用。它确实创建了目录,但它并没有通过说“文件夹已存在”来执行if部分。它一定是超级明显的东西,我没有看到作为菜鸟。谢谢你的帮助。
Set-Location "\\domain.net\Target"
$Folders = Import-Csv "C:\Temp\Test.csv"
ForEach ($Folder in $Folders) {
if (Test-Path -LiteralPath $Folder -PathType Container)
{
Write-Host "Folder exists already"
}
else
{
New-Item $Folder.Name -Type Directory
}
}
CSV文件内容示例:
Name
Folder1
Folder2
Folder3
Folder3\Folder1
解决方案感谢以下人员的帮助:
#================= VARIABLES ==================================================
$Target = "\\Temp\Target"
$InputF = "\\Temp\Downloads\Test.csv"
#
# CSV-File format:
# Name
# Dir1
# Dir2
# Dir3\Subdir1
CLS
Write-Host "Folder creation`n"
#$Target = Read-Host "Provide the full UNC-Path where we need to create subfolders"
#$InputF = Read-Host "Provide the full UNC-Path & name of the CSV input file"
$Folders = Import-Csv $InputF
Set-Location $Target
Write-Host "`nStarting folder creation:`n"
ForEach ($Folder in $Folders) {
if (Test-Path -LiteralPath $Folder.Name -PathType Container)
{
Write-Host "-Exists: $Target\$($Folder.Name)" -ForegroundColor DarkGray
}
else
{
New-Item $Folder.Name -Type Directory > $null
Write-Host "-Created: $Target\$($Folder.Name)"
}
}
答案 0 :(得分:3)
我们需要一个csv样本来帮助您。最可能的问题是设置路径等于行对象($folder
)。 CSV具有标题,因此您需要使用路径访问该属性。例如。
if (Test-Path -LiteralPath $Folder.Name -PathType Container)
更新:在提供csv示例后,使用正确的属性名称更新了示例。