1.

Solve : Batch Problem?

Answer»

Hi,

Can anyone get the below to work. As far as I can tell, there's no reason why it wouldn't (Sorry, I'm new to batch files, but comfortable with programming languages, so I *should* be able to understand all jargon thrown back at me )

CODE: [Select]@ECHO OFF

SET DefaultPath="C:\Program Files\WiredRed\EPop\EPopB.exe"

IF [%1]==[/?] GOTO HelpDisplay

IF [%1]==[] GOTO MissingArgs

IF NOT [%4]==[] (
IF [%5]==[] GOTO MissingArgs
)

IF [%2]==[] (
IF EXIST %DefaultPath% (
SET ExePath=%DefaultPath%
) ELSE (
SET ExePath=""
)
) ELSE (
IF EXIST %2 (
SET ExePath=%DefaultPath%
) ELSE (
IF EXIST %DefaultPath% (
SET ExePath=%DefaultPath%
) ELSE (
SET ExePath=""
)
)
)

The problem occurs at the IF [%2]==[] ( line. I keep getting "The syntax of the command is incorrect." when I don't supply a 2nd argument. It executes the else statement perfectly if I do supply a second argument.

What surprises me is that the syntax matches the above IF NOT [%4]==[] ( with the exception of the not command. Does anyone have any idea?

Thanks in advance.Try changing this line from IF EXIST %2 ( to IF NOT [%2]==[] (Hi oldun,

Thanks for the reply, but the problem doesn't occur at any stage in the ELSE segment. I would try that, but the EXIST segment is to test if the file exists, not whether or not the argument is null.If I correctly understand what you are attempting to achieve, the following should work:

Code: [Select]Set ExePath=""
If Exist %DefaultPath% Set ExePath=%DefaultPath%
IF Not [%2]==[] (
If Exist %2 ( Set ExePath=%DefaultPath% )
)
Quote from: oldun on February 06, 2011, 02:03:31 PM

If I correctly understand what you are attempting to achieve, the following should work:

Code: [Select]Set ExePath=""
If Exist %DefaultPath% Set ExePath=%DefaultPath%
IF Not [%2]==[] (
If Exist %2 ( Set ExePath=%DefaultPath% )
)

Thanks again for your post Oldun. Your close to what I'm trying to achieve, but to do it, your 4th line down would be ... ExePath=%2.

Basically, the idea of ExePath is to store the executable file's location. The user can send the filelocation as an argument (%2). There is a DefaultPath in case the file location sent as %2 doesn't exist (or if the user doesn't enter a location argument).

I've tried putting in your EXAMPLE (with the modification listed above) and it was still giving problems (") was unexpected at this time."). I thought it might be that you cannot set variables to argument values in this way, so I tried your example as typed... same error. I threw in a couple of ECHOs to find out thich closing bracket was the problem, and it was the final bracket. I thought it might have a problem with the nesting of brackets (for whatever reason), so I even tried it without brackets on the 4th line. Still the same error.

I can't see anything wrong with the code I first put up (or yours for that matter, because it is much easier to see that your code doesn't appear to have any problems visually). At this point I have to ask, what are the chances that it's a problem with the environment itself? (As worrying as that would be).Here is the COMPLETE script. This executes without errors when I run it.

Code: [Select]@ECHO OFF
SetLocal
SET DefaultPath="C:\Program Files\WiredRed\EPop\EPopB.exe"

IF [%1]==[/?] GOTO HelpDisplay

IF [%1]==[] GOTO MissingArgs

IF NOT [%4]==[] (
IF [%5]==[] GOTO MissingArgs
)

Set ExePath=""
If Exist %DefaultPath% Set ExePath=%DefaultPath%
IF Not [%2]==[] (
If Exist %2 ( Set ExePath=%2 )
)
I BUILT it from scratch starting with the code you posted. Seems to be working now for some reason. I'm even comparing it side-by-side to the amended one I tried earlier and they look the exact same WELL, thanks for your help, you certainly earned your thanks on this one.

Quick question though so I at least learn something from this project. I've added code onto the end of it. Why is it that one of these works and one doesn't?

Code: [Select]::Will work

IF NOT %ExePath%=="" ECHO Will Run
Code: [Select]::Won't work, at least for me anyway

IF NOT %ExePath%=="" (
ECHO Will Run
)
Rest assured I retyped it a couple of times to MAKE SURE that there were no dodgey unprintable characters that just appeared to be spaces.Did you tab that echo command batch has a heart attack when it reads a tabQuote from: mat123 on February 14, 2011, 12:45:13 AM
Did you tab that echo command batch has a heart attack when it reads a tab

Really?

i meant when a tab is in front of a commandQuote from: mat123 on February 14, 2011, 01:11:43 PM
i meant when a tab is in front of a command



Discussion

No Comment Found