My Google Drive for an unknown reason creates multiple duplicates of certain files in a format of: filename (2021_02_08 11_22_39 UTC).ext
I would like to move these files on another folder, just in case. I found this thread here which seems similar. Im not quite an expert on regex. Could you please help me out on that?
Regex: [a-zA-Z] ). for searching: UTC).
I suppose it should read something like:
$source = "C:\Dropbox"
$destination = "C:\DropboxDupes"
gci .\Dropbox -Recurse -File | ?{ $_.basename -match "[a-zA-Z] \)\."} | % {
$destination_filename = $_.fullname.Replace($source, $destination)
$destination_dir = split-path $destination_filename -Parent
if(-not (Test-Path $destination_dir -PathType Container)) {
mkdir $destination_dir | out-null
}
move-item $_.fullname $destination_filename
}
CodePudding user response:
Try '\([\d_\s] UTC\)'
to match digits, underscore, and space followed by 'UTC' and all enclosed in parentheses.
It is best not to use aliases, use parameter names, and stick with case in scripts.
$source = "C:\Dropbox"
$destination = "C:\DropboxDupes"
Get-ChildItem -Path '.\Dropbox' -Recurse -File |
Where-Object { $_.BaseName -match '\([\d_\s] UTC\)'} |
ForEach-Object {
$destination_filename = $_.FullName.Replace($source, $destination)
$destination_dir = Split-Path -Path $destination_filename -Parent
if (-not (Test-Path -Path $destination_dir -PathType Container)) {
New-Item -Type Directory -Path $destination_dir | Out-Null
}
Move-Item -Path $_.FullName -Destination $destination_filename
}