Link to home
Start Free TrialLog in
Avatar of steve michael
steve michael

asked on

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.
Avatar of oBdA
oBdA

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

Open in new window

Avatar of steve michael

ASKER

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

Hi oBdA, That worked. Many Thanks!
You should accept the answer(s) that solved your question (https:#a42387022), not your own comment ...
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.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
May i know how to accept the answer here? I could not find any button to do the same. Please let me know.
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-
Again: you should accept the Expert's answer(s) that solved your question (https:#a42387022, https:#a42387153), not your own comment ...
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.
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
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..