To Read and open the file (from a filepath that resides in another file) using powershell

Hi,

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.
steve michaelAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
$txt = Get-Content -Path ((Get-Content -Path 'C:\temp\main_file.txt').Trim("`r`n"))

Open in new window

0
steve michaelAuthor Commented:
oBdA, one question here. From your answer, I understand that gc will open the file main_file.txt, but after that will it open the file sub_file.csv from the file path ( \\mynewfolder\folder\sub_file.csv which is inside the main_file.txt). Please clarify. I was assuming that we need to have another gc to open the file sub_file.csv. my assumption might be wrong. Could you please elaborate your answer where you are opening the file sub_file.csv from your code. That helps me.
0
oBdACommented:
The "inner" (second) "Get-Content" will read the contents of the file "C:\temp\main_file.txt" and strip any CR/LF from it. The result will then be used as Path for the "outer" (first) "Get-Content".
The extended version would look like this:
$Main = (Get-Content -Path 'C:\temp\main_file.txt').Trim("`r`n")
$txt = Get-Content -Path $Main

Open in new window

0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

steve michaelAuthor Commented:
Hi oBdA, That worked. Many Thanks!
0
oBdACommented:
You should accept the answer(s) that solved your question (https:#a42387022), not your own comment ...
0
steve michaelAuthor Commented:
okay oBdA. one query here, How can i get the file name part alone (sub_file.csv) from the file path \\mynewfolder\folder\sub_file.csv (which is inside the main_file.txt), I tried to get the file name part alone like below. This is a separate ps script to get the file name part alone.
but it didn't work, There is some tweak i need to do.

$Main = (Get-Content -Path 'C:\temp\main_file.txt').Trim("`r`n")
$txt = Get-Content -Path $Main
$outputFile = Split-Path -Path $txt -Resolve -leaf | Set-Content "C:\temp\my_file_name_part.txt"

Could you please help.
0
oBdACommented:
The "$txt" you're using in Split-Path is the content of the file path stored in main_file.txt, not the path itself.
And Set-Content by default doesn't return anything, so $outputFile will always be empty. If you want to write the file name to a file and set it to a variable at the same time, Set-Content needs -PassThru.
$SubfilePath = (Get-Content -Path 'C:\temp\main_file.txt').Trim("`r`n")
$txt = Get-Content -Path $SubfilePath
$SubfileName = Split-Path -Path $SubfilePath -Leaf | Set-Content 'C:\temp\my_file_name_part.txt' -PassThru

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
steve michaelAuthor Commented:
May i know how to accept the answer here? I could not find any button to do the same. Please let me know.
0
oBdACommented:
There should be big green buttons "Best solution" under the comments.
How do I accept a comment as my solution?
http://support.experts-exchange.com/customer/en/portal/articles/608621-how-do-i-accept-a-comment-as-my-solution-
0
oBdACommented:
Again: you should accept the Expert's answer(s) that solved your question (https:#a42387022, https:#a42387153), not your own comment ...
0
steve michaelAuthor Commented:
Hi oBdA,

From the link you provided, I could see the below steps to accept the answer and i did the same :)
"To accept a comment as a solution, please follow these steps (screen shots below):
Log into your Experts Exchange account.
Go to the question you would like to close. You'll find it under "My Open Questions" in your workspace.
Click on "Best Solution" located on the comment that provided the answer to your question and click "Next" on the bottom rail.
If you'd like, leave a comment for the experts.  This is a good chance for you to say thank you to the experts and verify that the solution worked.
Click "Close Question"."

I clicked on "Best solution" under the comments and it navigated to another pop window to share comments, where i have typed my comments "Thank you Expert for answering the qstn!"  and then clicked on close the question button. Is this still wrong way to accept the answer. please let me know.
0
oBdACommented:
You need to use the "Best answer" directly under the Expert's comment(s) that solved your question.
In this case
https://www.experts-exchange.com/questions/29071116/To-Read-and-open-the-file-from-a-filepath-that-resides-in-another-file-using-powershell.html?anchor=a42387153
0
steve michaelAuthor Commented:
I have clicked on 'Best Solution' button (in the expert's comments) and it navigated to this pop up comment box with 'close the question' button at the bottom.. and i'm going to click the 'close the question' button which at the bottom of the comment box..
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.