Why this if statement not working properly?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Why this if statement not working properly?

#1 Post by BoQsc » 08 Feb 2022 07:36

The output is:

Code: Select all

'qemu-img' is not recognized as an internal or external command,
operable program or batch file.
tssd
While there should be no output at all. Even when the C:\Users\Windows10\machines\disk1.img.qcow2 exists. These errors appear.

Code: Select all

@ECHO OFF

IF NOT EXIST "C:\Users\Windows10\machines\disk1.img.qcow2" (
	echo no
	pause

	echo tssd
	ECHO 3.Creating qcow2 local storage image of 10GB. %HOMEDRIVE%%HOMEPATH%\machines\disk1.img.qcow2
	qemu-img create -f qcow2 "C:\Users\Windows10\machines\disk1.img.qcow2" "10G"
	pause

	echo tssd
	ECHO 4. Example of resizing qcow2 local storage disk image (Resize to 20GB)
	qemu-img resize "%HOMEDRIVE%%HOMEPATH%\machines\disk1.img.qcow2" "20G"
		echo tssd
	ECHO.
	pause
)
pause 

Last edited by BoQsc on 08 Feb 2022 07:58, edited 2 times in total.

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

Re: Why this if statement not working properly?

#2 Post by ShadowThief » 08 Feb 2022 07:42

There is no program called qemu-img in any of the folders that are listed when you run echo %PATH%.

Try using the full path to qemu-img.exe in your script. Use double quotes if you've got spaces in the path.

BoQsc
Posts: 92
Joined: 30 Jun 2014 04:10

Re: Why this if statement not working properly?

#3 Post by BoQsc » 08 Feb 2022 07:52

That's not the point, why does it even show that error when it shouldn't, even when the file exists in that place.

Code: Select all

IF NOT EXIST "C:\Users\Windows10\machines\disk1.img.qcow2" (
The disk1.img.qcow2 exists on my system, but the errors outputs still appear.

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

Re: Why this if statement not working properly?

#4 Post by ShadowThief » 08 Feb 2022 08:53

Everything inside of your code block gets read in all at once, so the interpreter has to know where qemu-img is just in case it needs that information.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Why this if statement not working properly?

#5 Post by miskox » 08 Feb 2022 14:13

@shadow:

Code: Select all

@echo off
if exist %0 (
	echo xx
	sadfasdf asdfasdf
)

if exist somefile (
	echo xxx	
	sadfasdf asdfasdf
)

Result:

Code: Select all

C:\Users\saso>a.cmd
xx
'sadfasdf' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\saso>

(just added NOT to the second IF)

Code: Select all

@echo off
if exist %0 (
	echo xx
	sadfasdf asdfasdf
)

if NOT exist somefile (
	echo xxx	
	sadfasdf asdfasdf
)
Result:

Code: Select all

C:\Users\saso>a.cmd
xx
'sadfasdf' is not recognized as an internal or external command,
operable program or batch file.
xxx
'sadfasdf' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\saso>

So I guess interpreter does not need to know if 'sadfasdf.exe' exists...

Saso

(Win 10 20H2 19042.1466)
Last edited by miskox on 08 Feb 2022 14:16, edited 1 time in total.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Why this if statement not working properly?

#6 Post by miskox » 08 Feb 2022 14:15

I think the problem is with this:

Code: Select all

(Resize to 20GB)
change to:

Code: Select all

^(Resize to 20GB^)
Saso

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

Re: Why this if statement not working properly?

#7 Post by ShadowThief » 08 Feb 2022 15:31

That's definitely it, although you only need to escape the ).

Post Reply