Help with a Batch File

August 21, 2011 at 18:04:52
Specs: Windows 7, 3.0GHz, 8GB
I'm trying to write a batch file where when a value is entered at the command line, it will set a value in a command, but if another value is entered, that other value is set. This is the batch file:

set a="Error"
set b="Working"

set x=ERROR
set y=WORKING

if a==%5 set %5=%x%
if b==%5 set %5=%y%


c:\command.exe -p post.server.com -table DATA_TABLE -variable host=%1;location=%2;origin=%3;object=%4;severity=%5;

So if I run the batch file and enter this after the batch file:

batchcommand.bat "value1" "value2" "value3" "value4" "Error"

It will execute the command at the CLI, but the output is this:

c:\command.exe -p post.server.com -table DATA_TABLE -variable host="value1";location="value2";origin="value3";object="value4";severity="Error";

This is not substituting the values I want (ERROR and WORKING) for the values I enter and I'm running out of patience to make it work. What is wrong with this and why isn't it working? I have closed the command window after I make changes so the variables are removed. I'm not good with writing code, so if it looks REALLY bad, please note I'm new to writing code.

Thanks if you guys can help!


See More: Help with a Batch File

Report •

#1
August 23, 2011 at 08:13:23
Ok, I've tried to simplify this a bit.

if Error == %8 SET %8 = ERROR else SET %8 = WORKING

c:\program.exe -p post.server.com -table DATA_TABLE -variable host=%1;location=%2;origin=%3;object=%4;severity=%5;

So if I run the batch file and enter this after the batch file:

batchcommand.bat "value1" "value2" "value3" "value4" "Error"


I get this output still.

c:\program.exe -p post.server.com -table DATA_TABLE -variable host="value1";location="value2";origin="value3";object="value4";severity="Error";

Anyone have any direction on how I should manage this? I think my issue is with the %8 definition, but the output from the CLI shows that it's reading %8 as Error in every place I have %8 listed. So why doesn't the IF/ELSE work?


Report •

#2
August 23, 2011 at 08:22:52
Don't use command.exe. It's a DOS emulation environment, and I doubt you're running legacy 16-bit applications.

How To Ask Questions The Smart Way


Report •

#3
August 23, 2011 at 08:26:40
Sorry, I changed the post. The batch file triggers an executable to run. I renamed it program.exe. Sorry for the confusion.

Report •

Related Solutions

#4
August 23, 2011 at 08:29:07

Report •

#5
August 23, 2011 at 08:54:05
Ok, well I read through that and other than a minor change, I believe I am doing this correctly here:

IF "%5" == Error (SET %5 = ERROR) ELSE (SET %5 = WORKING)

program.exe -n rivendell -table TABLE_DATA -variables "host=%1;location=%2;origin=%3;object=%4;severity=%5"

Output is this:
c:\>batchfile.bat one two three four Error

c:>IF "Error" == Error (SET Error = ERROR) ELSE (SET Error = WORKING)

c:\>program.exe -n rivendell -table TABLE_DATA -variables mc_"host=one;location=two;origin=three;object=four;severity=Error"

This is the only thing I see that I may have been doing wrong. OBVIOUSLY, I am still doing something wrong, as it's still not working, but I didn't see anything from the site that I'm not doing here.

String1 == String2
Specifies a true condition only if String1 and String2 are the same. These values can be literal strings or batch variables (for example, %1). You do not need to use quotation marks around literal strings.

So that tells me that I needed some or could use quotes around the variable, which is %5. I added that and no issues.

I'm using If and providing a condition evaluation. After the evaluation I have a command (set) and if that isn't met, another condition command (else and then set). I've incorporated parentheses for each of the set options and I'm not receiving any errors when this executes as I have echo turned on.

So still doing something wrong, but don't know what that is.


Report •

#6
August 23, 2011 at 09:02:00
You do not need to use quotation marks around literal strings.
You technically don't have to, but it's recommended really hard, because the quotes are included with the string comparison.
IF "%5"=="Error" (SET %5 = Error) ELSE (SET %5 = WORKING)

How To Ask Questions The Smart Way


Report •

#7
August 23, 2011 at 09:45:34
Ok, this just isn't working. I'm looking at doing a GOTO where I have the same output, but I have both lines built, one with ERROR and one with WORKING. This is what I've tested and works. Not sure why the SET wasn't working, but it wasn't. This is just easier. If I echo off the output for the IF statements though, I don't see the command output for the execution of the program. :( Can I have it echo the GOTO command and not echo the IF statements?


IF "%5" == "Error" (GOTO ERROR)
IF "%5" == "Error" (GOTO WORKING)

:ERROR
program.exe -n rivendell -table TABLE_DATA -variables "host=%1;location=%2;origin=%3;object=%4;severity=ERROR"
@echo off
goto:eof

:WORKING
program.exe -n rivendell -table TABLE_DATA -variables "host=%1;location=%2;origin=%3;object=%4;severity=WORKING"
@echo off
goto:eof


Report •

Ask Question