2

I need to create a windows .bat file that:

  1. finds HTTPPortNumber=#### in a file
  2. adds 1 to ####
  3. saves that HTTPPortNumber=NEWPORT to the file.

Basically I am trying to increment the port number in a config file by 1 and save it.

I have been playing with powershell eg:

powershell -Command "(Select-String "HTTPPortNumber=*"  c:\Work\Clients\bcg\Config\Tm1s.cfg)"

But I can't figure out how to pipe the result into a variable so I can run substr on it.

You can find a sample cfg file here for testing.

Compo
  • 36,585
  • 5
  • 27
  • 39
Red Arlint
  • 21
  • 1

1 Answers1

1

Here is a beginning example, but you should give us more details to understand more your aim :


@echo off
setlocal enabledelayedexpansion

set "filename=c:\Work\Clients\bcg\Config\Tm1s.cfg"

for /f "usebackq tokens=2 delims==" %%i in (`findstr /r /c:"HTTPPortNumber=.*" %filename%`) do (
  set "port=%%i"
  set /a "newport=port+1"
  set "newline=HTTPPortNumber=!newport!"
  echo Found HTTPPortNumber=!port!. Incrementing to !newport!.
  type %filename% | findstr /v /c:"HTTPPortNumber=.*" > tempfile.txt
  echo !newline! >> tempfile.txt
  move /y tempfile.txt %filename% > nul
)

echo Done.
pause

This script uses the findstr command to search for the line containing HTTPPortNumber= in the specified file.

The line is then processed using the for /f loop, where usebackq tokens=2 delims== is used to parse the line and extract the second token (the port number).

The port number is then incremented by 1 and saved back to the file by creating a temporary file with the updated line and then moving the temporary file back to the original file.


Edit : Powershell Solution


$filename = 'c:\Work\Clients\bcg\Config\Tm1s.cfg'
$fileContent = Get-Content $filename
$newContent = ''

foreach ($line in $fileContent) {
  if ($line -match 'HTTPPortNumber=\d+') {
    $port = [int] ($line -replace 'HTTPPortNumber=(\d+)', '$1')
    $newport = $port + 1
    Write-Output "Found HTTPPortNumber=$port. Incrementing to $newport."
    $newContent += "HTTPPortNumber=$newport" + [Environment]::NewLine
  } else {
    $newContent += $line + [Environment]::NewLine
  }
}

Set-Content $filename $newContent
Write-Output 'Done.'

This Powershell script will search for the line HTTPPortNumber=#### in the file c:\Work\Clients\bcg\Config\Tm1s.cfg and increment the value of #### by 1.

The updated value will be saved back to the same file.

Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • Hackoo, I have added a link to a sample cfg file in the main post. Whilst it does appear to have only one section, I'm not sure if it is possible to have others, and if the comments are always there, then it may be less than ideal for the new value just to be added to the bottom of the file anyhow. In this case I would expect that the intention is to increment the value, but with no line positions changing. – Compo Feb 11 '23 at 12:30
  • Yes I don't want to change the line position, but I think I can work with it from there with the help of https://stackoverflow.com/questions/60034/how-can-you-find-and-replace-text-in-a-file-using-the-windows-command-line-envir – Red Arlint Feb 11 '23 at 15:31
  • I just realized that findstr wasn't working with the file encoding. Thanks between what you posted and what I just figured out I got it. – Red Arlint Feb 11 '23 at 15:53
  • @RedArlint Please check edited answer with the PowerShell script solution ! – Hackoo Feb 11 '23 at 16:16
  • Thnx that's a lot less obscure. I will look into powershell tonight – Red Arlint Feb 13 '23 at 13:52