Repository navigation
Expand file tree
/
Copy pathconvert-encoding.ps1
More file actions
135 lines (107 loc) · 3.95 KB
/
Copy pathconvert-encoding.ps1
File metadata and controls
135 lines (107 loc) · 3.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env pwsh
<#
.SYNOPSIS
Convert files from Windows-1251 to UTF-8 encoding recursively.
.DESCRIPTION
This script converts all text files in a specified folder (and subfolders)
from Windows-1251 encoding to UTF-8 encoding with BOM.
.PARAMETER Path
The path to the folder containing files to convert.
.PARAMETER Extensions
File extensions to process. Default: php, html, htm, css, js, txt, xml, sql
.PARAMETER DryRun
Show what would be converted without actually converting files.
.PARAMETER Backup
Create backup files with .bak extension before converting.
.EXAMPLE
.\convert-encoding.ps1 -Path "C:\myproject" -DryRun
Show what files would be converted.
.EXAMPLE
.\convert-encoding.ps1 -Path "C:\myproject" -Backup
Convert files with backup.
.EXAMPLE
.\convert-encoding.ps1 -Path "C:\myproject" -Extensions "php,txt"
Convert only PHP and TXT files.
#>
param(
[Parameter(Mandatory=$true, HelpMessage="Enter the folder path to convert")]
[string]$Path,
[Parameter()]
[string[]]$Extensions = @("php", "html", "htm", "css", "js", "txt", "xml", "sql"),
[Parameter()]
[string]$InputEncoding = "windows-1251",
[Parameter()]
[string]$OutputEncoding = "utf-8",
[Parameter()]
[switch]$DryRun,
[Parameter()]
[switch]$Backup
)
# Validate path
if (-not (Test-Path $Path)) {
Write-Error "Path '$Path' does not exist."
exit 1
}
# Convert extensions to include dot
$ExtensionsWithDot = foreach ($ext in $Extensions) { ".$ext" }
Write-Host "Encoding Conversion: $InputEncoding to $OutputEncoding" -ForegroundColor Green
Write-Host "Path: $Path" -ForegroundColor Cyan
Write-Host "Extensions: $($Extensions -join ', ')" -ForegroundColor Cyan
Write-Host "Dry Run: $DryRun" -ForegroundColor Yellow
Write-Host "Backup: $Backup" -ForegroundColor Yellow
Write-Host ""
# Get all files with specified extensions
$files = Get-ChildItem -Path $Path -Recurse -File | Where-Object {
$ExtensionsWithDot -contains $_.Extension
}
Write-Host "Found $($files.Count) files to process" -ForegroundColor Green
Write-Host ""
if ($files.Count -eq 0) {
Write-Host "No files found to convert." -ForegroundColor Yellow
exit 0
}
$converted = 0
$errors = 0
foreach ($file in $files) {
try {
# Read file with specified input encoding
$content = [System.IO.File]::ReadAllText($file.FullName, [System.Text.Encoding]::GetEncoding($InputEncoding))
if ($DryRun) {
Write-Host "[DRY RUN] Would convert: $($file.FullName)" -ForegroundColor Gray
} else {
# Create backup if requested
if ($Backup) {
$backupPath = $file.FullName + ".bak"
Copy-Item -Path $file.FullName -Destination $backupPath -Force
}
# Write file with specified output encoding
if ($OutputEncoding -eq "utf-8") {
$utf8WithoutBom = New-Object System.Text.UTF8Encoding($false)
[System.IO.File]::WriteAllText($file.FullName, $content, $utf8WithoutBom)
} else {
[System.IO.File]::WriteAllText($file.FullName, $content, [System.Text.Encoding]::GetEncoding($OutputEncoding))
}
Write-Host "Converted: $($file.FullName)" -ForegroundColor Green
}
$converted++
}
catch {
Write-Error "Failed to convert $($file.FullName): $($_.Exception.Message)"
$errors++
}
}
Write-Host ""
Write-Host "Conversion Summary:" -ForegroundColor Cyan
Write-Host "Files processed: $converted" -ForegroundColor Green
Write-Host "Errors: $errors" -ForegroundColor Red
if ($errors -gt 0) {
Write-Host ""
Write-Warning "Some files failed to convert. Check the error messages above."
}
if (-not $DryRun -and $converted -gt 0) {
Write-Host ""
Write-Host "Conversion completed successfully!" -ForegroundColor Green
if ($Backup) {
Write-Host "Backup files created with .bak extension." -ForegroundColor Yellow
}
}