powershell7zipfile-extensionfile-listing

Find a file regardless of its extension


I'm a little stuck. I'm trying to create a script which extracts compressed ISO files and plays them on an Emulator. I've got this far:

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"

So my command to start a game (after converting this .ps1 into an .exe) would be something like:

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

So for example

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

I've identified where I need to modify my script to get it looking in the right place for my extracted file... The filename of the uncompressed file is always the same name as the archive except with either a .ISO or .BIN extension. Ideally I'd like the script to check for .ISO first and if not found, check for .BIN, but we're getting way above my knowledge level there... Any help much appreciated!


Solution

  • $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