Articles

Solved cmd Programing and .bat Question about variables in a prgm.

January 15, 2013 at 12:48:10
Specs: Windows 7

In some Win 7 computers the command line find no command...
when you would open this commands so, that they make sense, you must write in programmed cmd C:\Windows\System32\ bevfore the command, e.g. :
C.\Windows\System32\shutdown /s /f /t 0

btw a question:
I have written a program, but allways says this "Missing Operand"
The Program:

@echo off
@echo Zeitlich gesetztes Herunterfahren
@echo Stunden:
set/P Stunden =
@echo Minuten:
set/P Minuten =
@echo Sekunden:
set/P Sekunden =
set/a var = 3600* %Stunden% + 60* %Minuten% + %Sekunden%
C:\Windows\System32\shutdown /s /f /t %var%
@echo Zum Abbrechen a eingeben:
set/P Eingabe=
if %Eingabe% == a goto abbrechen

:abbrechen
C:\Windows\System32\shutdown /a

Pause

Can someone thell me my mistakes?
When you don't understand where the prgm is for:
I would try to write a prgm for a simple shutdown, where I musn't calculate the seconds...


See More: cmd Programing and .bat Question about variables in a prgm.

Report •


✔ Best Answer
January 15, 2013 at 19:14:21

It could also be the infamous "set /p" trap: If you don't set the target var prior to the set /p statement, it can really kill your party, especially when there's no "setlocal" in the preceding code. It will fly, wrongly, with all but the last undefined. If the last is undefined, then you will get your missing operand (set /a x+y+) Also, if prompting for values, it is a good idea to tell the user what he is being prompted for (as mike demonstrated). So, here's this further addition to the mix, using mike's code as the launching pad:
@echo off & setlocal
echo Zeitlich gesetztes Herunterfahren
set stunden=
set /P Stunden=Stunden:
if not defined stunden echo undefined stunden element. Bailing out& goto :eof
::set /a Stunden=%Stunden%
set minuten=
set /P Minuten=Minuten:
if not defined minuten echo undefined minuten element. Bailing out & goto :eof
::set /a Minuten=%Minuten%
set sekunden=
set /P Sekunden=Sekunden:
if not defined sekunden echo undefined sekunden element. Bailing out & goto :eof
set /a var =(3600*Stunden+60*Minuten+Sekunden)
C:\Windows\System32\shutdown /s /f /t %var%
echo Zum Abbrechen a eingeben:
set eingabe=
set /P Eingabe=
if %Eingabe% == a goto abbrechen
:abbrechen
C:\Windows\System32\shutdown /a
echo shutdown aborted.
Pause



#1
January 15, 2013 at 13:18:19

It's a timing issue. Put in a short timeout after your variables are declared. Like this:

@echo off
@echo Zeitlich gesetztes Herunterfahren
@echo Stunden:
set/P Stunden =
@echo Minuten:
set/P Minuten =
@echo Sekunden:
set/P Sekunden =
sleep 1
set/a var = 3600* %Stunden% + 60* %Minuten% + %Sekunden%
C:\Windows\System32\shutdown /s /f /t %var%
@echo Zum Abbrechen a eingeben:
set/P Eingabe=
if %Eingabe% == a goto abbrechen
:abbrechen
C:\Windows\System32\shutdown /a


Report •

#2
January 15, 2013 at 13:27:43

::This should make things work better. As far as I know in order to do batch math
::The variables that are being prompted have to be changed with a set /a
echo Zeitlich gesetztes Herunterfahren
set /P Stunden="Studen= "
set /a Stunden=%Stunden%
set /P Minuten="Minuten= "
set /a Minuten=%Minuten%
set /P Sekunden="Minuten= "
set /a Sekunden=%Minuten%
set /a var =(3600*Stunden+60*Minuten+Sekunden)

C:\Windows\System32\shutdown /s /f /t %var%
@echo Zum Abbrechen a eingeben:
set/P Eingabe=
if %Eingabe% == a goto abbrechen
:abbrechen
C:\Windows\System32\shutdown /a
Pause

:: mike


Report •

#3
January 15, 2013 at 15:58:31

Gambit007: when you would open this commands so, that they make sense, you must write in programmed cmd C:\Windows\System32\ bevfore the command
Their PATH variable is messed up. You can verify this with the PATH command.

jowah: It's a timing issue.
What? No it's not. CMD is single threaded. It runs commands sequentially, not in parallel.

mikelinus: The variables that are being prompted have to be changed with a set /a
All variables are saved as text, so you don't need to pass it though SET /A. The issue, which you fixed without comment, is the space between the variable name and the equals sign.

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
January 15, 2013 at 18:23:26

@Razor2.3 I have has issues in the past with numbers drawn in from a /p not working in a math function. I always assumed it needed to be passed through a /a. I apparently failed to copy and paste the line about spaces. Thank you.

:: mike


Report •

#5
January 15, 2013 at 19:14:21
✔ Best Answer

It could also be the infamous "set /p" trap: If you don't set the target var prior to the set /p statement, it can really kill your party, especially when there's no "setlocal" in the preceding code. It will fly, wrongly, with all but the last undefined. If the last is undefined, then you will get your missing operand (set /a x+y+) Also, if prompting for values, it is a good idea to tell the user what he is being prompted for (as mike demonstrated). So, here's this further addition to the mix, using mike's code as the launching pad:
@echo off & setlocal
echo Zeitlich gesetztes Herunterfahren
set stunden=
set /P Stunden=Stunden:
if not defined stunden echo undefined stunden element. Bailing out& goto :eof
::set /a Stunden=%Stunden%
set minuten=
set /P Minuten=Minuten:
if not defined minuten echo undefined minuten element. Bailing out & goto :eof
::set /a Minuten=%Minuten%
set sekunden=
set /P Sekunden=Sekunden:
if not defined sekunden echo undefined sekunden element. Bailing out & goto :eof
set /a var =(3600*Stunden+60*Minuten+Sekunden)
C:\Windows\System32\shutdown /s /f /t %var%
echo Zum Abbrechen a eingeben:
set eingabe=
set /P Eingabe=
if %Eingabe% == a goto abbrechen
:abbrechen
C:\Windows\System32\shutdown /a
echo shutdown aborted.
Pause


Report •

#6
January 17, 2013 at 13:53:28

thx for all answers :)

Report •


Ask Question