Skip to content

Instantly share code, notes, and snippets.

@derammo
Last active October 11, 2024 12:46
Show Gist options
  • Select an option

  • Save derammo/73e3da28f1db4661bda2a47d10934ae6 to your computer and use it in GitHub Desktop.

Select an option

Save derammo/73e3da28f1db4661bda2a47d10934ae6 to your computer and use it in GitHub Desktop.
remove Type=Ground+Vehicle from one zipped TacView ACMI file at a time
# Define the directory to search for .acmi files
$directory = "."
# Get the most recently modified .zip.acmi file that does not have a backup file
$latestFile = Get-ChildItem -Path $directory -Filter *.zip.acmi | Where-Object { -not (Test-Path "$($_.FullName).bak") } | Sort-Object LastWriteTime -Descending | Select-Object -First 1
if (-not $latestFile) {
Write-Output "No unprocessed .zip.acmi files found in the directory."
exit 1
}
if (Test-Path "$($latestFile.FullName).bak") {
Write-Output "$($latestFile.FullName) cannot be modified; backup file '$($latestFile.FullName).bak' already exists."
exit 2
}
# Create temporary directories for unzipped contents and modified archive
$tempContentDir = New-Item -ItemType Directory -Path (Join-Path $env:TEMP ([System.Guid]::NewGuid().ToString()))
$tempDir = New-Item -ItemType Directory -Path (Join-Path $env:TEMP ([System.Guid]::NewGuid().ToString()))
# Copy the latest .acmi file to the temporary directory and rename it because Expand-Archive does not
# support this file extension
$copiedFilePath = Join-Path $tempDir.FullName ($latestFile.Name -replace '\.zip\.acmi$', '.acmi.zip')
Copy-Item -Path $latestFile.FullName -Destination $copiedFilePath
# Unzip the .acmi file
Expand-Archive -DestinationPath $tempContentDir.FullName -LiteralPath $copiedFilePath -Force
# Path to the acmi.txt file inside the unzipped folder
$acmiTxtPath = Join-Path $tempContentDir.FullName "acmi.txt"
if (Test-Path $acmiTxtPath) {
# Read the contents of acmi.txt
$content = [System.IO.File]::ReadAllText($acmiTxtPath)
# Perform search and replace operation
$modifiedContent = $content.Replace("Type=Ground+Vehicle,", "")
# Write the modified content back to acmi.txt
[System.IO.File]::WriteAllText($acmiTxtPath, $modifiedContent)
# Create a new zip archive with the modified acmi.txt
$newZipPath = Join-Path $tempContentDir.FullName "$($latestFile.BaseName)"
Compress-Archive -Path "$($tempContentDir.FullName)\acmi.txt" -DestinationPath $newZipPath
# copy the time stamps of $latestFile to $newZipPath
(Get-Item $newZipPath).LastWriteTime = $latestFile.LastWriteTime
(Get-Item $newZipPath).CreationTime = $latestFile.CreationTime
# Rename the original .zip.acmi file to .zip.acmi.bak
Rename-Item -Path $latestFile.FullName -NewName "$($latestFile.FullName).bak"
# Move the new zip archive to the original file's location
Move-Item -Path $newZipPath -Destination $latestFile.FullName
Write-Output "$($latestFile.FullName) modified; original ACMI file backed up as '$($latestFile.FullName).bak'."
} else {
Write-Output "acmi.txt not found in the archive."
exit 3
}
# Clean up temporary directories
Remove-Item -Path $tempContentDir.FullName -Recurse -Force
Remove-Item -Path $tempDir.FullName -Recurse -Force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment