如何编写Windows .bat文件实现HTTPPortNumber端口号自增
Batch Script to Increment HTTPPortNumber in TM1 Config File
Solution Overview
We’ll combine Batch and PowerShell commands in a single .bat file to handle reading the config, modifying the port value, and saving changes back—avoiding the variable storage issue you encountered earlier.
Complete Batch Script
@echo off setlocal enabledelayedexpansion :: Define your config file path set "CONFIG_FILE=C:\Work\Clients\bcg\Config\Tm1s.cfg" :: PowerShell command to modify the port number powershell -Command "$content = Get-Content '%CONFIG_FILE%'; $newContent = $content | ForEach-Object { if ($_ -match '^HTTPPortNumber=(\d+)$') { 'HTTPPortNumber=' + ([int]$matches[1] + 1) } else { $_ } }; $newContent | Set-Content '%CONFIG_FILE%'" echo HTTPPortNumber incremented successfully. endlocal
Key Step Breakdown
- Read the config:
Get-Content '%CONFIG_FILE%'loads the entire file into a variable. - Target the port line: The regex
^HTTPPortNumber=(\d+)$matches only lines exactly formatted asHTTPPortNumber=####, capturing the numeric port value. - Increment and rewrite: When a match is found, the captured port is converted to an integer, incremented by 1, and the line is reconstructed. All other lines remain unchanged.
- Save changes:
Set-Content '%CONFIG_FILE%'writes the modified content back to the original file.
Testing Tips
- Verify you have write permissions for the config file path.
- Test with a copy of the IBM sample cfg file first to prevent accidental data loss.
内容的提问来源于stack exchange,提问作者Red Arlint
相关产品推荐
相关产品推荐

