1 Replies - 339 Views - Last Post: 21 September 2017 - 01:26 PM Rate Topic: -----

#1 mercy_angel  Icon User is offline

  • D.I.C Regular

Reputation: 0
  • View blog
  • Posts: 330
  • Joined: 28-February 13

powershell copy a string in one file to another

Posted 19 September 2017 - 02:54 AM

can you help me with find a some script to help me to replace a string from one to another file.
for example in test1.xml i have string id="123", and i want to copy it to test2.xml instead of id="1" to be "123"

i tried with some script:

# Original text
$inputFile1 = "C:\Users\tomy\Desktop\signage4-final (2)\data\config.xml"
# Text to be inserted
$inputFile2 = "C:\Users\tomy\Desktop\signage4-final (8)\data\config.xml"
# Output file
$outputFile = "C:\ephemeral\file3.txt"
# Find where the last </location> tag is
if ((Select-String -Pattern "uid=" -Path $inputFile1 | 
    select -last 1) -match ":(/>\d+):")
{
    $insertPoint = $Matches[1]
    # Build up the output from the various parts
    Get-Content -Path $inputFile1 | select -First $insertPoint | Out-File $outputFile
    Get-Content -Path $inputFile2 | Out-File $outputFile -Append
    Get-Content -Path $inputFile1 | select -Skip $insertPoint | Out-File $outputFile -Append
}


but i am not sure why he use 2 input and 1 output files. idont get it...

Is This A Good Question/Topic? 0
  • +

Replies To: powershell copy a string in one file to another

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1852
  • View blog
  • Posts: 6,658
  • Joined: 19-February 09

Re: powershell copy a string in one file to another

Posted 21 September 2017 - 01:26 PM

View Postmercy_angel, on 19 September 2017 - 10:54 AM, said:

... i am not sure why he use 2 input and 1 output files. i dont get it...


insertPoint is where the new data (string) is to be inserted. The new data is held in the 2nd input file.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1