-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake-draft.ps1
More file actions
124 lines (106 loc) · 3.93 KB
/
make-draft.ps1
File metadata and controls
124 lines (106 loc) · 3.93 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
<#
.SYNOPSIS
Renames files in a specified folder (or a single file) by adding the current date to the beginning of the filename, unless the filename already starts with the date.
.DESCRIPTION
This script takes a file or folder path as input. It then iterates through the files
in the specified folder (or renames the single file) and renames them by adding the
current date in "yyyy-MM-dd-" format to the beginning of the original filename.
If a file already starts with the date in the specified format, it is skipped.
.PARAMETER Path
The path to the file or folder to process. If a folder is specified, all files
in that folder will be renamed.
.EXAMPLE
.\make-draft.ps1 -Path "C:\MyFolder"
Renames all files in the "C:\MyFolder" directory, unless they already start with the date.
For example, "image.jpg" becomes "2024-01-15-image.jpg", but "2024-01-15-image.jpg" is not changed.
.EXAMPLE
.\make-draft.ps1 -Path "C:\MyFolder\document.txt"
Renames the single file "C:\MyFolder\document.txt" to "2024-01-15-document.txt", unless it already starts with the date.
.EXAMPLE
.\make-draft.ps1 -Path "C:\MyFolder\document.txt" -Remove
Renames the single file "C:\MyFolder\2024-01-15-document.txt" to "document.txt", unless it already starts with the date.
.INPUTS
String. The path to the file or folder.
.OUTPUTS
None. The script renames files in place.
.NOTES
Requires PowerShell v3.0 or later.
#>
param(
[Parameter(Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true,
Position = 0)]
[Alias("PSPath")]
[string]$Path,
[Parameter(Mandatory = $false)]
[switch]$Remove
)
begin {
# Get the current date in YYYY-MM-DD format
$currentDate = Get-Date -Format "yyyy-MM-dd"
# Function to rename a file
function Rename-File {
param(
[Parameter(Mandatory = $true)]
[System.IO.FileInfo]$File,
[Parameter(Mandatory = $true)]
[string]$NewDate
)
$ShouldRename = $false
if ($Remove) {
# Remove the date if present
if ($file.Name -match "^(\d{4}-\d{2}-\d{2}-)(.+)") {
$newFileName = $matches[2] # Get the part after the date
$ShouldRename = $true
}
else {
Write-Verbose "Skipped '$($file.FullName)', does not start with a date."
}
}
else {
if ($File.Name -notmatch "^\d{4}-\d{2}-\d{2}-") {
$newFileName = "$NewDate-$($File.Name)"
$ShouldRename = $true
}
else {
Write-Host "Skipped '$($file.FullName)', already starts with date."
}
}
if ($ShouldRename) {
try {
Rename-Item -Path $File.FullName -NewName $newFileName -Force
Write-Host "Renamed '$($File.Name)' to '$newFileName'"
}
catch {
Write-Host "Error renaming '$($File.FullName)': $($_.Exception.Message)"
}
}
}
}
process {
Write-Host "Processing folder '$Path'"
# Check if the path is a file or a directory
if (Test-Path -Path $Path -PathType 'Leaf') {
# It's a file
$file = Get-Item $Path
# Check if the filename already starts with the date
Rename-File -File $file -NewDate $currentDate
}
elseif (Test-Path -Path $Path -PathType 'Container') {
# It's a directory
$files = Get-ChildItem -Path $Path -File # Get only files, not directories
# Iterate through each file in the directory
foreach ($file in $files) {
# Check if the filename already starts with the date
Rename-File -File $file -NewDate $currentDate
}
}
else {
# Path is invalid
Write-Host "Error: The specified path '$Path' is not a valid file or directory."
}
}
end {
# Optional: Add any cleanup or summary here
}