Page 1 of 1

if else [solved]

Posted: 23 Feb 2023 07:32
by masterfrog
Hello together,

i have a problem with a simple task.
This is my one-line-code which is directly run in cmd.exe (not in an Batch-File):

Code: Select all

C:\Temp>if not exist test.txt ( echo "not there" ) else ( echo "it's there" ) & echo "let's do the next things"
My Problem is, that the code echos only "not there" if the test.txt doesn't exist. But it doesn't echo the "let's do the next things".
The Task should check if the file exists or not and no matter what the result is, it should continue wich the statement after the last &.
Unfortunately it interprets the "& echo "let's do the next things" as a part of the else statement.

I can't solve it in a batch file because of restrictions, so i need to do it directly on the prompt line.

It would be great, if someone can help me.
I am using windows 10.

Kind regards
Stefan

Re: if else

Posted: 23 Feb 2023 09:01
by Aacini
Use this way:

Code: Select all

C:\Temp> ( if not exist test.txt ( echo "not there" ) else echo "it's there" ) & echo "let's do the next things"
Antonio

Re: if else

Posted: 24 Feb 2023 03:12
by masterfrog
Helllo Antonio,

thank you very much for your fast answer.
It works! :D

Kind regards
Stefan