2

How can I loop through a list of files, perform an operation and save the file to a new location with a name from a list?

As an example I have a list of CSV files, and a list of new names, I would like to do a simple operation on the CSV file like changing the delimiter and save each file to a new location with a new name from the list of names. How can I set the save path and new name correctly?

$files = Get-ChildItem C:\folder\*.csv
$te = Import-Csv C:\names.csv
For ($i=0; $i -lt $te.Length;$i++){
    Import-Csv $file -Delimiter "|" | Export-Csv -Path C:\another_folder\$te[$i].csv -Delimiter ","
}

To be precise, the problem is that the save path after the -Path statement is incorrect. How do I fix this?

1 Answer 1

1

You just need to wrap the path with quotes:

$files = Get-ChildItem C:\folder\*.csv
$te = Import-Csv C:\names.csv
For ($i=0; $i -lt $te.Length;$i++){
    Import-Csv $file -Delimiter "|" | Export-Csv -Path "C:\another_folder\$te[$i].csv" -Delimiter ","
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.