Using powershell, I'd like to prepend the creation date of a number of files, but only if the filenames do not already start with the date. For other files, the code should skip them.
I've successfully used the following command to prepend dates, but I'd like to make it conditional on the filename not starting with any date format (or perhaps, any digits at all).
Dir | Rename-Item -NewName {(Get-Date $_.CreationTime -Format "yyyy-MM-dd ")+ $_.BaseName+$_.Extension}
You can use regex to match a pattern. I'm not the best with regex but found the following regex elsewhere for matching a date in the format you specified. I just took your existing code and inserted a Where-Object
to filter out files that already match the pattern.
$regex = [regex] '^\d{4}-\d{2}-\d{2}'
Get-ChildItem | Where-Object {!($_.Name -match $regex)} | Rename-Item -NewName {(Get-Date $_.CreationTime -Format "yyyy-MM-dd ")+ $_.BaseName+$_.Extension}