Edit txt file with new value

Hi All,

I have below txt entry file at C:\Program Files (x86)\IBM\config.txt

ApplicationIdentifier=BFDemo
ConfigurationServer=11.22.33.44
ConfigurationServerPort=2222

I want to be edit the txt file as below

ApplicationIdentifier=BFRelay
ConfigurationServer=44.33.22.11
ConfigurationServerPort=4444

is there any action command for this please help me out,

Thanks,
Nagaraj,

1 Like

It looks like this is a very similar request to what you are trying to do:

Where you can point to the file, and then use the action script to find the area of the file you want to modify and then append it.

You should be able to use something designed to edit INI files to edit these values.

I have examples on BigFix.Me that use VBScript to do this.

In your case the ā€œFile Sectionā€ of the INI would be blank most likely. In my above example, Iā€™m actually putting the function call that does the INI edit at the very end of the VBScript file, which is originally from robvanderwoude.

I have other VBSscripts here: https://github.com/jgstew/tools/tree/master/VBS

For my query I have used the below query

// store the file location

parameter ā€œfilenameā€ = ā€œC:\Users\IBM_ADMIN\Desktop\install_config.txtā€

//Prompt the window
//action parameter query ā€œQRadarā€ with description " IP of the server: "

//Test appendfile
//appendfile {parameter ā€œQRadarā€ of action}

// iterate through the file replacing lines as necessary

