Skip to content

Commit ec2cdd6

Browse files
committed
Update Rename-AndExportFiles
enriched params, help messages, fixes and other improvements
1 parent c93819a commit ec2cdd6

File tree

2 files changed

+100
-51
lines changed

2 files changed

+100
-51
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,167 @@
11
Function Rename-AndExportFiles {
22
<#
33
.SYNOPSIS
4-
Renames, formats, and exports file properties information.
5-
4+
Rename and optionally export files based on specified criteria.
5+
66
.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+
99
.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.
1111
.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.
1313
.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.
1515
.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.
1919
.PARAMETER OutFile
20-
Mandatory - Name of the file to which the list of files will be written.
20+
Mandatory - specifies the output file path.
2121
.PARAMETER IncludeSubdirectories
22-
Not Mandatory - If present, include files from subdirectories.
22+
Not Mandatory - include subdirectories in the search.
2323
.PARAMETER AppendToOutput
24-
Not Mandatory - If present, append to the output file.
24+
Not Mandatory - append to the output file if it exists.
2525
.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.
2727
.PARAMETER LogActions
28-
Not Mandatory - If present, log renaming actions to a log file.
28+
Not Mandatory - switch to enable logging actions.
2929
.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).
3141
3242
.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"
3444
3545
.NOTES
36-
Version: 0.3.3
46+
v0.3.4
3747
#>
3848
[CmdletBinding()]
3949
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+
})]
4157
[string]$FilesPath,
4258

43-
[Parameter(Mandatory = $false)]
44-
[string]$FileExtension = $null,
59+
[Parameter(Mandatory = $false, HelpMessage = "Specify the file extension")]
60+
[ValidateNotNullOrEmpty()]
61+
[string]$FileExtension,
4562

46-
[Parameter(Mandatory = $false)]
47-
[string]$RenameStringIn = $null,
63+
[Parameter(Mandatory = $false, HelpMessage = "Specify the string to find in file names")]
64+
[string]$RenameStringIn,
4865

49-
[Parameter(Mandatory = $false)]
50-
[string]$RenameStringOut = $null,
66+
[Parameter(Mandatory = $false, HelpMessage = "Specify the string to replace in file names")]
67+
[string]$RenameStringOut,
5168

52-
[Parameter(Mandatory = $false)]
53-
[switch]$CSVFormat,
69+
[Parameter(Mandatory = $false, HelpMessage = "Export details of files to the output file")]
70+
[switch]$ExportFileDetails,
5471

55-
[Parameter(Mandatory = $true)]
72+
[Parameter(Mandatory = $true, HelpMessage = "Specify the output file")]
73+
[ValidateNotNullOrEmpty()]
5674
[string]$OutFile,
5775

58-
[Parameter(Mandatory = $false)]
76+
[Parameter(Mandatory = $false, HelpMessage = "Include subdirectories in the search")]
5977
[switch]$IncludeSubdirectories,
6078

61-
[Parameter(Mandatory = $false)]
79+
[Parameter(Mandatory = $false, HelpMessage = "Append to the output file if it already exists")]
6280
[switch]$AppendToOutput,
6381

64-
[Parameter(Mandatory = $false)]
82+
[Parameter(Mandatory = $false, HelpMessage = "Skip renaming if the file with the new name already exists")]
6583
[switch]$SkipExistingFiles,
6684

67-
[Parameter(Mandatory = $false)]
85+
[Parameter(Mandatory = $false, HelpMessage = "Enable logging actions")]
6886
[switch]$LogActions,
6987

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"
72119
)
73120
try {
74-
if (!(Test-Path -Path $FilesPath -PathType Container)) {
75-
throw "The specified folder does not exist!"
76-
}
77121
$Filter = "*.*"
78122
if ($FileExtension) {
79123
$Filter = "*.$FileExtension"
80124
}
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+
}
82131
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"
84133
}
85134
$LogEntries = @()
86135
foreach ($File in $Files) {
87136
if ($RenameStringIn) {
88137
$NewName = $File.Name -replace $RenameStringIn, $RenameStringOut
89138
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
94142
}
95143
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
99147
}
148+
Rename-Item -Path $File.FullName -NewName $NewName -Force -Verbose
149+
$LogEntries += "Renamed $($File.FullName) to $($NewName)."
100150
}
101151
}
102152
}
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
105155
}
106156
else {
107157
$File.Name | Out-File -FilePath $OutFile -Append:$AppendToOutput -Force
108158
}
109159
}
110160
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
112162
}
113163
}
114164
catch {
115-
Write-Error -Message $_
165+
Write-Error -Message "Error: $_"
116166
}
117167
}

ps-cryptography/Disable-BitLocker.ps1

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Function Disable-BitLocker {
1313
Disable-BitLocker -MountPoint "C:"
1414
1515
.NOTES
16-
v0.0.1
1716
#>
1817
[CmdletBinding()]
1918
param (

0 commit comments

Comments
 (0)