问题描述
让我从说起这件事开始我是一个完全的初学者。我正在尝试用占位符替换一堆丢失的文件。问题是有多种文件类型,每种文件类型需要一个不同的占位符文件。
我使用的是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
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)