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,