让我先说我在这方面是一个完整的初学者。我正在尝试用占位符替换一堆丢失的文件。问题是有多种文件类型,每种都需要不同的占位符文件。
我正在使用 powershell 7,并且 Split-Path -extension 似乎工作得很好。但我似乎无法使用 if 语句来选择正确的占位符文件。
任何帮助将不胜感激。
# List of Files to be replaced
$CSVFile ="C:\FileList.csv"
# Header for file location column
$FileLocationHeaderInCSV ="FileLocation"
#PlaceHolderFile Location
$PlaceHolderJPG ="C:\MissingFile.jpg"
$PlaceHolderTIF ="C:\MissingFile.tif"
Import-Csv $CSVFile | ForEach-Object {
#CSV File Header Row
$FileLocation = $_.$($FileLocationHeaderInCSV)
$DestinationFolder= (Split-Path -Path $FileLocation)
#Test if the folder exists, if it doesn't create it!
if (!(Test-Path -path $DestinationFolder)) {
New-Item $DestinationFolder -Type Directory
Write-Host "Creating Folder '$DestinationFolder'" -ForegroundColor Green
}
else {
}
$FileExtension =(Split-Path -Extension $FileLocation)
if ($FileExtension -eq ".JPG") {
$PlaceHolderFile= $PlaceHolderJPG
}
elseif ($FileExtension -eq ".TIF") {
$PlaceHolderFile= $PlaceHolderTIF
}
elseif ($FileExtension -eq ".TIFF") {
$PlaceHolderFile= $PlaceHolderTIF
}
elseif ($FileExtension -eq ".IMG") {
$PlaceHolderFile= $PlaceHolderTIF
}
else {
Write-Host "Unknown File Type!!!!" -ForegroundColor Red
}
Write-host "Replacing File '$FileLocation'" -ForegroundColor Cyan
Copy-Item $PlaceHolderFile -Destination $FileLocation
}