I have written a simple PowerShell script that reads data from a master CSV file and parses a URL column within.
It then checks the local file system to test if a binary file exists (in this case a WAV or AIFF file) within a subdirectory of the script root path, and if it does, appends the row meta data with a "Local File" column appended to a new CSV file.
The script works perfectly fine and executes in about 10 seconds with the master CSV file containing a sample of about 2000 rows, however a client in Korea is saying that the script slows down and eventually times out with a master CSV containing roughly 60000 rows.
A sample of the CSV I am parsing can be found here.
My gut feeling is that this is a memory issue caused by the $NewCSVObject however since this one of my first PS scripts, it very well could be just bad coding on my part.
function Get-ScriptDirectory {
Split-Path -parent $PSCommandPath
}
function Resolve-Local-CSV {
Write-Host "Generating Local CSV File ... Please wait ..."
$currentPath = Get-ScriptDirectory
$csvfile = "$currentPath{0}media_local_master.csv" -f [IO.Path]::DirectorySeparatorChar
$masterCSV = Import-Csv "$csvfile" -header _id, active, track_title, Download_URL, artist_name, album_title, composer, duration, publisher, description, url_path, beats_per_minute, file_path_uncompressed, preview_url, genres, moods, styles, instruments, keywords, last_modified -delimiter ','
$NewCSVObject = @()
$masterCSV | ForEach-Object {
$downloadUrl = $_.Download_URL
if ($downloadUrl -ne 'Download_URL') {
$parsedurl = $downloadUrl.split("/")
$artist = $parsedurl[4]
$album = $parsedurl[5]
$track = $parsedurl[6]
$albumTarget = "$currentPath{0}media{0}$artist{0}$album{0}" -f [IO.Path]::DirectorySeparatorChar
$trackTarget = "$albumTarget$track"
If ((test-path $trackTarget)) {
$localfile = "media{0}$artist{0}$album{0}$track" -f [IO.Path]::DirectorySeparatorChar
$_ | Add-Member -MemberType NoteProperty -Name "Local_File" -Value $localfile
$NewCSVObject += $_
}
}
$newLocalMaster = "$currentPath{0}media_local_master_final.csv" -f [IO.Path]::DirectorySeparatorChar
$NewCSVObject | Export-Csv "$newLocalMaster" -notype
}
Write-Host "Finished Generating Local CSV File ..."
}
Resolve-Local-CSV