Batch file and string parameters

  • Thread starter Thread starter brett
  • Start date Start date
B

brett

I'd like to use something similar to:

my.bat ui

for the following batch file.

@echo on
SET programToRun=%1

IF [/I] [%programToRun%] == []
(
@echo "stopped in wrong right place"
)
ELSE
(
IF [/I] [%programToRun%] == ["ui"]
)
@echo "stopped in right place"
))

However, the output is:

C:\TEST>SET programToRun=ui
[ui] was unexpected at this time.
C:\TEST>IF [/I] [ui] == []

What am I doing wrong?

I use the last IF since there will different parameters. I will have
an IF for each possible parameter, since there doesn't seem to be an
IF...ELSE IF.

Thanks,
Brett
 
You have several problems:
- Unmatched brackets
- Incorrect location of brackets
- Incorrect location of "else" statements

A little indentation would help. Try this instead:
@echo off
SET programToRun=%1
IF /I [%programToRun%]==[] (
echo "stopped in wrong place"
) ELSE (
IF /I [%programToRun%]==[ui] (
echo "stopped in right place"
)
)

or a little shorter:

@echo off
SET programToRun=%1
IF /I [%programToRun%]==[] (
echo "stopped in wrong place"
) ELSE (IF /I [%programToRun%]==[ui] echo "stopped in right place")
 
Unless you show us the batch file you use it is impossible
to tell you where the problem is.
 
brett said:
I pasted your code into a .bat file. Then I did

test.bat ui

Brett

Sorry, I won't comment until you paste the contents
of the batch file back into your reply.
 
@echo off
SET programToRun=%1
IF /I [%programToRun%]==[] (
echo "stopped in wrong place"
) ELSE (
IF /I [%programToRun%]==[ui] (
echo "stopped in right place"
)
)
 
brett said:
@echo off
SET programToRun=%1
IF /I [%programToRun%]==[] (
echo "stopped in wrong place"
) ELSE (
IF /I [%programToRun%]==[ui] (
echo "stopped in right place"
)
)

I called your your batch file "test.bat" and ran it in three ways:
- test.bat
- test.bat xxx
- test.bat ui
It ran perfectly well in each case. How did you run it?
 
I called your your batch file "test.bat" and ran it in three ways:
- test.bat
- test.bat xxx
- test.bat ui
It ran perfectly well in each case. How did you run it?

Same three ways you did (minus the "-" sign) but got the same error.

Brett
 
brett said:
Same three ways you did (minus the "-" sign) but got the same error.

Brett

I suspect you're running a different batch file than the one
you think you're running. To prove it, modify it
like so:
@echo off
echo This is the modified batch file
SET programToRun=%1
IF /I [%programToRun%]==[] (
echo "stopped in wrong place"
) ELSE (
IF /I [%programToRun%]==[ui] (
echo "stopped in right place"
)
)
Unless you see the message "This is the modified batch
file", you're not running the right batch file.

If you still get this error then there is something wrong
with your machine. If it bothers you then you can give
me access to your PC so that I can tell you what it is,
otherwise let it go.
 
Back
Top