Skip to content

Instantly share code, notes, and snippets.

@mcoffin
Created April 6, 2021 00:01
Show Gist options
  • Save mcoffin/94756fcdfcb14b37a1b3ac93ffbbe06d to your computer and use it in GitHub Desktop.
Save mcoffin/94756fcdfcb14b37a1b3ac93ffbbe06d to your computer and use it in GitHub Desktop.
move-setups.ps1
Function Find-Setups([Parameter(Mandatory=$true)][String]$Season, [Parameter(Mandatory=$true)][String]$Track, [String]$Path = ".", [switch]$Recurse) {
$filter = "*$Season*$Track*.sto"
if ($Recurse) {
Get-ChildItem -Path "$Path" -Recurse -File -Filter "$filter"
} else {
Get-ChildItem -Path "$Path" -File -Filter "$filter"
}
}
Function Print-Debug(
[Parameter(Mandatory=$true)][String]$FunctionName,
[Parameter(Mandatory=$true)][String]$DebugString
) {
Write-Output "$FunctionName : $DebugString"
}
Function Actually-Move-Setup([Parameter(Mandatory=$true)][System.IO.FileInfo]$Path, [Parameter(Mandatory=$true)][System.IO.DirectoryInfo]$Destination, [switch]$DryRun, [switch]$VerboseOutput) {
$name = $Path.Name
$prefix = 'Moving'
if ($DryRun) {
$prefix = 'Would move'
}
if ($VerboseOutput) {
$fullname = $Path.FullName
Write-Output "Current directory:"
Write-Output @(Get-Location)
Print-Debug "Actually-Move-Setup" "destination directory:"
Write-Output $Destination
$realdestination = $Destination.FullName
$partialname = $Path.Name
Write-Output "$prefix '$fullname' to '$realdestination\$partialname"
}
if (!$DryRun) {
Move-Item -Path $Path -Destination $Destination
}
}
Function Actually-Move-Setups(
[Parameter(Mandatory=$true)][System.IO.FileInfo[]]$Path,
[Parameter(Mandatory=$true)][String]$Destination,
[switch]$VerboseOutput,
[switch]$DryRun
) {
if ($VerboseOutput) {
Write-Output "Paths to be moved:"
Write-Output $Path
Write-Output "Moving setups to '$Destination':"
$FullPath = ($Path).FullName
Write-Output $FullPaths
}
if ( ! ( Test-Path -Path $Destination ) ) {
Write-Output "Setup folder '$Destination' didn't exist, so we're creating it"
mkdir -p $Destination
}
$destinationpath = $Destination
$destinationdir = Get-Item -Path "$Destination"
if ($VerboseOutput) {
Print-Debug "Actually-Move-Setups" "destination directory"
Write-Output $destinationdir
}
Write-Output $Path | %{Actually-Move-Setup -Path $_.FullName -Destination $destinationdir.FullName -VerboseOutput:$VerboseOutput -DryRun:$DryRun}
}
Function Move-Setups(
[Parameter(Mandatory=$true)][String]$Track,
[Parameter(Mandatory=$true)][String]$Season,
[String]$FriendlyTrack = "",
[String]$Path = ".",
[switch]$Recurse,
[switch]$VerboseOutput,
[switch]$DryRun
) {
$trackdirname = "$FriendlyTrack"
if ( "$trackdirname" -eq "" ) {
$trackdirname = "$Track"
}
if ( "$Path" -ne "." ) {
if ( !( Test-Path "$Path" ) ) {
Write-Output "Warning: path \"$Path\" didn't exist, so we're creating it"
mkdir -p "$Path"
}
Push-Location $Path
}
$newpath = "$trackdirname\$Season"
if ($VerboseOutput) {
Write-Output "Moving files in path $Path for season $Season and track $Track"
Write-Output "setup path: $newpath"
}
if ($Recurse) {
$setups = Find-Setups -Recurse -Season $Season -Track $Track
} else {
$setups = Find-Setups -Season $Season -Track $Track
}
if ($VerboseOutput) {
Write-Output "Setups to be moved:"
Write-Output $setups
Write-Output "Setups paths to be moved:"
Write-Output (Write-Output $setups).FullName
}
Actually-Move-Setups -Path $setups -Destination "$newpath" -VerboseOutput:$VerboseOutput -DryRun:$DryRun
if ( "$Path" -ne "." ) {
Pop-Location
}
}
# Function Print-All-FullPath(
# [Parameter(Mandatory=$true, ValueFromPipeline=$true)][System.IO.FileInfo[]]$Files
# ) {
# $count = $Files.Count
# Write-Output "Got $count files"
# Write-Output "paths:"
# Write-Output $Files
# Write-Output "full paths:"
# Write-Output $Files | ForEach-Object {Write-Output $_.FullPath}
# }
# param (
# [Parameter(Mandatory=$true)][String]$Track,
# [Parameter(Mandatory=$true)][String]$Season,
# [String]$Path = ".",
# [switch]$Recurse,
# [switch]$VerboseOutput
# )
#
# Move-Setups -Recurse:$Recurse -VerboseOutput:$VerboseOutput -Path $Path -Track $Track -Season $Season
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment