Unfundednut
Unfundednut

Reputation: 334

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.

Upvotes: 3

Views: 20976

Answers (2)

Tobias
Tobias

Reputation: 19

It will work if you put the path in a veriable instead

$path = $savelocation + '\' + $filename
Export-Csv -Path $path -Force -Delimiter ',' -NoTypeInformation

Upvotes: 1

BartekB
BartekB

Reputation: 8650

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... :)

Upvotes: 4

Related Questions