Importing a .csv file with varying name into powershell

Viewed 40

Complete novice here, Creating a basic GUI that allows me to automatically adjust a bill of materials .csv file with relatively minimal user input. Its basic function is to add a ".DXF" file extension to the end of each value in the first column - all of this functionality is working for now but I'm trying to find out how to import a csv without an explicit filename in the import-csv -path. The .csv file will always contain the word "BOM", so I've tried to allocate a $FileName that I can use with import-csv, but I think I'm using the get childitem cmdlet wrong.

UPDATE: I'm trying to find a way to search a browsed folder directory ($path), to find a BOMxx.csv file so I can assign a variable to the "xx", where xx would follow the form "_ 0", "_ 1", "_ 2" etc. I then hope to use import-csv on the BOMxx.csv and amend the "$($.PartName).DXF" to be "$($.PartName)xx.DXF", so the newly exported CSV column includes "xx.DXF" on the end of each partname.

function Execute {
    $FileName = gci -Recurse | Where { $_.Name -match 'BOM' } | Select Fullname
    Import-Csv -Path "$path\$FileName.csv" | ForEach-Object {
        $_.PartName = "$($_.PartName).DXF" 
        $_ 
    } | Export-Csv -Path "$path\PrimeCutImport.csv" -NoTypeInformation
}

$result = $LocalBOMForm.ShowDialog()

if ($result –eq [System.Windows.Forms.DialogResult]::Cancel) {
    write-output 'Cancelled'
}

Any feedback on this would be greatly appreciated. Thanks. EDIT: Added $path for clarification:

$BrowseBtn.Add_Click( { Browse })
function Browse { 
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null

    $foldername = New-Object System.Windows.Forms.FolderBrowserDialog
    $foldername.Description = "Select a folder"
    $foldername.rootfolder = "MyComputer"
    $foldername.SelectedPath = $initialDirectory
    if ($foldername.ShowDialog() -eq "OK") {
        $folder += $foldername.SelectedPath
    }
    return $folder
}

$path = Browse

UPDATE 2: Got it to work how I needed it to. Might be a bit janky but it works:

function Execute {
    $rev=gci -path "$path" -Filter BOM*.csv | Select-Object -Property PSChildName 
$rev=$rev -replace "\D+"
    Import-Csv -Path "$path\BOM_$rev.csv" | ForEach-Object {
        $_.PartName = "$($_.PartName)_$rev.DXF" 
        $_ 
    } | Export-Csv -Path "$path\PrimeCutImport_$rev.csv" -NoTypeInformation
}

Thanks for the responses.

0 Answers
Related