Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export-CSV with the path as a variable

I have a script that basically writes down each file and folder on a remote share and dumps it to a csv. Everything works wonderfully (there is still a lot of work to be done on the script itself tho) except for the last part.

Import-Csv -Path ($savelocation + '\' + $filename) -Delimiter ',' | ForEach-Object {
    if ($audioarray -contains $_.Extension) {
        $_.MediaType = 'Audio'
        $_
    } elseif ($videoarray -contains $_.Extension) {
        $_.MediaType = 'Video'
        $_
    } elseif ($otherarray -contains $_.Extension) {
        $_.MediaType = 'Other'
        $_
    } else {
        $_
    }
} | Export-Csv -Path ($savelocation + '\' + $filename) -Force -Delimiter ',' -NoTypeInformation

If I change the path to a static location something like \\servername\share\testfolder it works fine. But if I use the above or even if I join the ($savelocation + '\' + $filename) into something like $filename it still just writes an empty file.

like image 442
Unfundednut Avatar asked Oct 15 '25 07:10

Unfundednut


1 Answers

Sorry, but it won't work for any path. Reason is simple - you read/write to the same file. It would work, if you would read into variable, and work on data in memory.

IMO having a path (elements) stored in variable have nothing to do with it. BTW: you can join paths with Join-Path cmdlet... :)

like image 68
BartekB Avatar answered Oct 18 '25 02:10

BartekB



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!