1

I run a batch skript where I want to use a third input as optional input. I tried

some code...

If Not "%~3"=="" (

    echo hello world >> %3
)
some code...

The skript works fine as long I actually pass some third parameter but breaks once I skip it. Unfortunately, I am unaware of error handling in batch. I cannot even say why the program breaks.

Can you help me with either the error handling or the solution?

Best, Peter

Peter Frey
  • 361
  • 4
  • 17
  • `If Not "%~3"==""` would be 100% correct written `If Not "%~3" == ""` with spaces around the third argument `==`. The syntax error causing exit of batch file processing is inside the command block executed if the batch file is started with three arguments. So you have not posted the code which we need to see to tell you what is wrong in this command block. See also [debugging a batch file](https://stackoverflow.com/a/42448601/3074564). – Mofi May 25 '20 at 08:00
  • 1
    Please note that Windows command processor processes the entire command block starting with `(` and ending with matching `)` before executing the command __IF__. So with no third argument string passed to the batch file the command block must be nevertheless fully syntactically correct. Better would be to use `if "%~3" == "" goto Label` and write the code for third argument string existing not in a command block as it is obviously done now. – Mofi May 25 '20 at 08:28
  • @Mofi: Right you are. Thanks. So actually the if code was a simple echo hello world >>%3, so I did not believe it could hurt. But of course if %3 does not exist. this cannot be processed. Yur solution with the label does the job. – Peter Frey May 25 '20 at 09:03
  • 1
    The basic template for what you're doing is `If Not "%~3" == "" ((Echo hello world) 1>> "%~3" 2> NUL)`, or possibly more accurately, `If Not "%~3" == "" (If Exist "%~3" ((Echo hello world) 1>> "%~3") Else ((Echo hello world) 1> "%~3" 2> NUL))`. – Compo May 25 '20 at 09:39
  • @Compo: Thanks. I actually prefer your version as it looks less nested. why do you make the extra parenteses around echo? It seems like if Not "%~3" == "" ( echo hello world >> "%~3" ) does the job already. However, please answer my question so I can mark it as answered. – Peter Frey May 25 '20 at 12:06
  • Because, @Peter, it's a template, a template is built in such a way as to ensure that specific parts are kept together, aid your learning, and forge habits. It will help you to prevent mistakes and to remember, when you inevitably decide to remove spaces, redirection handles and parentheses, _(but please never doublequotes!)_, what could happen. Try this: `echo U R the 1>"test file1.log"` and then try this `(echo U R the 1)>"test file2.log"`. Now you could have used `echo U R the 1 > "test file3.log"` as in your example, but that would then output an unwanted trailing space character. – Compo May 25 '20 at 12:21
  • @PeterFrey, feel free to post your own answer using what you've learned from the comment section, and if you do not receive further, more suitable, answers, you could mark your own answer as accepted. – Compo May 25 '20 at 12:25

1 Answers1

1

So basically this is the key information to find the right answer:

The Windows command processor processes the entire command block starting with ( and ending with matching ) before executing the command IF. So with no third argument string passed to the batch file the command block must be nevertheless fully syntactically correct.

The most simple way to archive this would be:

If Not "%~3" == "" ( echo hello world >> "%~3" )

As the extra duoblequoes around %~3 will ensure correct syntax.

One can be safer by using:

If Not "%~3" == "" (
    If Exist "%~3" ((Echo hello world) 1>> "%~3") 
    Else ((Echo hello world) 1> "%~3" 2> NUL)
)
Peter Frey
  • 361
  • 4
  • 17