Skip to content

Commit c93819a

Browse files
committed
Update Rename-AndExportFiles
fix and improve, remove class
1 parent d536e1c commit c93819a

File tree

1 file changed

+42
-84
lines changed

1 file changed

+42
-84
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,3 @@
1-
Class FileOperations {
2-
[string]$FilesPath
3-
[string]$FileExtension
4-
[string]$OutFile
5-
[string]$RenameStringIn
6-
[string]$RenameStringOut
7-
[switch]$CSVFormat
8-
[switch]$IncludeSubdirectories
9-
[switch]$AppendToOutput
10-
[switch]$SkipExistingFiles
11-
[switch]$LogActions
12-
[string]$LogFilePath
13-
FileOperations(
14-
[string]$FilesPath,
15-
[string]$FileExtension,
16-
[string]$OutFile
17-
) {
18-
$this.FilesPath = $FilesPath
19-
$this.FileExtension = $FileExtension
20-
$this.OutFile = $OutFile
21-
}
22-
[void] ProcessFiles() {
23-
try {
24-
if (!(Test-Path -Path $this.FilesPath -PathType Container)) {
25-
throw "The specified folder does not exist."
26-
}
27-
$Filter = "*.*"
28-
if ($this.FileExtension) {
29-
$Filter = "*.$($this.FileExtension)"
30-
}
31-
$Files = Get-ChildItem -Path $this.FilesPath -File -Recurse:$this.IncludeSubdirectories -Filter $Filter
32-
if ($Files.Count -eq 0) {
33-
throw "No files found with the specified extension."
34-
}
35-
if ($this.LogActions -and $this.LogFilePath) {
36-
$LogEntries = @()
37-
}
38-
foreach ($File in $Files) {
39-
if ($this.RenameStringIn) {
40-
$NewName = $File.Name -replace $this.RenameStringIn, $this.RenameStringOut
41-
if ($NewName -ne $File.Name) {
42-
if ($this.SkipExistingFiles -and Test-Path (Join-Path $File.DirectoryName $NewName)) {
43-
if ($this.LogActions -and $this.LogFilePath) {
44-
$LogEntries += "Skipped renaming $($File.FullName) to $($NewName) (File already exists)."
45-
}
46-
} else {
47-
Rename-Item -Path $File.FullName -NewName $NewName -Force
48-
if ($this.LogActions -and $this.LogFilePath) {
49-
$LogEntries += "Renamed $($file.FullName) to $($NewName)."
50-
}
51-
}
52-
}
53-
}
54-
if ($this.CSVFormat) {
55-
$File | Export-Csv -Path $this.OutFile -NoTypeInformation -Append:$this.AppendToOutput -Force
56-
} else {
57-
$File.Name | Out-File -FilePath $this.OutFile -Append:$this.AppendToOutput -Force
58-
}
59-
}
60-
if ($this.LogActions -and $this.LogFilePath -and $LogEntries.Count -gt 0) {
61-
$LogEntries | Out-File -FilePath $this.LogFilePath -Append -Force
62-
}
63-
} catch {
64-
Write-Error -Message $_
65-
}
66-
}
67-
}
68-
691
Function Rename-AndExportFiles {
702
<#
713
.SYNOPSIS
@@ -101,7 +33,7 @@ Function Rename-AndExportFiles {
10133
Rename-AndExportFiles -FilesPath "C:\Windows" -OutFile "C:\Temp\results.csv" -CSVFormat -IncludeSubdirectories -LogActions -LogFilePath "C:\Temp\rename_log.txt"
10234
10335
.NOTES
104-
Version: 0.3.2
36+
Version: 0.3.3
10537
#>
10638
[CmdletBinding()]
10739
param(
@@ -139,21 +71,47 @@ Function Rename-AndExportFiles {
13971
[string]$LogFilePath
14072
)
14173
try {
142-
$FileProcessor = [FileOperations]::new(
143-
$FilesPath,
144-
$FileExtension,
145-
$OutFile
146-
)
147-
$FileProcessor.RenameStringIn = $RenameStringIn
148-
$FileProcessor.RenameStringOut = $RenameStringOut
149-
$FileProcessor.CSVFormat = $CSVFormat
150-
$FileProcessor.IncludeSubdirectories = $IncludeSubdirectories
151-
$FileProcessor.AppendToOutput = $AppendToOutput
152-
$FileProcessor.SkipExistingFiles = $SkipExistingFiles
153-
$FileProcessor.LogActions = $LogActions
154-
$FileProcessor.LogFilePath = $LogFilePath
155-
$FileProcessor.ProcessFiles()
156-
} catch {
74+
if (!(Test-Path -Path $FilesPath -PathType Container)) {
75+
throw "The specified folder does not exist!"
76+
}
77+
$Filter = "*.*"
78+
if ($FileExtension) {
79+
$Filter = "*.$FileExtension"
80+
}
81+
$Files = Get-ChildItem -Path $FilesPath -File -Recurse:$IncludeSubdirectories -Filter $Filter
82+
if ($Files.Count -eq 0) {
83+
throw "No files found with the specified extension!"
84+
}
85+
$LogEntries = @()
86+
foreach ($File in $Files) {
87+
if ($RenameStringIn) {
88+
$NewName = $File.Name -replace $RenameStringIn, $RenameStringOut
89+
if ($NewName -ne $File.Name) {
90+
if (($SkipExistingFiles) -and (Test-Path (Join-Path $File.DirectoryName $NewName))) {
91+
if ($LogActions -and $LogFilePath) {
92+
$LogEntries += "Skipped renaming $($File.FullName) to $($NewName) (File already exists)."
93+
}
94+
}
95+
else {
96+
Rename-Item -Path $File.FullName -NewName $NewName -Force -Verbose
97+
if ($LogActions -and $LogFilePath) {
98+
$LogEntries += "Renamed $($file.FullName) to $($NewName)."
99+
}
100+
}
101+
}
102+
}
103+
if ($CSVFormat) {
104+
$File | Export-Csv -Path $OutFile -NoTypeInformation -Append:$AppendToOutput -Force
105+
}
106+
else {
107+
$File.Name | Out-File -FilePath $OutFile -Append:$AppendToOutput -Force
108+
}
109+
}
110+
if ($LogActions -and $LogFilePath -and $LogEntries.Count -gt 0) {
111+
$LogEntries | Out-File -FilePath $LogFilePath -Append -Force
112+
}
113+
}
114+
catch {
157115
Write-Error -Message $_
158116
}
159117
}

0 commit comments

Comments
 (0)