-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvbat.cmd
More file actions
8 lines (6 loc) · 2.21 KB
/
vbat.cmd
File metadata and controls
8 lines (6 loc) · 2.21 KB
1
2
3
4
5
6
7
8
@echo off
SETLOCAL
:: Pass all arguments to PowerShell in a single line.
:: Internal logic detects adjacent '.encoding' files, parses 'encoding=value', and fallbacks to CP949.
powershell -NoProfile -Command "$args_str = '%*'; $file_pattern = ''; $enc_override = $null; $tokens = $args_str -split ' '; for($i=0; $i -lt $tokens.Length; $i++) { $t = $tokens[$i].Trim(); if ($t -eq '') { continue }; if ($t -eq '--help' -or $t -eq '-h') { Write-Host 'Usage:'; Write-Host ' vbat [filename/pattern] [--encoding=value] [-e value]'; exit }; if ($t -like '--encoding=*') { $enc_override = $t.Substring(11) } elseif ($t -eq '--encoding' -or $t -eq '-e') { $enc_override = $tokens[++$i].Trim() } else { $file_pattern = $t } }; if (-not $file_pattern) { Write-Host 'Error: Missing filename or pattern. Type `vbat -h` for help.'; exit }; $OutputEncoding = [System.Text.Encoding]::UTF8; $files = Get-ChildItem -Path $file_pattern -ErrorAction SilentlyContinue; if (-not $files) { Write-Host \"File(s) not found: $file_pattern\"; exit }; foreach ($f in $files) { $filePath = $f.FullName; $current_enc = 949; if ($enc_override) { $current_enc = $enc_override } else { $metaPath = $filePath + '.encoding'; if (Test-Path $metaPath) { $metaContent = Get-Content $metaPath -ErrorAction SilentlyContinue; foreach ($line in $metaContent) { if ($line -match '^\s*encoding\s*=\s*(.+)$') { $current_enc = $Matches[1].Trim(); break } } } }; if ($current_enc -ieq 'CP949') { $current_enc = 949 }; if ($current_enc -match '^\d+$') { $current_enc = [int]$current_enc }; if ($current_enc -ne 'Default' -and $current_enc -ne 'UTF-8' -and $current_enc -ne 'UTF8') { try { [void][System.Text.Encoding]::GetEncoding($current_enc) } catch { Write-Host \"Error: Unsupported encoding '$current_enc' for file $($f.Name)\"; continue } }; if ($current_enc -ieq 'Default') { [System.IO.File]::ReadAllText($filePath, [System.Text.Encoding]::Default) | bat --file-name $f.Name } elseif ($current_enc -ieq 'UTF-8' -or $current_enc -ieq 'UTF8') { [System.IO.File]::ReadAllText($filePath, (New-Object System.Text.UTF8Encoding($false))) | bat --file-name $f.Name } else { [System.IO.File]::ReadAllText($filePath, [System.Text.Encoding]::GetEncoding($current_enc)) | bat --file-name $f.Name } }"
ENDLOCAL