0

I have file called 'main_file.txt' (c:temp\main_file.txt) that contains a single line of file path of another file like below.

\\mynewfolder\folder\sub_file.csv 

Here in the below code, I need to access the above sub_file.csv (\mynewfolder\folder\sub_file.csv)and use the contents to do some process. How to change my below first line of code to access the contents of the sub_file.csv which is available as a path inside the main_file.txt.

$txt = Get-Content "how to get the contents of sub_file.csv" | Out-String
{
...do some process with the contents of sub_file.csv..
...
...
}    

Could you please help.

3
  • 1
    $txt = c:\temp\main_file.txt | % { "Do stuff" } ? – SimonS Nov 30 '17 at 14:19
  • Something like this? a = Get-Content -Path C:\temp\main_file.txt Import-Csv -Path $a What are you trying to do with the CSV file? – Smeerpijp Nov 30 '17 at 15:35
  • I'm wondering why you're doing this though, you might aswell just put the path of the csv file in the import-csv cmd-let straight away. – Smeerpijp Dec 5 '17 at 8:33
1

Try this:

$handle = Get-Content c:\temp\main_file.txt
$content = Import-Csv $handle
$content | Format-Table

The variable $content should contain an array of PSObjects where each object represents a record from the csv file. I've included the last command as just a sample of doing something with the contents after reading them.

If your csv data is very big, you may not want to read it all into a variable before you process it. Instead, you may want to feed the output of Import-Csv into a pipeline, and process the records one at a time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.