/build/static/layout/Breadcrumb_cap_w.png

Scripting Question


I have 2 files that each have one line of text and I need to change the text in file 2 to match file 1

03/26/2020 298 views

In each file the text says Port=3159(Different port number each time).

I need to change the port number in the second file to match the port number in the first file. Is there a way to do this in using a vbscript?

0 Comments   [ + ] Show comments

Comments


All Answers

0

Can do it in PowerShell, if that works for you?


Updated answer:
Got bored, here you go.

Answered 03/27/2020 by: rileyz
Red Belt

  • $VerbosePreference = 'Continue'

    $FileA = Get-Content "C:\Media\File A.txt"
    $FileB = Get-Content "C:\Media\File B.txt"

    Write-Verbose 'Contents of File A'
    Write-Verbose $($FileA | Out-String)

    Write-Verbose 'Contents of File B'
    Write-Verbose $($FileB | Out-String)

    Write-Verbose 'Find and make change.'
    $FileA_PortNumber = $FileA -match '^Port=\d.*' #Regex Pattern, find match and assign to variable
    $FileB_PortNumber = $FileB -match '^Port=\d.*' #Regex Pattern, find match and assign to variable

    $FileB_IndexOfPort = $FileB.indexof("$FileB_PortNumber") #Assign index number from array, of where the port number is in the array
    $FileB.Item($FileB_IndexOfPort) = "$FileA_PortNumber" #Update file B, with port number from file A, based on the index number

    Write-Warning ' Updated Contents of File B'
    Write-Verbose $($FileB | Out-String)

    Write-Verbose 'Updating file'
    $FileB | Set-Content "C:\Media\File B.txt"
This content is currently hidden from public view.
Reason: test For more information, visit our FAQ's.
This content is currently hidden from public view.
Reason: test For more information, visit our FAQ's.
This content is currently hidden from public view.

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

View more:

Share

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