if/elseif dont work like expected...Sorry, this time in English

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Chronofos
Posts: 6
Joined: 16 Mar 2024 11:09

if/elseif dont work like expected...Sorry, this time in English

#1 Post by Chronofos » 22 Mar 2024 17:38

Within a "project" I wrote a batch suboutine, which should display help text and catch errors when calling it.

Code: Select all

if "%1"=="/help" (
     ECHO Ausgabe Hilfe
     cmd /k
) else if NOT exist %1 (
     ECHO Datei existiert nicht^! Abbruch^!
     cdm /k
) else if "%~x1"=="" (
     ECHO Keine DateiErweiterung^! Abbruch^!
     cdm /k
)  else if NOT "%~x1"==".txt" (
     ECHO DateiErweiterung ist nicht txt^! Abbruch^!
     cdm /k
)  else if "%~x1"==".txt" (
     ECHO DateiErweiterung ist %~x1
Results/Output
[There are the files: textfile.txt / textfile / textfile.pdf]

Code: Select all

O:\>prog /help
Ausgabe Hilfe

O:\>prog dont.exist.txt
Datei existiert nicht  Abbruch!

O:\>prog textfile
Keine DateiErweiterung! Abbruch!

O:\>prog textfile.pdf
DateiErweiterung ist nicht txt! Abbruch!

O:\>prog textfile.txt
DateiErweiterung ist .txt
I have now tried to extend the batch program to query whether a parameter was passed in the command line. Since obviously IF DEFINDED/ IF NOT DEFINED seem to have a problem with the variable %1, I used an "intermediate variable" tempvar_param_1 in which I stored %1 with set tempvar_param_1=%~1%.
This also works, both with IF DEFINED and with IF NOT DEFINED

Code: Select all

@echo off
set tempvar_param_1=%~1%
if defined tempvar_param_1 (ECHO bei IF DEFINED - Variable ist definiert) else (ECHO bei IF DEFINED - Variable ist nicht definiert)
if NOT defined tempvar_param_1 (ECHO bei IF NOT DEFINED -Variable ist nicht definiert) else (ECHO bei IF NOT DEFINDE Variable ist definiert)
When called without passing parameters

Code: Select all


O:\>prog
bei IF DEFINED - Variable ist nicht definiert
bei IF NOT DEFINED -Variable ist nicht definiert
So that works too.
I'm now trying to include this in the IF/ELSE block

Code: Select all

@ECHO OFF
set tempvar_param_1=%~1%

if defined tempvar_param_1 (
     ECHO bei IF DEFINED - Variable ist definiert
     cmd /k
) else if "%1"=="/help" (
     ECHO Ausgabe Hilfe
     cmd /k
) else if NOT exist %1 (
     ECHO Datei existiert nicht^! Abbruch^!
     cdm /k
) else if "%~x1"=="" (
     ECHO Keine DateiErweiterung^! Abbruch^!
     cdm /k
)  else if NOT "%~x1"==".txt" (
     ECHO DateiErweiterung ist nicht txt^! Abbruch^!
     cdm /k
)  else if "%~x1"==".txt" (
     ECHO DateiErweiterung ist %~x1---
When I call it, I get the error message: Syntax error.

Code: Select all

O:\>prog
prog
Syntaxfehler.
Where is my mistake in thinking? Where is my error in code?

ShadowThief
Expert
Posts: 1162
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: if/elseif dont work like expected...Sorry, this time in English

#2 Post by ShadowThief » 22 Mar 2024 22:54

If that's the entire code, then you're missing the final )

You've also written cdm /k instead of cmd /k (although I don't see why you'd need that at all since it only spawns a child terminal process), but that throws a different error.

OJBakker
Expert
Posts: 88
Joined: 12 Aug 2011 13:57

Re: if/elseif dont work like expected...Sorry, this time in English

#3 Post by OJBakker » 23 Mar 2024 03:24

Code: Select all

if /I "%~1"=="/help" (
     ECHO Ausgabe Hilfe
     cmd /k
) else if NOT exist "%~1" (
     ECHO Datei existiert nicht^! Abbruch^!
     cmd /k
) else if /I "%~x1"=="" (
     ECHO Keine DateiErweiterung^! Abbruch^!
     cmd /k
)  else if /I NOT "%~x1"==".txt" (
     ECHO DateiErweiterung ist nicht txt^! Abbruch^!
     cmd /k
)  else if /I "%~x1"==".txt" (
     ECHO DateiErweiterung ist %~x1
)
There is no need for intermediate variables.
changes:
added missing closing parenthesis
changed 'cdm /k' to 'cmd /k'
made if-comparisons case-insensitive
handle filenames with spaces, example: "My File.txt"
prevent syntaxerror with 'if exist %1'
The double quote's around %1 ensure that 'if exist' has the required parameter.
The tilde ~ is needed in case the filename itself has surrounding double quote's. This is necessary for files with a space in the filename.

examples: prog file.txt
prog "file.txt"
prog my file.txt
prog "my file.txt"

Chronofos
Posts: 6
Joined: 16 Mar 2024 11:09

Re: if/elseif dont work like expected...Sorry, this time in English

#4 Post by Chronofos » 23 Mar 2024 12:15

Hi OJBakker!
Thank you for your quick and competent help! You've helped me a lot.

Post Reply