appendfile {concatenation ā€œ%0d%0aā€ of ( if (it starts with ā€œConfigurationServer=ā€)then (ā€œConfigurationServer=88.99.33.66ā€ else it as string) of lines of file (parameter ā€œfilenameā€)}

// backup the old file (first delete any old backup files)

delete ā€œ{parameter ā€œfilenameā€}.bakā€

move ā€œ{parameter ā€œfilenameā€}ā€ ā€œ{parameter ā€œfilenameā€}.bakā€

// replace with the new file

move __appendfile ā€œ{parameter ā€œfilenameā€}ā€

the above command is working fine and changing the ā€œConfigurationServer=ā€ value as ā€œConfigurationServer=88.99.33.66ā€

but I need one more request want to edit like below

ā€œ(second line also want to edit the value like ā€œfrom (ConfigurationServerPort=2222) to ConfigurationServerPort=3333ā€)ā€

is there any parameter(and, or, if) have to use,

please suggest me on the same,

Thanks,
Nagaraj,

Iā€™d try the VBScripts that were mentioned above or you can also try to do a replace with powershell like below.

delete __createfile
delete c:\windows\temp\configUpdate.ps1
 
createfile until EOF
 
$path = "C:\Program Files (x86)\IBM\config.txt"
 
(Get-Content $path) -replace 'ApplicationIdentifier=BFDemo', 'ApplicationIdentifier=BFRelay' | Set-Content $path
(Get-Content $path) -replace 'ConfigurationServer=11.22.33.44', 'ConfigurationServer=44.33.22.11' | Set-Content $path
(Get-Content $path) -replace 'ConfigurationServerPort=2222', 'ConfigurationServerPort=4444' | Set-Content $path
 
EOF
 
copy __createfile c:\windows\temp\configUpdate.ps1
 
runhidden cmd /K powershell.exe -ExecutionPolicy Bypass c:\windows\temp\configUpdate.ps1

I thought about this a bit more and have updated it to be a bit more generic by checking if the settings exists with any value after the ā€˜=ā€™ and update it. It will also add the setting to the file if it doesnā€™t already exist and remove any newlines from the file.

delete __createfile
delete c:\windows\temp\configUpdate.ps1
 
createfile until EOF
 
$path = "C:\Program Files (x86)\IBM\config.txt"

if (Test-Path $path){{
    # Remove Newlines from the file
    (Get-Content $path) | ? {{$_.trim() -ne ""} | Set-Content $path

    # If ApplicationIdentifier exists replace the value.  If it doesn't exist add it to the file
    if(Select-String -Path $path -Pattern "(ApplicationIdentifier)\=(.*)"){{
        (Get-Content $path) -replace "(ApplicationIdentifier)\=(.*)", 'ApplicationIdentifier=BFRelay' | Set-Content $path
    }elseif(!(Select-String -Path $path -Pattern "(ApplicationIdentifier\b)")) {{Add-Content -Path $path "ApplicationIdentifier=BFRelay"}

    # If ConfigurationServer exists replace the value.  If it doesn't exist add it to the file
    if(Select-String -Path $path -Pattern "(ConfigurationServer)\=(.*)"){{
        (Get-Content $path) -replace "(ConfigurationServer)\=(.*)", 'ConfigurationServer=44.33.22.11' | Set-Content $path
    }elseif(!(Select-String -Path $path -Pattern "(ConfigurationServer\b)")) {{Add-Content -Path $path "ConfigurationServer=44.33.22.11"}

    # If ConfigurationServerPort exists replace the value.  If it doesn't exist add it to the file
    if(Select-String -Path $path -Pattern "(ConfigurationServerPort)\=(.*)"){{
        (Get-Content $path) -replace "(ConfigurationServerPort)\=(.*)", 'ConfigurationServerPort=4444' | Set-Content $path
    }elseif(!(Select-String -Path $path -Pattern "(ConfigurationServerPort\b)")) {{Add-Content -Path $path "ConfigurationServerPort=4444"}
	
}
 
EOF
 
copy __createfile c:\windows\temp\configUpdate.ps1
 
runhidden cmd /K powershell.exe -ExecutionPolicy Bypass c:\windows\temp\configUpdate.ps1

Can we do prompt the (changing) value using above method ?

Please help me out

Thanks,
Nagaraj,

Yes. You can use an action parameter query or just set the parameter in the action.

action parameter query "Path" with Description "Path to Config:"
action parameter query "ApplicationIdentifier" with Description "AppID:"
action parameter query "ConfigurationServer" with Description "Config Server IP:"
action parameter query "ConfigurationServerPort" with Description "Config Server Port:"

//parameter "Path" = ""
//parameter "ApplicationIdentifier" = ""
//parameter "ConfigurationServer" = ""
//parameter "ConfigurationServerPort" = ""

delete __createfile
delete c:\windows\temp\configUpdate.ps1
createfile until EOF
$path = "{parameter "Path" of action}"
(Get-Content $path) -replace ā€˜ApplicationIdentifier=BFDemoā€™, ā€˜ApplicationIdentifier={parameter "ApplicationIdentifier" of action}ā€™ | Set-Content $path
(Get-Content $path) -replace ā€˜ConfigurationServer=11.22.33.44ā€™, ā€˜ConfigurationServer={parameter "ConfigurationServer" of action}ā€™ | Set-Content $path
(Get-Content $path) -replace ā€˜ConfigurationServerPort=2222ā€™, ā€˜ConfigurationServerPort={parameter "ConfigurationServerPort" of action}ā€™ | Set-Content $path
EOF
copy __createfile c:\windows\temp\configUpdate.ps1

runhidden cmd /K powershell.exe -ExecutionPolicy Bypass c:\windows\temp\configUpdate.ps1

Thanks @dakota

For your updates and help,

we had created like below and it is working now,

// store the file location
parameter ā€œfilenameā€ = "C:\Program Files (x86)\IBM\config.txt"
parameter ā€œfilenamebakā€ = ā€œC:\Program Files (x86)\IBM\config.bakā€
//Prompt the window
action parameter query ā€œQRadarā€ with description ā€œQRadar Server IP:ā€
//Storing the data of QRadar
parameter ā€œQRadarIPā€ = ā€œ{parameter ā€œQRadarā€}ā€
// iterate through the file replacing lines as necessary
appendfile {concatenation ā€œ%0d%0aā€ of ( if (it starts with ā€œConfigurationServer=ā€) then (ā€œConfigurationServer=ā€ & (parameter ā€œQRadarIPā€)) else it as string) of ( if (it starts with ā€œConfigurationServerPort=ā€) then (ā€œConfigurationServerPort=ā€ & (parameter ā€œQRadarIPā€)) else it as string) of lines of file (parameter ā€œfilenameā€)}
// backup the old file (first delete any old backup files
delete ā€œ{parameter ā€œfilenamebakā€}.bakā€
//creating the file
move ā€œ{parameter ā€œfilenameā€}ā€ ā€œ{parameter ā€œfilenamebakā€}.bakā€
// replace with the new file
move __appendfile ā€œC:\Program Files (x86)\IBM\config.txtā€

Thanks,