|
| 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 | + |
| 69 | +Function Rename-AndExportFiles { |
| 70 | + <# |
| 71 | + .SYNOPSIS |
| 72 | + Renames, formats, and exports file properties information. |
| 73 | +
|
| 74 | + .DESCRIPTION |
| 75 | + This function renames files, formats output, and performs other relevant file operations. |
| 76 | +
|
| 77 | + .PARAMETER FilesPath |
| 78 | + Mandatory - Path of the folder that contains the files to be processed. |
| 79 | + .PARAMETER FileExtension |
| 80 | + Not Mandatory - Extension of the files to be processed. If not specified, all files in the folder will be processed. |
| 81 | + .PARAMETER RenameStringIn |
| 82 | + Not Mandatory - A string to search for in file names and replace with the value of the RenameStringOut parameter if found. |
| 83 | + .PARAMETER RenameStringOut |
| 84 | + Not Mandatory - The string to replace RenameStringIn if found in file names. |
| 85 | + .PARAMETER CSVFormat |
| 86 | + Not Mandatory - If present, the list of files will be written to the output file in CSV format. |
| 87 | + .PARAMETER OutFile |
| 88 | + Mandatory - Name of the file to which the list of files will be written. |
| 89 | + .PARAMETER IncludeSubdirectories |
| 90 | + Not Mandatory - If present, include files from subdirectories. |
| 91 | + .PARAMETER AppendToOutput |
| 92 | + Not Mandatory - If present, append to the output file. |
| 93 | + .PARAMETER SkipExistingFiles |
| 94 | + Not Mandatory - If present, skip renaming files if the new name already exists. |
| 95 | + .PARAMETER LogActions |
| 96 | + Not Mandatory - If present, log renaming actions to a log file. |
| 97 | + .PARAMETER LogFilePath |
| 98 | + Not Mandatory - Path to the log file if logging is enabled. |
| 99 | +
|
| 100 | + .EXAMPLE |
| 101 | + Rename-AndExportFiles -FilesPath "C:\Windows" -OutFile "C:\Temp\results.csv" -CSVFormat -IncludeSubdirectories -LogActions -LogFilePath "C:\Temp\rename_log.txt" |
| 102 | +
|
| 103 | + .NOTES |
| 104 | + Version: 0.3.2 |
| 105 | + #> |
| 106 | + [CmdletBinding()] |
| 107 | + param( |
| 108 | + [Parameter(Mandatory = $true)] |
| 109 | + [string]$FilesPath, |
| 110 | + |
| 111 | + [Parameter(Mandatory = $false)] |
| 112 | + [string]$FileExtension = $null, |
| 113 | + |
| 114 | + [Parameter(Mandatory = $false)] |
| 115 | + [string]$RenameStringIn = $null, |
| 116 | + |
| 117 | + [Parameter(Mandatory = $false)] |
| 118 | + [string]$RenameStringOut = $null, |
| 119 | + |
| 120 | + [Parameter(Mandatory = $false)] |
| 121 | + [switch]$CSVFormat, |
| 122 | + |
| 123 | + [Parameter(Mandatory = $true)] |
| 124 | + [string]$OutFile, |
| 125 | + |
| 126 | + [Parameter(Mandatory = $false)] |
| 127 | + [switch]$IncludeSubdirectories, |
| 128 | + |
| 129 | + [Parameter(Mandatory = $false)] |
| 130 | + [switch]$AppendToOutput, |
| 131 | + |
| 132 | + [Parameter(Mandatory = $false)] |
| 133 | + [switch]$SkipExistingFiles, |
| 134 | + |
| 135 | + [Parameter(Mandatory = $false)] |
| 136 | + [switch]$LogActions, |
| 137 | + |
| 138 | + [Parameter(Mandatory = $false)] |
| 139 | + [string]$LogFilePath |
| 140 | + ) |
| 141 | + 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 { |
| 157 | + Write-Error -Message $_ |
| 158 | + } |
| 159 | +} |
0 commit comments