Writing data from one file to the next

  • I added most of the information on what I'm trying to do in the TestScript.ps1.txt file. I also added the sample files I am trying to work with.

    This is not the exact files I am trying to do this on, but covers the goal I am trying to accomplish. I cannot share the exact files due to security policy at work.

    I basically have File1 (File_config.txt) that I want to add text to. The text I need to add is in File2 (File_Exemptions.txt). I am only looking for particular text out of File2 and I use File3 (File_Checks.txt) to check for that text if it matches.

    The catch is the text I am adding has to go in a particular spot in File1. When I pull in the content of File1 I wait to find the text "<Audit>", once it finds this text I can start putting the data out of File2 that equals the value from File3.

    The error I keep getting:

    The process cannot access the file 'C:\Users\Shawn\Documents\File_config.txt' because it is being used by another process.

    I can use Add-Content and it does not show this error, however it also does not put the text after "<Audit>", it puts it to the very end of File1, where I cannot have it. File1 is an XML file but the way File2 is formated I cannot work with it all as XML so I have to treat them as text file. The "string" I am pulling out of File2 is the node I want to add into File1. File2 is just not formated as XML so I cannot treat it as such.

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton

  • I also did find one suggestion about first loading the file I'm trying to write back to into memory first (File_config.txt). I did this by just adding $x = (Get-Content File_config.txt) and then just do $x and pipe that along as before.

    That does get rid of the error however the only thing it is writing back to the file is one line, cleaning out everything else and just writting that one line. So when the script is done the only thing in the file is the last object it wrote to the file.

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton

  • See if this version of TestScript.ps1 is what you're looking for:

    <#

    3 files are involved

    1) File_config.txt - Main file that is in XML format but for this

    it is just text file. Adding information to this file.

    2) File_Exemptions.txt - This file contains XML data for File_config.txt

    but the file itself is not in XML format completely so it is treated as text file.

    3) File_Check.txt - This file has values that are one of the fields in

    the XML file (File_config.txt). I want to go through this file

    pulling out the like from File_Exemptions.txt and add them to File_Config.txt but

    after the text "<Audit>" is found. The input goes after this tag.

    #>

    $location = 'C:\@\Projects\SSC\PS_OneFileToTheNext'

    Set-Location $location

    $configFile = "$location\File_config.txt"

    $exempFile = "$location\File_Exemptions.txt"

    $checkFile = "$location\File_Check.txt"

    $chk = (Get-Content $checkFile)

    ForEach($i in $chk)

    {

    $new = (Get-Content $exempFile | Where-Object {$_ -like "$i"})

    $new

    $new_content = ""

    Get-Content $configFile | ForEach-Object {

    $new_content += ($_ + "`r`n")

    If ($_ -match "<Audit>") {

    $new_content += ($new + "`r`n")

    }

    }

    Set-Content -Path $configFile -Value $new_content

    }

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • The things I think about sometimes while on my commute home...here is a much more efficient version of the script that only writes the final file once and only reads the exemp file once.

    <#

    3 files are involved

    1) File_config.txt - Main file that is in XML format but for this

    it is just text file. Adding information to this file.

    2) File_Exemptions.txt - This file contains XML data for File_config.txt

    but the file itself is not in XML format completely so it is treated as text file.

    3) File_Check.txt - This file has values that are one of the fields in

    the XML file (File_config.txt). I want to go through this file

    pulling out the like from File_Exemptions.txt and add them to File_Config.txt but

    after the text "<Audit>" is found. The input goes after this tag.

    #>

    $location = 'C:\@\Projects\SSC\PS_OneFileToTheNext'

    Set-Location $location

    $configFile = "$location\File_config.txt"

    $exempFile = "$location\File_Exemptions.txt"

    $checkFile = "$location\File_Check.txt"

    $chk = (Get-Content $checkFile)

    $exemp_content = (Get-Content $exempFile)

    $new_content = ""

    Get-Content $configFile | ForEach-Object {

    $new_content += ($_ + "`r`n")

    If ($_ -match "<Audit>") {

    ForEach($i in $chk)

    {

    $new = ($exemp_content | Where-Object {$_ -like "$i"})

    $new

    $new_content += ($new + "`r`n")

    }

    }

    }

    Set-Content -Path $configFile -Value $new_content

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • I got a little help from Twitter, well alot of help actually...

    Can you explain to me what the $new_content variable is doing?

    What I ended up with after some help is using a Find/Replace loop. I could get the text to write back to the config file just not where I needed it. So I used the -replace in a ForEach loop and then added back the <Audit> along with the line object from the exempt file. The only thing I have found with using the Set-Content is that it is adding a return character at the end of my config file. Which my config file is an XML formated file so when I go to my next processes I work with that file as XML, however when PowerShell converts it to PowerShell it does not like seeing that blank line.

    Apparently that is a bug with using the Set-Content. Something I believe the Scripting Guys have something on for a workaround.

    The code I came out with:

    Get-Content $configFile | ForEach-Object { $_ -replace ("<Audit>", "<Audit> $newline"} | Set-Content $configFile

    The $newline being the content from $exempFile.

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton

  • Shawn Melton (6/16/2011)


    I got a little help from Twitter, well alot of help actually...

    Hmmm...maybe I need to plug in over there...I have never used Twitter for tech questions.

    Can you explain to me what the $new_content variable is doing?

    $new_content holds the revised file content of the config file. It is built in three steps:

    1. Iterate over config file copying all content into $new_content until <Audit> is reached.

    2. (still inside config file loop) Now iterate over check file looking for and adding entries also in exempt file to $new_content.

    3. (broke out of check file loop) Continue iterating over the config file copying all remaining content into $new_content until the end of the file is reached.

    The code I came out with:

    Get-Content $configFile | ForEach-Object { $_ -replace ("<Audit>", "<Audit> $newline"} | Set-Content $configFile

    The $newline being the content from $exempFile.

    I thought I maintained the same logic in my scripts as the script provided in your OP but now I have doubts as to whether your script was an example of the end product with the one hurdle in it or whether the logic did not resemble what you were after. It would help if you could provide an example of the resulting config file you're looking for.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • Yes I will agree with you the "example" I wrote up is probably not easy to follow. I wish I could have just provided the files in question but they are part of a custom app that the customer owns and I did not feel comfortable putting them out there.

    I think your code does do the same thing I ended up with, just in a different manner (the great thing about PowerShell:-D). My goal was to take lines from the exempt file, only those that match to the check file, and put them in the config file after <Audit>.

    When I first did the -replace code I thought it would end up repeating <Audit> each time it wrote the line out, but it does not.

    Shawn Melton
    Twitter: @wsmelton
    Blog: wsmelton.github.com
    Github: wsmelton

  • I agree, PowerShell is neat like that. I am happy you found a solution 🙂

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply