-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdos-renamer.ps1
96 lines (73 loc) · 2.87 KB
/
dos-renamer.ps1
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
<#
.SYNOPSIS
DOS Filename Renamer Script
.DESCRIPTION
This PowerShell script renames files in a specified directory to be compatible with the DOS 8.3 filename convention.
It traverses the directory recursively and generates DOS-compatible names for each file.
The script supports an optional `--hex` argument to use hexadecimal numbering for filenames that exceed the 8-character limit.
.PARAMETERS
--hex
Optional. If provided, the script will use hexadecimal numbering for filenames that exceed the 8-character limit.
.NOTES
- The script must be run with appropriate permissions to rename files in the specified directory.
- The user is prompted to confirm the proposed changes before the renaming operation is performed.
- The script ignores directories and only processes files.
.EXAMPLE
To run the script and rename files in the current directory:
.\dos-renamer.ps1
To run the script with hexadecimal numbering for long filenames:
.\dos-renamer.ps1 --hex
Follow the prompts to confirm the proposed changes.
#>
$directory_path = Get-Location
$dos_names = @{}
$proposed_changes = @()
function Get-DOSName($name, $counter, $hex) {
$base_name = [System.IO.Path]::GetFileNameWithoutExtension($name)
$extension = [System.IO.Path]::GetExtension($name)
if ($base_name.Length -gt 8) {
if ($hex) {
if ($counter -gt 255) {
$base_name = $base_name.Substring(0, 5) + $counter.ToString("X3")
} else {
$base_name = $base_name.Substring(0, 6) + $counter.ToString("X2")
}
} else {
$base_name = $base_name.Substring(0, 6) + "~" + $counter
}
}
if ($extension.Length -gt 4) {
$extension = $extension.Substring(0, 3)
}
return $base_name + $extension
}
$hex = $false
if ($args -contains "--hex") {
$hex = $true
}
Get-ChildItem -Path $directory_path -Recurse | ForEach-Object {
$original_name = $_.Name
$dos_name = Get-DOSName $original_name 1 $hex
$counter = 2
while ($dos_names.ContainsKey($dos_name)) {
$dos_name = Get-DOSName $original_name $counter $hex
$counter++
}
$dos_names[$dos_name] = $true
$proposed_changes += [PSCustomObject]@{
'Original Name' = $original_name
'DOS Compatible Name' = $dos_name
}
}
$proposed_changes | Format-Table -AutoSize
$user_input = Read-Host -Prompt "Do you want to rename the files as proposed? (Y/N)"
if ($user_input -eq 'Y') {
foreach ($change in $proposed_changes) {
$original_path = Join-Path -Path $directory_path -ChildPath $change.'Original Name'
Rename-Item -Path $original_path -NewName $change.'DOS Compatible Name'
}
}
else {
Write-Output "Exiting the script as per user request."
exit
}