Last active
April 6, 2023 02:46
-
-
Save augustohp/b0f6e8b0bee853c6fcc7da6bf13e6177 to your computer and use it in GitHub Desktop.
Renames `.WAV` files on removable media with the date they were created and move them to a local disk.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Will rename and move reharsals from removable drives to another place. | |
.DESCRIPTION | |
This script will rename and move `.WAV` files from removable drives to another place. | |
It will rename files that are not named with a date to a date and an incremental suffix. | |
It will move files that are named with a date to a different path containing the year and the quarter they were recorded. | |
The destination path is hardcoded in the script. | |
#> | |
$ErrorActionPreference = "Stop" | |
$DestinationPath = 'C:\Users\User\Músicas\Ensaios' | |
$ToggleDebug = $true | |
$ToggleVerbose = $true | |
function Find-WaveFiles { | |
<# | |
.SYNOPSIS | |
Lists all `.WAV` files on removable drives. | |
.PARAMETER NamedWithDates | |
All reharsals are named with a date and an incremental suffix starting at 01, | |
e.g. `2018-01-01_01.wav`. This parameter will list only files named with dates | |
(already renamed) or not (needing a rename). | |
.OUTPUTS | |
System.IO.FileInfo | |
.EXAMPLE | |
Find-WaveFiles -NamedWithDates $true | |
Lists all `.WAV` files on removable drives that are named with dates. | |
#> | |
Param( | |
# Find only file names with dates or without a date in their name | |
[Parameter( | |
Mandatory=$true, | |
Position=0, | |
HelpMessage='Find only file names with dates or without a date in their name')] | |
[bool] $NamedWithDates | |
) | |
Process { | |
$drives = Get-Volume | | |
Where-Object { $_.DriveType -eq 'Removable' } | | |
Where-Object { $_.FileSystemType -NotContains 'Unknown' } | |
Write-Debug "Found $($drives.Count) removable drives: $($drives.DriveLetter -join ', ')" | |
$filesFound = @() | |
foreach ($drive in $drives) { | |
Write-Debug "Searching for *.wav files on '$($drive.DriveLetter):' ..." | |
if ($NamedWithDates) { | |
$filesFound += Get-ChildItem -Path $drive.Root -Filter *.wav -Recurse | | |
Where-Object {$_.Name -like '????-??-??*'} | |
} else { | |
$filesFound += Get-ChildItem -Path $drive.Root -Filter *.wav -Recurse | | |
Where-Object {$_.Name -notlike '????-??-??*'} | |
} | |
} | |
Write-Debug "Found $($filesFound.Count) files." | |
$filesFound | |
} | |
} | |
function Rename-Reharsals { | |
<# | |
.SYNOPSIS | |
List `.WAV` files, on all removable drives, without the date they were | |
created as their names and rename them. | |
.EXAMPLE | |
Rename-Reharsals | |
#> | |
Process { | |
Find-WaveFiles -NamedWithDates $false | ForEach-Object { | |
$createdAt = (Get-ItemProperty $_.FullName).CreationTime | |
$createdAt = $createdAt.ToString('yyyy-MM-dd') | |
$currentName = $_.FullName | |
$dirName = $currentName.Substring(0, $currentName.LastIndexOf('\')) | |
$newName = $createdAt + '_01.wav' | |
$i = 1 | |
while (Test-Path "${dirName}\\${newName}") { | |
$i++ | |
$newName = $createdAt + '-' + $i.ToString('00') + '.wav' | |
} | |
Write-Host "Renaming: '${currentName}' -> '${newName}' ..." | |
Rename-Item $currentName $newName | |
} | |
} | |
} | |
function Move-Reharsals { | |
<# | |
.SYNOPSIS | |
Will move `.WAV` files, on all removable drives, that were already renamed | |
to a different path containing the year and the quarter they were recorded. | |
#> | |
[CmdletBinding()] | |
param ( | |
[Parameter( | |
Mandatory=$true, | |
HelpMessage='Where files will be moved to')] | |
[string] $DestinationPath | |
) | |
Begin { | |
if (!(Test-Path $DestinationPath)) { | |
throw [System.IO.DirectoryNotFoundException]::New("Destination path does not exist: '${DestinationPath}'") | |
} | |
} | |
Process { | |
Find-WaveFiles -NamedWithDates $true | ForEach-Object { | |
$date = $_.Name.Substring(0,10) | |
$year = $date.Substring(0,4) | |
$quarter = [int]([math]::Ceiling((([datetime]$date).Month / 3))) | |
$quarter = $quarter.ToString('00') | |
$newPath = "${DestinationPath}\${year}\Q${quarter}" | |
$currentName = $_.FullName | |
$newName = $newPath + '\' + $_.Name | |
if (!(Test-Path $newPath)) { | |
Write-Debug "Creating directory: '${newPath}' ..." | |
New-Item -ItemType Directory -Path $newPath | |
} | |
if (!(Test-Path $newName)) { | |
Write-Host "Moving: '${currentName}' -> '${newName}' ..." | |
Move-Item $currentName $newName | |
} else { | |
Write-Warning "File '${newName}' already exists on destination, ignoring. You should remove it manually.'" | |
} | |
} | |
} | |
} | |
try | |
{ | |
Write-Host "WAV files on removable drives: " | |
Find-WaveFiles -NamedWithDates $false | |
Write-Host "----------------------------------------" | |
$menu = @() | |
$menu += New-Object System.Management.Automation.Host.ChoiceDescription "&Rename", "Rename them with their creation date" | |
$menu += New-Object System.Management.Automation.Host.ChoiceDescription "&Move", "Move them from the removable drive" | |
$menu += New-Object System.Management.Automation.Host.ChoiceDescription "&Quit", "Quit" | |
$choice = $Host.UI.PromptForChoice("What you want to do with the files listed above?", "Choose an option", $menu, 2) | |
if ($choice -eq 0) { | |
Rename-Reharsals -Debug:$ToggleDebug -Verbose:$ToggleVerbose | |
} elseif ($choice -eq 1) { | |
Move-Reharsals -DestinationPath $DestinationPath -Debug:$ToggleDebug -Verbose:$ToggleVerbose | |
} else { | |
Write-Host "Bye!" | |
return | |
} | |
} | |
catch [System.IO.DirectoryNotFoundException] | |
{ | |
Write-Error $PSItem.ToString() | |
} | |
Read-Host -Prompt "Press any key to continue..." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment