Page 1 of 1
Why this if statement not working properly?
Posted: 08 Feb 2022 07:36
by BoQsc
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
Re: Why this if statement not working properly?
Posted: 08 Feb 2022 07:42
by ShadowThief
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.
Re: Why this if statement not working properly?
Posted: 08 Feb 2022 07:52
by BoQsc
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.
Re: Why this if statement not working properly?
Posted: 08 Feb 2022 08:53
by ShadowThief
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.
Re: Why this if statement not working properly?
Posted: 08 Feb 2022 14:13
by miskox
@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)
Re: Why this if statement not working properly?
Posted: 08 Feb 2022 14:15
by miskox
I think the problem is with this:
change to:
Saso
Re: Why this if statement not working properly?
Posted: 08 Feb 2022 15:31
by ShadowThief
That's definitely it, although you only need to escape the ).