由于某些原因,if/else语句无法正常工作。它确实可以创建目录,但没有执行if部分并显示“文件夹已存在”。我作为一个新手可能没发现一些非常明显的问题。感谢您的帮助。
CSV文件内容示例:
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文件内容示例:
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)"
}
}
$Folder.Name
,例如if (Test-Path -LiteralPath $Folder.Name -PathType Container)
来访问文件夹名称。 - tkokasih