Matthias Martein
Matthias Martein

Reputation: 1

Export .CSV file with changes made to same name and folder as import

I need to change some date formats in incoming .csv files so they can be properly processed later on.

I am able to change the date formats but I can't figure out how to export my .csv so it keeps the same name and remains in the same folder. Multiple .csv files will pop up in the folder from time to time these have to be processed one by one.

One CSV file works fine, exporting to another folder also but then he writes everything into the same csv.

This is what I've got:

Import-Csv -Path (Get-ChildItem -Path C:\Test2\input '*.csv').FullName | ForEach-Object { $_.SamplingDateTime = '{0:yyyyMMddhhmmss}' -f $(Get-Date $_.SamplingDateTime); $_.DateOfBirth = '{0:yyyyMMdd}' -f $(Get-Date $_.DateOfBirth) $_ } | export-csv -NoTypeInformation C:\Test2\input\*.csv

export-csv : Cannot perform operation because the path resolved to more than one file. This command cannot operate on multiple files. --> makes sense but don't know how else to do it, quite new to the whole thing. ..

Maybe trough a quick detour into a subfolder? It must be done trough powershell.

Some advice?

Upvotes: 0

Views: 932

Answers (1)

Daniel
Daniel

Reputation: 5114

You do not have access to the filepath in the pipeline using import-csv first. The path of the output file needs to be tracked somehow to be used in the final export-csv command. Try something like this

    Get-ChildItem -Path C:\Test2\input '*.csv' | ForEach-Object {
        $path = $_.FullName;
        $_.FullName | Import-Csv | ForEach-Object { 
            $_.SamplingDateTime = '{0:yyyyMMddhhmmss}' -f $(Get-Date $_.SamplingDateTime);  
            $_.DateOfBirth = '{0:yyyyMMdd}' -f $(Get-Date $_.DateOfBirth);
            $_ 
        } | export-csv -NoTypeInformation -Path $path     
    }

Upvotes: 1

Related Questions