0

I have a config file, The file is like mach.conf

Machine_IP = <IP Address> Machine_Name = <Machine name> User_Name = <UID> ERRORCODE = 8000 .. 

Likewise. I am new to windows batch script, though I had read on internet about batch scripting and able to understand & write simple batch scripts. But no where I find handling files using batch script, like I want to read the config file if I see ERRORCODE = 8000 do a change and replace 8000 with 8001.

How to do this using batch scripting. Please provide me links or some hints.

Thanks...

1 Answer 1

1

I've added a bit of code that may not be necessary, but allows the program to manipulate unquoted <'s and >'s. All it does is pre-pend a ^ to them, so < becomes ^< and > becomes ^>.

@echo off setlocal enabledelayedexpansion if exist config.tmp del config.tmp for /f "tokens=1* delims== " %%x in (config.cnf) do call :processFile "%%x" "%%y" move config.tmp config.cnf > nul echo File Updated endlocal goto :eof :processFile set "Var=%~1" set "Val=%~2" :: Add Escape Code to the greater-than and less-than symbols so they don't :: act as pipes and create errors. set "Var=%Var:<=^<%" set "Var=%Var:>=^>%" set "Val=%Val:<=^<%" set "Val=%Val:>=^>%" if "%Var%"=="ERRORCODE" set /a Val=%Val% + 1 echo %Var% = %Val%>> config.tmp 
Sign up to request clarification or add additional context in comments.

1 Comment

@jeb For several reasons. 1) <> is included in the pseudo data provided by the OP, though I doubt it would be in the actual data. 2) You should be able to extrapolate how to do it yourself from my examples. 3) To add them would have been more work. 4) I doubt that any of these characters will be in an actual configuration file, so it would be an unnecessary complication and distraction from the actual question asked. As it is the examples I gave make up nearly 1/3 of the code already and each additional character requires two more lines of code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.