none
Read a text file and write some content to another text file RRS feed

  • Question

  • Hi,

    I'm trying to write a powershell script for the below requirement, I'm new to powershell, can someone help me with solution. It would be a great help from you.

    Requirement: I need to read a text file from the command prompt and provide the content to a test.exe (it is a executable file which sorts the folders provided in the location mentioned in input.txt file) and print the result into a output.txt file.

    input text file: input.txt

    =========================================================

    [TestCase1]
    Name        = CheckPras
    FolderPath    = C:\Pras

    [TestCase2]
    Name         = CheckTest
    FolderPath     = C:\test

    [TestCase3]
    Name         = Checkg@@g)!
    FolderPath     = C:\g@@g)!
    =======================================================

    Output file: output.txt (this should contain as below)

    =============================================

    TestCase1: details

    TestCase2:details

    TestCase3:details

    Thanks in advance !!!!

    Pras

    Wednesday, May 23, 2012 10:34 AM

Answers

  • I said: ... why not show us the script you have created to this point. We may then better deduce where you are stuck with it.

    You said: c:\>Automation.ps1 -i input.txt -o output.txt

    It appears you are stuck at the point just before any powershell coding has been written. If so, here are a few suggestions to start out with:

    1. use a param statement to define the "-i" and "-o" command line parameter
    2. use get-content to read the input file as suggeted by powershell junior
    3. use a regex to extract the pertinent info from the input file as suggested by powershell junior
    4. use out-file to write the output to the output file

    If the code you write to do this does not work post a copy of it back here along with any output or error messages that are displayed.


    Al Dunbar

    Friday, May 25, 2012 3:40 AM

All replies

  • I am trying to understand your issue and hopefully i didnt misinterpret it:

    You can retreive the content of a file with:

    $file = Get-Content -Path "c:\users\test\desktop\input.txt"

    For the parsing I would go with an approach which is based on regular expressions:

    For the testcase:

    $Testcase_Regex = "^\[TestCase\d{1}\]$"
    
    $file -match $Testcase_Regex

    Now you need to do the same for the Name and the FolderPath. Afterwards it should be easy to combine those information together and export them to a file.

    I am ath the moment a bit stuck in work as soon as I am free again I will try to finish it.


    Regards / Freundliche Grüsse Powershell Junior




    • Edited by King Julien Wednesday, May 23, 2012 11:37 AM
    Wednesday, May 23, 2012 11:36 AM
  • this sounds like a homework question, mainly because you have stated what you want a script to do, but not what your ultimate goal is. Your goal might just be to sort the folders mentioned in the input file (whatever that means) using an executable called test.exe. Presumably that program will provide the "details" that need to be included in the output. It is a little difficult for us to figure out how to do that not knowing what these details are or how they are output by test.exe. Does it write to stdout, into a text file, into an XML file, or what?

    Rather than making us guess where your specific problem lies with this script of yours, why not show us the script you have created to this point. We may then better deduce where you are stuck with it.


    Al Dunbar

    Wednesday, May 23, 2012 4:02 PM
  • Hi,

    I will try to make it more clear. Assume that I have a folders "c:\test123", this folder has many directories inside like c:\test123\ram",c:\test123\shyam",c:\test123\kishan", and "c:\test123\kishan\radha".

    By using the test.exe, if I run from command prompt as below

    c:\> test.exe c:\test123

    Results:

    It will sort out the directories and displays as below.

    ==========================

    c:\test123\kishan

    c:\test123\kishan\radha

    c:\test123\ram

    c:\test123\shyam

    =============================

    Now if I want to write a powershell code to make it automated, I would like to read the input directory location from a text file called input.txt and write the output to a output.txt.

    Please check the below details:folder path mentioned as "c:\test123" in input.txt file.

    input.txt:

    ========================

    [TestCase1]
    Name        = Checktest123
    FolderPath    = C:\test123

    ===========================

    Now with the Powershell, we should take test.exe and should take input.txt as input file and write the results to output.txt as shown below.

    c:\>Automation.ps1 -i input.txt -o output.txt

    I think I made t clear. Thanks a lot in advance.

    Thursday, May 24, 2012 4:49 PM
  • I said: ... why not show us the script you have created to this point. We may then better deduce where you are stuck with it.

    You said: c:\>Automation.ps1 -i input.txt -o output.txt

    It appears you are stuck at the point just before any powershell coding has been written. If so, here are a few suggestions to start out with:

    1. use a param statement to define the "-i" and "-o" command line parameter
    2. use get-content to read the input file as suggeted by powershell junior
    3. use a regex to extract the pertinent info from the input file as suggested by powershell junior
    4. use out-file to write the output to the output file

    If the code you write to do this does not work post a copy of it back here along with any output or error messages that are displayed.


    Al Dunbar

    Friday, May 25, 2012 3:40 AM