查找文件,而不管其扩展名如何

时间:2018-09-10 12:47:44

标签: powershell 7zip file-extension file-listing

我有点卡住了。我正在尝试创建一个脚本,以提取压缩的ISO文件并在模拟器上播放它们。我已经做到了:

param (    
    [string]$7zPath, # File location of 7z
    [string]$emulatorPath, # File location of the Emulator exe
    [string]$filePath # File location of the compressed file
)

$7zArgList = @(
    "e", # 7z arguments go here
    $filePath
)

# Location of the decompressed game <<< This is what is incorrect!!!
$decompressedFile = $filePath.Substring(0, $filePath.LastIndexOf("."))

# Start 7z and print the output in the console
"Starting decompression using 7z"
& $7zPath $7zArgList | Out-Host

# Start Emulator
"Starting Emulator"
Start-Process $emulatorPath -ArgumentList """$decompressedFile""" -Wait

# Remove the decompressed file when Emulator closes
"Removing the decompressed file"
Remove-Item $decompressedFile

"Done"

所以我启动游戏的命令(将这个.ps1转换为.exe之后)将类似于:

"ThisCode.exe" "7zip.exe" "Emulator.exe" "Path2Rom"

例如

"C:\7zPrepper.exe" "C:\Program Files\7-Zip\7z.exe" "C:\Program Files (x86)\PCSX2\pcsx2.exe" "C:\Roms\Game 1.7z"

我已经确定了需要修改脚本的位置,以使其在正确的位置查找提取的文件...未压缩文件的文件名始终与归档文件相同,但使用.ISO或.BIN扩展名。理想情况下,我希望脚本先检查.ISO,如果找不到,请检查.BIN,但是我们的知识水平超出了我的理解范围……任何帮助,我们将不胜感激!

1 个答案:

答案 0 :(得分:4)

$filepath_without_ext = [System.IO.Path]::ChangeExtension($filepath,$null)
# or
$filepath_without_ext = $filePath.Substring(0, $filePath.LastIndexOf("."))

$decompressedFile = Get-Item -Path "$filepath_without_ext*" | Where-Object -Property Extension -Match -Value 'BIN|ISO' | Select-Object -Last 1 -ExpandProperty FullName