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
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
Answers (1)
Please log in to answer
Posted by:
rileyz
4 years ago
Can do it in PowerShell, if that works for you?
Updated answer:
Got bored, here you go.
Comments:
-
$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" - rileyz 4 years ago