Tuesday, 24 April 2018

Using PowerShell to Create a Correctly Ordered USB Music Key

I have a car which has an Alpine CDE-181RR stereo system. This stereo has a USB slot which you can either use for charging your phone up, or plug in a USB Key full of music. My usage case for this blog post is the latter. I use a 4GB SanDisk thumb drive which is so tiny you barely notice it. The problem with copying music to the USB thumb drive, is that the stereo treats it like a CD i.e. what you copy first is first track in the music list, and what you copy last is the last song.

Image: Alpine CDE-181RR car stereo

Image: SanDisk thumb drive

So, I thought, why not use PowerShell to construct my USB Music Key like a CD burn.
The PowerShell script is below, copy it into a text editor and save as say USB_Music_Key.ps1.
The first thing we need is a correctly formatted CSV with headers:

Include = Put ‘Y’ in the column if you want to include the audio media file
Source Path = Source directory path (need to put ‘\’ on the end)
Source File Name = Source audio media file name
Destination Path = Destination directory path (will be the USB drive, and needs ‘\’ on the end)
Destination File Name = Destination save file name

Once you’ve constructed your CSV file, run the PowerShell script as>

.\USB_Music_Key.ps1 -CSVFilePath {FILENAME/PATH}

This will tell you how much capacity is required on the destination.
Then run as>

.\USB_Music_Key.ps1 -CSVFilePath {FILENAME/PATH} -CalculateOff

- to construct your USB Music Key.

Image: Using USB_Music_Key.ps1

Image: Input CSV

Image: Completed “burnt” USB Music Key (notice capacity is spot on)

Note: I’ve not actually tested this in my car yet. I will update if the order of tracks is not as expected.

The PowerShell Script


<#
.SYNOPSIS
  Creates a correctly ordered USB Music Key.

.DESCRIPTION
  Simple program to copy music files from source to USB key.

.INPUTS
  -CSVFilePath CSVFilePath
    Use this switch to specify where to find the CSV file.
  -CalculateOff
    Use this switch to make the program copy data.
    (Default behaviour is just to calculate how much data will be copied.)
 
  A correctly formatted CSV.
  CSV HEADER 1 = "Include"
  CSV HEADER 1 = "Source Path"
  CSV HEADER 2 = "Source File Name"
  CSV HEADER 3 = "Destination Path"
  CSV HEADER 4 = "Destination File Name"
  Note: Use (Get-ChildItem SOURCE_PATH).Name to obtain list from source path.
 
.OUTPUTS
  Copied data to USB key.

.NOTES
  Creation Date: April 23, 2018
#>

Param(
  [Parameter(Mandatory=$true)][String]$CSVFilePath,
  [Switch]$CalculateOff
)


FUNCTION Wr {
  Param([String]$Echo,[String]$Ink = "WHITE")
  If($Echo){Write-Host $Echo -ForegroundColor $Ink -NoNewLine}
  Else{Write-Host}
}

If(!(Test-Path -LiteralPath $CSVFilePath)){Wr "Cannot find $CSVFilePath!" RED;Wr;EXIT}
[System.Array]$Data = Import-CSV $CSVFilePath -Encoding Default

[Int64]$CR = 0
$Data | Foreach{
  [String]$Include = $_."Include"
  [String]$SourcePath = $_."Source Path"
  [String]$SourceFileName = $_."Source File Name"
  [String]$Source = $SourcePath + $SourceFileName
  [String]$DestinationPath = $_."Destination Path"
  [String]$DestinationFileName = $_."Destination File Name"
  [String]$Destination = $DestinationPath + $DestinationFileName
  If(($Include -eq "Y") -and $CalculateOff){
    If(!(Test-Path -LiteralPath $SourcePath)){
      Wr "Failed test path $SourcePath" RED;Wr
              }Else{
      Wr "Copying Item: $Source to $Destination";Wr
      If(!(Test-Path -LiteralPath $DestinationPath)){
        [Void](New-Item -ItemType directory -Path $DestinationPath)
      }
      Copy-Item -LiteralPath $Source -Destination $Destination
    }
  }elseif($Include -eq "Y"){
    $CR += (Get-Item -LiteralPath $Source).length
  }
}
If(!$CalculateOff){
  Wr "Capacity Required" CYAN;Wr
  Wr ($CR / 1024);Wr " KB";Wr
  Wr ($CR / (1024*1024));Wr " MB";Wr
  Wr ($CR / (1024*1024*1024));Wr " GB";Wr
}


1 comment:

  1. swiss replica watches, combining elegant style and cutting-edge technology, a variety of styles of replica Breguet watches, the pointer walks between your exclusive taste style.

    ReplyDelete