-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRename-File.ps1
More file actions
1306 lines (1092 loc) · 48.2 KB
/
Copy pathRename-File.ps1
File metadata and controls
1306 lines (1092 loc) · 48.2 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function Rename-File
{
<#
.SYNOPSIS
Renames files with advanced transformation options including case conversion, normalization,
pattern replacement, and batch numbering.
.DESCRIPTION
This function provides a comprehensive file renaming solution with multiple transformation
options that can be combined. It supports:
- Case conversion (uppercase, lowercase, title case, camel case, pascal case)
- Normalization (remove accents, control characters, shell meta-characters)
- Text operations (append, prepend, trim, substitute, replace)
- Whitespace handling (replace with underscores, dashes, or custom characters)
- URL decoding
- Extension management (keep, remove, replace)
- Expression-based transformations using PowerShell script blocks
- Counter-based batch renaming with custom formatting
- Conflict resolution with automatic suffixing
All transformations are applied in a specific order to ensure predictable results.
Use -DryRun to safely preview what files will be renamed before executing actual changes.
.PARAMETER Path
The path to the file(s) to rename. Accepts wildcards and pipeline input.
Can be a single file path, multiple paths, or a directory. If omitted, the
current working directory is used. Use -Recurse to include subdirectories.
.PARAMETER NewName
The new name for the file. If not specified, transformations are applied to the current name.
Can be used with counter formatting (e.g., 'file_{0:D3}.txt' for file_001.txt).
.PARAMETER LiteralPath
The literal path to the file(s) to rename. Unlike Path, this does not interpret wildcards.
.PARAMETER Normalize
Normalizes the filename by removing accents, converting to basic ASCII characters,
and removing common problematic characters. Also trims leading and trailing whitespace.
Useful for cross-platform compatibility.
.PARAMETER ToUpper
Converts the filename to UPPERCASE.
.PARAMETER ToLower
Converts the filename to lowercase.
.PARAMETER ToTitleCase
Converts the filename to Title Case (first letter of each word capitalized).
.PARAMETER ToCamelCase
Converts the filename to camelCase (first letter lowercase, subsequent words capitalized).
.PARAMETER ToPascalCase
Converts the filename to PascalCase (first letter of each word capitalized, no spaces).
.PARAMETER Trim
Trims leading and trailing whitespace from the filename.
.PARAMETER TrimStart
Trims leading whitespace from the filename.
.PARAMETER TrimEnd
Trims trailing whitespace from the filename.
.PARAMETER Append
Text to append to the filename (before the extension).
.PARAMETER Prepend
Text to prepend to the filename.
.PARAMETER UrlDecode
URL-decodes the filename, converting %20 to spaces, %2B to +, etc.
.PARAMETER ReplaceSpacesWith
Replaces all spaces in the filename with the specified character(s).
Common values: '_' (underscore), '-' (dash), '.' (dot).
.PARAMETER ReplaceUnderscoresWith
Replaces all underscores in the filename with the specified character(s).
Useful for converting back to spaces or other delimiters.
.PARAMETER ReplaceDashesWith
Replaces all dashes/hyphens in the filename with the specified character(s).
.PARAMETER RemoveControlCharacters
Removes all control characters (ASCII 0-31 and 127) from the filename.
.PARAMETER RemoveShellMetaCharacters
Removes shell meta-characters that may cause issues in command-line environments.
Removes: & | ; < > ( ) $ ` \ " ' * ? [ ] # ~ = %
.PARAMETER SanitizeForCrossPlatform
Sanitizes the filename for cross-platform compatibility by removing/replacing
characters that are problematic on Windows, macOS, or Linux.
.PARAMETER RemoveExtension
Removes the file extension from the filename.
.PARAMETER NewExtension
Changes the file extension to the specified value (include the dot, e.g., '.txt').
.PARAMETER Replace
A hashtable of old/new string pairs to replace in the filename.
Example: @{ 'old' = 'new'; 'foo' = 'bar' }
.PARAMETER RegexReplace
A hashtable of regex pattern/replacement pairs to apply to the filename.
Example: @{ '\d+' = 'NUM'; '\s+' = '_' }
.PARAMETER Expression
A script block that receives the filename (without extension) as $_ and returns
the modified filename. Example: { $_.Substring(0, 10) }
.PARAMETER Counter
Adds a counter to the filename. Use with -CounterFormat to customize formatting.
The counter is applied when processing multiple files.
.PARAMETER CounterFormat
Format string for the counter. Default is 'D3' (3-digit with leading zeros).
Examples: 'D2' (01, 02), 'D4' (0001), 'X' (hexadecimal), 'N0' (no decimals).
.PARAMETER CounterStart
Starting number for the counter. Default is 1.
.PARAMETER CounterPosition
Position where the counter should be inserted.
Valid values: 'End' (default), 'Start', 'BeforeExtension'.
.PARAMETER Recurse
Recursively processes files in subdirectories when Path is a directory.
.PARAMETER Force
Forces the rename even if the target file already exists (overwriting the target).
By default, conflicts are resolved by adding a numeric suffix.
.PARAMETER WhatIf
Shows what would happen if the cmdlet runs without actually performing the rename.
.PARAMETER DryRun
Shows what would be renamed without actually performing the rename operations.
Uses internal preview logic to display exact output, that otherwise couldn't be accomplished with built-in -WhatIf support.
Note that you -WhatIf is an alias for -DryRun in this function.
.PARAMETER PassThru
Returns FileInfo objects for the renamed files.
.EXAMPLE
PS > Rename-File -Path 'My Document.txt' -ReplaceSpacesWith '_'
Renames 'My Document.txt' to 'My_Document.txt'.
.EXAMPLE
PS > Rename-File -Path 'FILE.TXT' -ToLower
Renames 'FILE.TXT' to 'file.txt'.
.EXAMPLE
PS > Get-ChildItem '*.jpg' | Rename-File -Prepend '2024-' -Counter -CounterFormat 'D3'
Renames all .jpg files to: 2024-001.jpg, 2024-002.jpg, 2024-003.jpg, etc.
.EXAMPLE
PS > Rename-File -Path 'document%20file.txt' -UrlDecode -ToLower
URL-decodes and converts to lowercase: 'document file.txt'.
.EXAMPLE
PS > Rename-File -Path 'file.dat' -NewExtension '.txt'
Changes the extension from .dat to .txt: 'file.txt'.
.EXAMPLE
PS > Get-ChildItem '*.txt' | Rename-File -RemoveShellMetaCharacters -Normalize
Sanitizes all .txt files by removing shell meta-characters and normalizing.
.EXAMPLE
PS > Rename-File -Path 'test_file.txt' -ReplaceUnderscoresWith ' ' -ToTitleCase
Converts 'test_file.txt' to 'Test File.txt'.
.EXAMPLE
PS > Rename-File -Path '*.log' -Expression { "backup_$_" } -Append '_2024'
Prepends 'backup_' and appends '_2024' to all .log files.
.EXAMPLE
PS > Rename-File -Path 'file.txt' -Replace @{ 'old' = 'new'; 'test' = 'prod' }
Replaces 'old' with 'new' and 'test' with 'prod' in the filename.
.EXAMPLE
PS > Get-ChildItem 'C:\Photos' -Recurse | Rename-File -SanitizeForCrossPlatform -Counter
Recursively sanitizes all filenames for cross-platform compatibility and adds counters.
.EXAMPLE
PS > Rename-File -Path 'IMG_*.jpg' -NewName 'photo_{0:D4}.jpg' -Counter
Renames IMG_*.jpg files to photo_0001.jpg, photo_0002.jpg, etc.
.EXAMPLE
PS > Rename-File -Path 'data.txt' -ToPascalCase -RemoveExtension
Converts 'data.txt' to 'Data' (removes extension).
.EXAMPLE
PS > Rename-File -Path '*.tmp' -RegexReplace @{ '\d{4}-\d{2}-\d{2}' = 'DATE' }
Replaces date patterns (YYYY-MM-DD) with 'DATE' in all .tmp files.
.EXAMPLE
PS > Rename-File -Path 'C:\MyFiles' -Recurse -DryRun -RemoveShellMetaCharacters
Shows a preview of what would be renamed when removing shell meta-characters from all files
in C:\MyFiles and subdirectories, without actually performing any renames.
.OUTPUTS
None by default.
[System.IO.FileInfo] when PassThru is specified.
.NOTES
Transformation Order:
1. URL decode (if specified)
2. Expression (if specified)
3. Replace operations (Replace, RegexReplace)
4. Normalization
5. Remove control/shell characters
6. Case conversion
7. Whitespace replacements
8. Trim operations (explicit or implied by -Normalize)
9. Prepend/Append
10. Counter (if specified)
11. Extension handling
Conflict Resolution:
If a file with the new name already exists and -Force is not specified,
a numeric suffix is automatically added (e.g., filename_2.txt).
Cross-Platform Compatibility:
The function works on Windows, macOS, and Linux. Use -SanitizeForCrossPlatform
to ensure filenames are compatible across all platforms.
Author: Jon LaBelle
License: MIT
Source: https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/Utilities/Rename-File.ps1
.LINK
https://jonlabelle.com/snippets/view/powershell/rename-files-with-advanced-options
.LINK
https://github.com/jonlabelle/pwsh-profile/blob/main/Functions/Utilities/Rename-File.ps1
#>
[CmdletBinding(DefaultParameterSetName = 'Path')]
[OutputType([System.IO.FileInfo])]
param(
[Parameter(ValueFromPipeline, ValueFromPipelineByPropertyName, ParameterSetName = 'Path', Position = 0)]
[SupportsWildcards()]
[String[]]$Path,
[Parameter(Mandatory, ValueFromPipelineByPropertyName, ParameterSetName = 'LiteralPath')]
[Alias('PSPath')]
[String[]]$LiteralPath,
[Parameter()]
[String]$NewName,
[Parameter()]
[Switch]$Normalize,
[Parameter()]
[Switch]$ToUpper,
[Parameter()]
[Switch]$ToLower,
[Parameter()]
[Switch]$ToTitleCase,
[Parameter()]
[Switch]$ToCamelCase,
[Parameter()]
[Switch]$ToPascalCase,
[Parameter()]
[Switch]$Trim,
[Parameter()]
[Switch]$TrimStart,
[Parameter()]
[Switch]$TrimEnd,
[Parameter()]
[String]$Append,
[Parameter()]
[String]$Prepend,
[Parameter()]
[Switch]$UrlDecode,
[Parameter()]
[String]$ReplaceSpacesWith,
[Parameter()]
[String]$ReplaceUnderscoresWith,
[Parameter()]
[String]$ReplaceDashesWith,
[Parameter()]
[Switch]$RemoveControlCharacters,
[Parameter()]
[Switch]$RemoveShellMetaCharacters,
[Parameter()]
[Switch]$SanitizeForCrossPlatform,
[Parameter()]
[Switch]$RemoveExtension,
[Parameter()]
[ValidatePattern('^\..+')]
[String]$NewExtension,
[Parameter()]
[Hashtable]$Replace,
[Parameter()]
[Hashtable]$RegexReplace,
[Parameter()]
[ScriptBlock]$Expression,
[Parameter()]
[Switch]$Counter,
[Parameter()]
[String]$CounterFormat = 'D3',
[Parameter()]
[Int]$CounterStart = 1,
[Parameter()]
[ValidateSet('End', 'Start', 'BeforeExtension')]
[String]$CounterPosition = 'BeforeExtension',
[Parameter()]
[Switch]$Recurse,
[Parameter()]
[Switch]$Force,
[Parameter()]
[Alias('WhatIf')]
[Switch]$DryRun,
[Parameter()]
[Switch]$PassThru
)
begin
{
$currentCounter = $CounterStart
$fileCount = 0
$processedPaths = New-Object 'System.Collections.Generic.HashSet[string]' ([System.StringComparer]::OrdinalIgnoreCase)
# Collect all files from pipeline before processing to prevent PassThru output from re-entering
$allFilesToProcess = [System.Collections.ArrayList]::new()
function Get-NormalizedString
{
param([String]$Text)
if ([String]::IsNullOrEmpty($Text))
{
return $Text
}
# Fix common UTF-8 mojibake sequences FIRST before any other normalization
# When UTF-8 bytes are interpreted as ISO-8859-1, we get mojibake sequences
# We construct these strings from escape sequences for maximum compatibility
$normalized = $Text
if (-not $script:RenameFileMojibakeReplacements)
{
$script:RenameFileMojibakeReplacements = @{}
}
$mojibakeReplacements = $script:RenameFileMojibakeReplacements
if ($mojibakeReplacements.Count -eq 0)
{
# Lowercase vowels with diacritics
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xA1)] = 'a' # á bytes C3 A1
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xA0)] = 'a' # à bytes C3 A0
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xA4)] = 'a' # ä bytes C3 A4
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xA2)] = 'a' # â bytes C3 A2
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xA3)] = 'a' # ã bytes C3 A3
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xA5)] = 'a' # å bytes C3 A5
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xA9)] = 'e' # é bytes C3 A9
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xA8)] = 'e' # è bytes C3 A8
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xAB)] = 'e' # ë bytes C3 AB
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xAA)] = 'e' # ê bytes C3 AA
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xAD)] = 'i' # í bytes C3 AD
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xAC)] = 'i' # ì bytes C3 AC
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xAF)] = 'i' # ï bytes C3 AF
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xAE)] = 'i' # î bytes C3 AE
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xB3)] = 'o' # ó bytes C3 B3
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xB2)] = 'o' # ò bytes C3 B2
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xB6)] = 'o' # ö bytes C3 B6
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xB4)] = 'o' # ô bytes C3 B4
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xB5)] = 'o' # õ bytes C3 B5
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xB8)] = 'o' # ø bytes C3 B8
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xBA)] = 'u' # ú bytes C3 BA
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xB9)] = 'u' # ù bytes C3 B9
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xBC)] = 'u' # ü bytes C3 BC
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xBB)] = 'u' # û bytes C3 BB
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xB1)] = 'n' # ñ bytes C3 B1
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xA7)] = 'c' # ç bytes C3 A7
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xBD)] = 'y' # ý bytes C3 BD
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0xBF)] = 'y' # ÿ bytes C3 BF
# Uppercase vowels with diacritics
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x80)] = 'A' # À bytes C3 80
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x84)] = 'A' # Ä bytes C3 84
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x82)] = 'A' # Â bytes C3 82
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x83)] = 'A' # Ã bytes C3 83
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x85)] = 'A' # Å bytes C3 85
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x89)] = 'E' # É bytes C3 89
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x88)] = 'E' # È bytes C3 88
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x8B)] = 'E' # Ë bytes C3 8B
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x8A)] = 'E' # Ê bytes C3 8A
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x8D)] = 'I' # Í bytes C3 8D
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x8C)] = 'I' # Ì bytes C3 8C
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x8F)] = 'I' # Ï bytes C3 8F
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x8E)] = 'I' # Î bytes C3 8E
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x93)] = 'O' # Ó bytes C3 93
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x92)] = 'O' # Ò bytes C3 92
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x96)] = 'O' # Ö bytes C3 96
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x94)] = 'O' # Ô bytes C3 94
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x95)] = 'O' # Õ bytes C3 95
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x98)] = 'O' # Ø bytes C3 98
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x9A)] = 'U' # Ú bytes C3 9A
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x99)] = 'U' # Ù bytes C3 99
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x9C)] = 'U' # Ü bytes C3 9C
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x9B)] = 'U' # Û bytes C3 9B
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x91)] = 'N' # Ñ bytes C3 91
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x87)] = 'C' # Ç bytes C3 87
$mojibakeReplacements[[string]([char]0xC3) + [string]([char]0x9D)] = 'Y' # Ý bytes C3 9D
}
foreach ($key in $mojibakeReplacements.Keys)
{
$normalized = $normalized -replace [regex]::Escape($key), $mojibakeReplacements[$key]
}
try
{
# Try NFD normalization to decompose accents
$normalized = $normalized.Normalize([System.Text.NormalizationForm]::FormD)
}
catch
{
# Fallback for PowerShell 5.1 or if Normalize method fails
# Use a character-by-character replacement table for common accented characters
if (-not $script:RenameFileFallbackReplacements)
{
$script:RenameFileFallbackReplacements = @{}
}
$replacements = $script:RenameFileFallbackReplacements
if ($replacements.Count -eq 0)
{
# Lowercase a variants
$replacements['á'] = 'a'
$replacements['à'] = 'a'
$replacements['ä'] = 'a'
$replacements['â'] = 'a'
$replacements['ã'] = 'a'
$replacements['å'] = 'a'
$replacements['ą'] = 'a'
# Lowercase e variants
$replacements['é'] = 'e'
$replacements['è'] = 'e'
$replacements['ë'] = 'e'
$replacements['ê'] = 'e'
$replacements['ę'] = 'e'
# Lowercase i variants
$replacements['í'] = 'i'
$replacements['ì'] = 'i'
$replacements['ï'] = 'i'
$replacements['î'] = 'i'
$replacements['į'] = 'i'
# Lowercase o variants
$replacements['ó'] = 'o'
$replacements['ò'] = 'o'
$replacements['ö'] = 'o'
$replacements['ô'] = 'o'
$replacements['õ'] = 'o'
$replacements['ø'] = 'o'
$replacements['ǿ'] = 'o'
# Lowercase u variants
$replacements['ú'] = 'u'
$replacements['ù'] = 'u'
$replacements['ü'] = 'u'
$replacements['û'] = 'u'
$replacements['ų'] = 'u'
# Lowercase y variants
$replacements['ý'] = 'y'
$replacements['ỳ'] = 'y'
$replacements['ÿ'] = 'y'
$replacements['ŷ'] = 'y'
# Lowercase c variants
$replacements['ć'] = 'c'
$replacements['č'] = 'c'
$replacements['ċ'] = 'c'
$replacements['ç'] = 'c'
# Lowercase d variants
$replacements['ď'] = 'd'
$replacements['đ'] = 'd'
# Lowercase g variants
$replacements['ğ'] = 'g'
$replacements['ģ'] = 'g'
# Other lowercase variants
$replacements['ħ'] = 'h'
$replacements['ĵ'] = 'j'
$replacements['ķ'] = 'k'
$replacements['ļ'] = 'l'
$replacements['ł'] = 'l'
$replacements['ń'] = 'n'
$replacements['ň'] = 'n'
$replacements['ņ'] = 'n'
$replacements['ñ'] = 'n'
$replacements['ŕ'] = 'r'
$replacements['ř'] = 'r'
$replacements['ŗ'] = 'r'
$replacements['ś'] = 's'
$replacements['š'] = 's'
$replacements['ş'] = 's'
$replacements['ť'] = 't'
$replacements['ţ'] = 't'
$replacements['ź'] = 'z'
$replacements['ž'] = 'z'
$replacements['ż'] = 'z'
# Uppercase variants
$replacements['Á'] = 'A'
$replacements['À'] = 'A'
$replacements['Ä'] = 'A'
$replacements['Â'] = 'A'
$replacements['Ã'] = 'A'
$replacements['Å'] = 'A'
$replacements['É'] = 'E'
$replacements['È'] = 'E'
$replacements['Ë'] = 'E'
$replacements['Ê'] = 'E'
$replacements['Í'] = 'I'
$replacements['Ì'] = 'I'
$replacements['Ï'] = 'I'
$replacements['Î'] = 'I'
$replacements['Ó'] = 'O'
$replacements['Ò'] = 'O'
$replacements['Ö'] = 'O'
$replacements['Ô'] = 'O'
$replacements['Õ'] = 'O'
$replacements['Ø'] = 'O'
$replacements['Ú'] = 'U'
$replacements['Ù'] = 'U'
$replacements['Ü'] = 'U'
$replacements['Û'] = 'U'
$replacements['Ý'] = 'Y'
$replacements['Ÿ'] = 'Y'
$replacements['Ç'] = 'C'
$replacements['Ñ'] = 'N'
}
foreach ($key in $replacements.Keys)
{
$normalized = $normalized -replace [regex]::Escape($key), $replacements[$key]
}
}
# Remove diacritical marks (combining characters)
$sb = New-Object System.Text.StringBuilder
foreach ($char in $normalized.ToCharArray())
{
$category = [System.Globalization.CharUnicodeInfo]::GetUnicodeCategory($char)
if ($category -ne [System.Globalization.UnicodeCategory]::NonSpacingMark)
{
$null = $sb.Append($char)
}
}
# Normalize back to FormC (composed form)
try
{
return $sb.ToString().Normalize([System.Text.NormalizationForm]::FormC)
}
catch
{
return $sb.ToString()
}
}
function ConvertTo-TitleCase
{
param([String]$Text)
if ([String]::IsNullOrEmpty($Text))
{
return $Text
}
$textInfo = (Get-Culture).TextInfo
return $textInfo.ToTitleCase($Text.ToLower())
}
function ConvertTo-CamelCase
{
param([String]$Text)
if ([String]::IsNullOrEmpty($Text))
{
return $Text
}
# Split on non-alphanumeric characters and spaces
$words = $Text -split '[^a-zA-Z0-9]+'
$result = New-Object System.Text.StringBuilder
for ($i = 0; $i -lt $words.Count; $i++)
{
$word = $words[$i]
if ([String]::IsNullOrWhiteSpace($word))
{
continue
}
if ($i -eq 0)
{
# First word: lowercase first letter
$null = $result.Append($word.Substring(0, 1).ToLower())
if ($word.Length -gt 1)
{
$null = $result.Append($word.Substring(1).ToLower())
}
}
else
{
# Subsequent words: uppercase first letter
$null = $result.Append($word.Substring(0, 1).ToUpper())
if ($word.Length -gt 1)
{
$null = $result.Append($word.Substring(1).ToLower())
}
}
}
return $result.ToString()
}
function ConvertTo-PascalCase
{
param([String]$Text)
if ([String]::IsNullOrEmpty($Text))
{
return $Text
}
# Split on non-alphanumeric characters and spaces
$words = $Text -split '[^a-zA-Z0-9]+'
$result = New-Object System.Text.StringBuilder
foreach ($word in $words)
{
if ([String]::IsNullOrWhiteSpace($word))
{
continue
}
# Uppercase first letter, lowercase the rest
$null = $result.Append($word.Substring(0, 1).ToUpper())
if ($word.Length -gt 1)
{
$null = $result.Append($word.Substring(1).ToLower())
}
}
return $result.ToString()
}
function Get-StringWithoutControlCharacter
{
param([String]$Text)
if ([String]::IsNullOrEmpty($Text))
{
return $Text
}
# Remove ASCII control characters (0-31 and 127)
return [regex]::Replace($Text, '[\x00-\x1F\x7F]', '')
}
function Get-StringWithoutShellMetaCharacter
{
param([String]$Text)
if ([String]::IsNullOrEmpty($Text))
{
return $Text
}
# Remove common shell meta-characters
# Characters: & | ; < > ( ) $ ` \ " ' * ? [ ] # ~ = %
$result = $Text
# Use individual replacements for maximum compatibility across PowerShell versions
$result = $result -replace '\&', ''
$result = $result -replace '\|', ''
$result = $result -replace '\;', ''
$result = $result -replace '[<>]', ''
$result = $result -replace '[\(\)]', ''
$result = $result -replace '\$', ''
$result = $result -replace '`', ''
$result = $result -replace '\\', ''
$result = $result -replace '"', ''
$result = $result -replace "'", ''
$result = $result -replace '\*', ''
$result = $result -replace '\?', ''
$result = $result -replace '[\[\]]', ''
$result = $result -replace '#', ''
$result = $result -replace '~', ''
$result = $result -replace '=', ''
$result = $result -replace '%', ''
return $result
}
function Get-SanitizedFilename
{
param([String]$Filename)
if ([String]::IsNullOrEmpty($Filename))
{
return $Filename
}
# Remove or replace characters that are problematic on Windows, macOS, or Linux
# Windows: < > : " / \ | ? *
# macOS/Linux: / (and null character)
# Also remove leading/trailing dots and spaces (problematic on Windows)
$sanitized = $Filename
$sanitized = $sanitized -replace '[<>:"/\\|?*]', '_'
$sanitized = $sanitized -replace '[\x00]', ''
$sanitized = $sanitized.Trim('. ')
return $sanitized
}
function Get-UrlDecodedString
{
param([String]$Text)
if ([String]::IsNullOrEmpty($Text))
{
return $Text
}
try
{
# Use .NET's UrlDecode
return [System.Web.HttpUtility]::UrlDecode($Text)
}
catch
{
# If System.Web is not available, do basic replacements
$result = $Text
$result = $result -replace '%20', ' '
$result = $result -replace '%21', '!'
$result = $result -replace '%22', '"'
$result = $result -replace '%23', '#'
$result = $result -replace '%24', '$'
$result = $result -replace '%25', '%'
$result = $result -replace '%26', '&'
$result = $result -replace '%27', "'"
$result = $result -replace '%28', '('
$result = $result -replace '%29', ')'
$result = $result -replace '%2A', '*'
$result = $result -replace '%2B', '+'
$result = $result -replace '%2C', ','
$result = $result -replace '%2D', '-'
$result = $result -replace '%2E', '.'
$result = $result -replace '%2F', '/'
return $result
}
}
function Get-UniqueFilename
{
param(
[String]$Directory,
[String]$BaseName,
[String]$FileExtension
)
# Use local variable with different name to avoid scope contamination
$ext = $FileExtension
$targetPath = Join-Path -Path $Directory -ChildPath ($BaseName + $ext)
$suffix = 2
while (Test-Path -LiteralPath $targetPath)
{
$newBaseName = "${BaseName}_${suffix}"
$targetPath = Join-Path -Path $Directory -ChildPath ($newBaseName + $ext)
$suffix++
}
return Split-Path -Leaf $targetPath
}
function Get-TransformedFilename
{
param(
[String]$OriginalName,
[String]$FileExt,
[Int]$CounterValue
)
# Use different parameter name (FileExt instead of Extension) to avoid any scope collision
$result = $OriginalName
$ext = $FileExt
# 1. URL decode (if specified)
if ($UrlDecode)
{
$result = Get-UrlDecodedString -Text $result
Write-Verbose "After URL decode: '$result'"
}
# 2. Expression (if specified)
if ($Expression)
{
try
{
# Use ForEach-Object to properly set $_ variable for the script block
$newResult = $result | ForEach-Object -Process $Expression
if (-not [String]::IsNullOrEmpty($newResult))
{
$result = $newResult
Write-Verbose "After expression: '$result'"
}
else
{
Write-Warning "Expression returned null or empty value for: $result"
}
}
catch
{
Write-Warning "Expression evaluation failed: $($_.Exception.Message)"
}
}
# 3. Replace operations
if ($Replace)
{
foreach ($key in $Replace.Keys)
{
$result = $result -replace [regex]::Escape($key), $Replace[$key]
}
Write-Verbose "After replace: '$result'"
}
if ($RegexReplace)
{
foreach ($pattern in $RegexReplace.Keys)
{
$result = $result -replace $pattern, $RegexReplace[$pattern]
}
Write-Verbose "After regex replace: '$result'"
}
# 4. Normalization
if ($Normalize)
{
$result = Get-NormalizedString -Text $result
Write-Verbose "After normalization: '$result'"
}
# 5. Remove control/shell characters
if ($RemoveControlCharacters)
{
$result = Get-StringWithoutControlCharacter -Text $result
Write-Verbose "After removing control characters: '$result'"
}
if ($RemoveShellMetaCharacters)
{
$result = Get-StringWithoutShellMetaCharacter -Text $result
Write-Verbose "After removing shell meta-characters: '$result'"
}
if ($SanitizeForCrossPlatform)
{
$result = Get-SanitizedFilename -Filename $result
Write-Verbose "After cross-platform sanitization: '$result'"
}
# 6. Case conversion (only one should be applied)
if ($ToUpper)
{
$result = $result.ToUpper()
Write-Verbose "After ToUpper: '$result'"
}
elseif ($ToLower)
{
$result = $result.ToLower()
Write-Verbose "After ToLower: '$result'"
}
elseif ($ToTitleCase)
{
$result = ConvertTo-TitleCase -Text $result
Write-Verbose "After ToTitleCase: '$result'"
}
elseif ($ToCamelCase)
{
$result = ConvertTo-CamelCase -Text $result
Write-Verbose "After ToCamelCase: '$result'"
}
elseif ($ToPascalCase)
{
$result = ConvertTo-PascalCase -Text $result
Write-Verbose "After ToPascalCase: '$result'"
}
# 7. Whitespace replacements
if ($ReplaceSpacesWith)
{
$result = $result -replace ' ', $ReplaceSpacesWith
Write-Verbose "After replacing spaces: '$result'"
}
if ($ReplaceUnderscoresWith)
{
$result = $result -replace '_', $ReplaceUnderscoresWith
Write-Verbose "After replacing underscores: '$result'"
}
if ($ReplaceDashesWith)
{
$result = $result -replace '-', $ReplaceDashesWith
Write-Verbose "After replacing dashes: '$result'"
}
# 8. Trim operations
if ($Trim -or $Normalize)
{
$result = $result.Trim()
Write-Verbose "After trim: '$result'"
}
elseif ($TrimStart)
{
$result = $result.TrimStart()
Write-Verbose "After trim start: '$result'"
}
elseif ($TrimEnd)
{
$result = $result.TrimEnd()
Write-Verbose "After trim end: '$result'"
}
# 9. Prepend/Append
if ($Prepend)
{
$result = $Prepend + $result
Write-Verbose "After prepend: '$result'"
}