|
1 | 1 | Function Rename-AndExportFiles {
|
2 | 2 | <#
|
3 | 3 | .SYNOPSIS
|
4 |
| - Renames, formats, and exports file properties information. |
5 |
| -
|
| 4 | + Rename and optionally export files based on specified criteria. |
| 5 | + |
6 | 6 | .DESCRIPTION
|
7 |
| - This function renames files, formats output, and performs other relevant file operations. |
8 |
| -
|
| 7 | + This function renames files based on provided criteria like file extension, strings in the file names, and date attributes. It also optionally exports file details to an output file. |
| 8 | + |
9 | 9 | .PARAMETER FilesPath
|
10 |
| - Mandatory - Path of the folder that contains the files to be processed. |
| 10 | + Mandatory - the path to the folder containing the files to be processed. |
11 | 11 | .PARAMETER FileExtension
|
12 |
| - Not Mandatory - Extension of the files to be processed. If not specified, all files in the folder will be processed. |
| 12 | + Not Mandatory - specifies the file extension to filter files. |
13 | 13 | .PARAMETER RenameStringIn
|
14 |
| - Not Mandatory - A string to search for in file names and replace with the value of the RenameStringOut parameter if found. |
| 14 | + Not Mandatory - the string to find in file names. |
15 | 15 | .PARAMETER RenameStringOut
|
16 |
| - Not Mandatory - The string to replace RenameStringIn if found in file names. |
17 |
| - .PARAMETER CSVFormat |
18 |
| - Not Mandatory - If present, the list of files will be written to the output file in CSV format. |
| 16 | + Not Mandatory - the string to replace in file names. |
| 17 | + .PARAMETER ExportFileDetails |
| 18 | + Not Mandatory - export file details to the output file. |
19 | 19 | .PARAMETER OutFile
|
20 |
| - Mandatory - Name of the file to which the list of files will be written. |
| 20 | + Mandatory - specifies the output file path. |
21 | 21 | .PARAMETER IncludeSubdirectories
|
22 |
| - Not Mandatory - If present, include files from subdirectories. |
| 22 | + Not Mandatory - include subdirectories in the search. |
23 | 23 | .PARAMETER AppendToOutput
|
24 |
| - Not Mandatory - If present, append to the output file. |
| 24 | + Not Mandatory - append to the output file if it exists. |
25 | 25 | .PARAMETER SkipExistingFiles
|
26 |
| - Not Mandatory - If present, skip renaming files if the new name already exists. |
| 26 | + Not Mandatory - skip renaming if the file with the new name already exists. |
27 | 27 | .PARAMETER LogActions
|
28 |
| - Not Mandatory - If present, log renaming actions to a log file. |
| 28 | + Not Mandatory - switch to enable logging actions. |
29 | 29 | .PARAMETER LogFilePath
|
30 |
| - Not Mandatory - Path to the log file if logging is enabled. |
| 30 | + Not Mandatory - specifies the log file path. |
| 31 | + .PARAMETER MinimumFileSize |
| 32 | + Not Mandatory - specifies the minimum file size. |
| 33 | + .PARAMETER CreatedAfter |
| 34 | + Not Mandatory - specifies the minimum creation date for files. |
| 35 | + .PARAMETER ModifiedAfter |
| 36 | + Not Mandatory - specifies the minimum modification date for files. |
| 37 | + .PARAMETER CreateBackup |
| 38 | + Not Mandatory - switch to create a backup of renamed files. |
| 39 | + .PARAMETER LogLevel |
| 40 | + Not Mandatory - log level (Info, Warning, Error). |
31 | 41 |
|
32 | 42 | .EXAMPLE
|
33 |
| - Rename-AndExportFiles -FilesPath "C:\Windows" -OutFile "C:\Temp\results.csv" -CSVFormat -IncludeSubdirectories -LogActions -LogFilePath "C:\Temp\rename_log.txt" |
| 43 | + Rename-AndExportFiles -FilesPath "C:\Data" -FileExtension "txt" -RenameStringIn "_old" -RenameStringOut "_new" -ExportFileDetails -OutFile "C:\Output\output.csv" -LogActions -LogFilePath "C:\Logs\log.txt" |
34 | 44 |
|
35 | 45 | .NOTES
|
36 |
| - Version: 0.3.3 |
| 46 | + v0.3.4 |
37 | 47 | #>
|
38 | 48 | [CmdletBinding()]
|
39 | 49 | param(
|
40 |
| - [Parameter(Mandatory = $true)] |
| 50 | + [Parameter(Mandatory = $true, HelpMessage = "Provide the path to the folder")] |
| 51 | + [ValidateScript({ |
| 52 | + if (-not (Test-Path -Path $_ -PathType Container)) { |
| 53 | + throw "The specified folder does not exist: $_" |
| 54 | + } |
| 55 | + $true |
| 56 | + })] |
41 | 57 | [string]$FilesPath,
|
42 | 58 |
|
43 |
| - [Parameter(Mandatory = $false)] |
44 |
| - [string]$FileExtension = $null, |
| 59 | + [Parameter(Mandatory = $false, HelpMessage = "Specify the file extension")] |
| 60 | + [ValidateNotNullOrEmpty()] |
| 61 | + [string]$FileExtension, |
45 | 62 |
|
46 |
| - [Parameter(Mandatory = $false)] |
47 |
| - [string]$RenameStringIn = $null, |
| 63 | + [Parameter(Mandatory = $false, HelpMessage = "Specify the string to find in file names")] |
| 64 | + [string]$RenameStringIn, |
48 | 65 |
|
49 |
| - [Parameter(Mandatory = $false)] |
50 |
| - [string]$RenameStringOut = $null, |
| 66 | + [Parameter(Mandatory = $false, HelpMessage = "Specify the string to replace in file names")] |
| 67 | + [string]$RenameStringOut, |
51 | 68 |
|
52 |
| - [Parameter(Mandatory = $false)] |
53 |
| - [switch]$CSVFormat, |
| 69 | + [Parameter(Mandatory = $false, HelpMessage = "Export details of files to the output file")] |
| 70 | + [switch]$ExportFileDetails, |
54 | 71 |
|
55 |
| - [Parameter(Mandatory = $true)] |
| 72 | + [Parameter(Mandatory = $true, HelpMessage = "Specify the output file")] |
| 73 | + [ValidateNotNullOrEmpty()] |
56 | 74 | [string]$OutFile,
|
57 | 75 |
|
58 |
| - [Parameter(Mandatory = $false)] |
| 76 | + [Parameter(Mandatory = $false, HelpMessage = "Include subdirectories in the search")] |
59 | 77 | [switch]$IncludeSubdirectories,
|
60 | 78 |
|
61 |
| - [Parameter(Mandatory = $false)] |
| 79 | + [Parameter(Mandatory = $false, HelpMessage = "Append to the output file if it already exists")] |
62 | 80 | [switch]$AppendToOutput,
|
63 | 81 |
|
64 |
| - [Parameter(Mandatory = $false)] |
| 82 | + [Parameter(Mandatory = $false, HelpMessage = "Skip renaming if the file with the new name already exists")] |
65 | 83 | [switch]$SkipExistingFiles,
|
66 | 84 |
|
67 |
| - [Parameter(Mandatory = $false)] |
| 85 | + [Parameter(Mandatory = $false, HelpMessage = "Enable logging actions")] |
68 | 86 | [switch]$LogActions,
|
69 | 87 |
|
70 |
| - [Parameter(Mandatory = $false)] |
71 |
| - [string]$LogFilePath |
| 88 | + [Parameter(Mandatory = $false, HelpMessage = "Specify the log file path")] |
| 89 | + [string]$LogFilePath, |
| 90 | + |
| 91 | + [Parameter(Mandatory = $false, HelpMessage = "Specify the minimum file size")] |
| 92 | + [ValidateRange(0, [int]::MaxValue)] |
| 93 | + [int]$MinimumFileSize, |
| 94 | + |
| 95 | + [Parameter(Mandatory = $false, HelpMessage = "Specify the minimum creation date")] |
| 96 | + [ValidateScript({ |
| 97 | + if ($_ -is [datetime] -and $_ -gt (Get-Date)) { |
| 98 | + throw "The date should be in the past." |
| 99 | + } |
| 100 | + $true |
| 101 | + })] |
| 102 | + [datetime]$CreatedAfter, |
| 103 | + |
| 104 | + [Parameter(Mandatory = $false, HelpMessage = "Specify the minimum modification date")] |
| 105 | + [ValidateScript({ |
| 106 | + if ($_ -is [datetime] -and $_ -gt (Get-Date)) { |
| 107 | + throw "The date should be in the past." |
| 108 | + } |
| 109 | + $true |
| 110 | + })] |
| 111 | + [datetime]$ModifiedAfter, |
| 112 | + |
| 113 | + [Parameter(Mandatory = $false, HelpMessage = "Create a backup of renamed files")] |
| 114 | + [switch]$CreateBackup, |
| 115 | + |
| 116 | + [Parameter(Mandatory = $false, HelpMessage = "Specify the log level")] |
| 117 | + [ValidateSet("Info", "Warning", "Error")] |
| 118 | + [string]$LogLevel = "Info" |
72 | 119 | )
|
73 | 120 | try {
|
74 |
| - if (!(Test-Path -Path $FilesPath -PathType Container)) { |
75 |
| - throw "The specified folder does not exist!" |
76 |
| - } |
77 | 121 | $Filter = "*.*"
|
78 | 122 | if ($FileExtension) {
|
79 | 123 | $Filter = "*.$FileExtension"
|
80 | 124 | }
|
81 |
| - $Files = Get-ChildItem -Path $FilesPath -File -Recurse:$IncludeSubdirectories -Filter $Filter |
| 125 | + $Files = Get-ChildItem -Path $FilesPath -File -Recurse:$IncludeSubdirectories -Filter $Filter | |
| 126 | + Where-Object { |
| 127 | + (!$MinimumFileSize -or $_.Length -ge $MinimumFileSize) -and |
| 128 | + (!$CreatedAfter -or $_.CreationTime -ge $CreatedAfter) -and |
| 129 | + (!$ModifiedAfter -or $_.LastWriteTime -ge $ModifiedAfter) |
| 130 | + } |
82 | 131 | if ($Files.Count -eq 0) {
|
83 |
| - throw "No files found with the specified extension!" |
| 132 | + throw "No files found with the specified criteria in: $FilesPath" |
84 | 133 | }
|
85 | 134 | $LogEntries = @()
|
86 | 135 | foreach ($File in $Files) {
|
87 | 136 | if ($RenameStringIn) {
|
88 | 137 | $NewName = $File.Name -replace $RenameStringIn, $RenameStringOut
|
89 | 138 | 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 |
| - } |
| 139 | + if ($SkipExistingFiles -and (Test-Path (Join-Path $File.DirectoryName $NewName))) { |
| 140 | + $LogEntries += "Skipped renaming $($File.FullName) to $($NewName) (File already exists)." |
| 141 | + continue |
94 | 142 | }
|
95 | 143 | else {
|
96 |
| - Rename-Item -Path $File.FullName -NewName $NewName -Force -Verbose |
97 |
| - if ($LogActions -and $LogFilePath) { |
98 |
| - $LogEntries += "Renamed $($file.FullName) to $($NewName)." |
| 144 | + if ($CreateBackup) { |
| 145 | + $BackupPath = Join-Path $File.DirectoryName "Backup_$($File.Name)" |
| 146 | + Copy-Item -Path $File.FullName -Destination $BackupPath -Verbose |
99 | 147 | }
|
| 148 | + Rename-Item -Path $File.FullName -NewName $NewName -Force -Verbose |
| 149 | + $LogEntries += "Renamed $($File.FullName) to $($NewName)." |
100 | 150 | }
|
101 | 151 | }
|
102 | 152 | }
|
103 |
| - if ($CSVFormat) { |
104 |
| - $File | Export-Csv -Path $OutFile -NoTypeInformation -Append:$AppendToOutput -Force |
| 153 | + if ($ExportFileDetails) { |
| 154 | + $File | Select-Object FullName, Name, Directory, Length, CreationTime, LastWriteTime | Export-Csv -Path $OutFile -NoTypeInformation -Append:$AppendToOutput -Force |
105 | 155 | }
|
106 | 156 | else {
|
107 | 157 | $File.Name | Out-File -FilePath $OutFile -Append:$AppendToOutput -Force
|
108 | 158 | }
|
109 | 159 | }
|
110 | 160 | if ($LogActions -and $LogFilePath -and $LogEntries.Count -gt 0) {
|
111 |
| - $LogEntries | Out-File -FilePath $LogFilePath -Append -Force |
| 161 | + $LogEntries | ForEach-Object { "[$(Get-Date)] [$LogLevel] $_" } | Out-File -FilePath $LogFilePath -Append -Force |
112 | 162 | }
|
113 | 163 | }
|
114 | 164 | catch {
|
115 |
| - Write-Error -Message $_ |
| 165 | + Write-Error -Message "Error: $_" |
116 | 166 | }
|
117 | 167 | }
|
0 commit comments