Skip to content

Instantly share code, notes, and snippets.

@DineshSolanki
Created March 22, 2023 14:11
Show Gist options
  • Save DineshSolanki/60481e1087528e950d71a89e0a0fe038 to your computer and use it in GitHub Desktop.
Save DineshSolanki/60481e1087528e950d71a89e0a0fe038 to your computer and use it in GitHub Desktop.
remove ansi escape codes from log files
# Get input path from user
$path = Read-Host "Enter file or folder path"
# Check if input path is a file or folder
if (Test-Path $path -PathType Leaf) {
# If path is a file, process it and save output to 'Cleaned' subdirectory
$outputPath = Join-Path (Split-Path $path) "Cleaned"
New-Item -ItemType Directory -Path $outputPath -Force
(Get-Content $path -Raw) -replace "\x1B\[[0-9;]*[mK]" | Set-Content (Join-Path $outputPath (Split-Path $path -Leaf))
} elseif (Test-Path $path -PathType Container) {
# If path is a folder, process all files within it and save output to 'Cleaned' subdirectory
$outputPath = Join-Path $path "Cleaned"
New-Item -ItemType Directory -Path $outputPath -Force
Get-ChildItem $path -File | ForEach-Object {
(Get-Content $_.FullName -Raw) -replace "\x1B\[[0-9;]*[mK]" | Set-Content (Join-Path $outputPath $_.Name)
}
} else {
Write-Host "Invalid input path"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment