Solved Bat file Replace text if its not correct inform user if ok

September 3, 2015 at 11:15:40
Specs: Windows 7
Hi all
I have a folder named c:\Ed inside the folder is a file called coms.cfg on line 7 SerialPort2=1 sometimes this gets changed to SerialPort2=0
I need a script that will ask the user to make sure they run another file before running this batch file and if line 7 is correct SerialPort2=1 to contact support.

Thanks in advance for the help


See More: Bat file Replace text if its not correct inform user if ok

Report •


#1
September 4, 2015 at 02:14:44
✔ Best Answer
@Echo off
setlocal enabledelayedexpansion
for /f "tokens=1,2* delims==" %%A in (coms.cfg) do (
        Set %%A=%%B
)
if !SerialPort2!==0 (
        ::Tell the user stuff
)
if !SerialPort2!==1 (
        ::Do some other things
)

Hopefully this'll help shed some light on what you want to accomplish

99 little bugs in the code,
99 little bugs.
Take one down, patch it around,
129 little bugs in the code.


Report •

#2
September 4, 2015 at 04:29:21
Thank you so much for the help this is going to save a ton of time.

Report •

#3
September 4, 2015 at 05:46:02
Can anyone explain what these do below trying to break them down, I tried running it and get this error ) was unexpected at this time.

1> for /f "tokens=1,2*
2> delims==" %%A
This one I got in (coms.cfg)
3>do (
4>Set %%A=%%B

I get that cons.cfg is the file but do I need to put anything into or replace A and B with the SerialPort info?

Also when I ran it it does not display anything on the screen to make sure the end users closes the other program before. I don't mean any disrespect but I'm to stupid to learn coding this late in life nor do I really wan't to :) but willing to give it a go.
Thanks


Report •

Related Solutions

#4
September 4, 2015 at 08:54:13
1, 2 and 3:
for /f "tokens=1,2* delims==" %%A in (coms.cfg) do (

Reads the file, putting token 1 into %%A, and the rest after the first delim. (=) in %%B, then executes the code in the DO clause (The clause must be closed with a ")").
Example: If the delim. is ":", then the stuff on before it will become %%A, and the stuff after it will become %%B. If there's an asterisk provided in say "tokens=1,2*" then the second token will contain EVERYTHING after the first delim. regardless of any other colon signs appearing later in the string.

4: If your file contains strings like:

something=value

Then the for loop will create a variable called "something" and assign the value "value" to it. Since your file contains either "SerialPort2=1" or "SerialPort2=0", it will create a variable in the script called "SerialPort2" and assign the value provided in the coms.cfg file.

You can read more about FOR in the command prompt. Enter:

for /?

Hope this helped

99 little bugs in the code,
99 little bugs.
Take one down, patch it around,
129 little bugs in the code.


Report •


Ask Question