-2

I'm making a small "lazy sleep" .bat file for Windows 10, and it works well. Except I get a "missing operand" at the top of the cmd windows when I start the program : cmd windows when I start the program I'm a newbie at Batch, so don't be harsh. I already went online but nothing matches my problem, and nothing I tried seems to fix it.

Here is my code (in French, sorry) :

@echo off
Rem 65001 for Unicode, 850 or 863 doesn't seem to work for French accents
chcp 65001

:annuler
set /a txt1=""
set /a tmp=0
set /p txt1="Veuillez préciser une unité : (h)eures, (m)inutes ou (s)econdes. (a)nnuler pour sortir du programme. "

IF /i "%txt1%"=="a" goto sortie
IF /i "%txt1%"=="h" goto heures
IF /i "%txt1%"=="m" goto minutes
IF /i "%txt1%"=="s" goto secondes

:heures

    set /p tmp="Dans combien d'heures souhaitez-vous mettre en veille prolongée ? 0 pour annuler : "
    IF /i "%tmp%"=="0" goto annuler
        set /a dur=tmp*3600
        timeout /t %dur%&&rundll32.exe powrprof.dll,SetSuspendState Sleep

:minutes

    set /p tmp="Dans combien de minutes souhaitez-vous mettre en veille prolongée ? 0 pour annuler : "
    IF /i "%tmp%"=="0" goto annuler
        set /a dur=tmp*60
        timeout /t %dur%&&rundll32.exe powrprof.dll,SetSuspendState Sleep

:secondes

    set /p tmp="Dans combien de secondes souhaitez-vous mettre en veille prolongée ? 0 pour annuler : "
    IF /i "%tmp%"=="0" goto annuler
        set /a dur=tmp
        timeout /t %dur%&&rundll32.exe powrprof.dll,SetSuspendState Sleep

:sortie

    pause

The question is "enter a unit (hour, minute, second) and then enter the value for the timer to got o sleep mode".

"annuler" means "cancel" "sortie" means "exit"

Thanks for any help you can bring.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • *"missing operand" at the top* is vague. What does *at the top* mean specifically? *at the top* says that it's on the `@echo off` line, which clearly does not produce a *missing operand* error. Neither does the next line that starts with `Rem`, so the problem is not *at the top*. – Ken White Nov 01 '19 at 04:06
  • It means, that when I start the program, I get the following lines : Page de codes active : 65001 Il manque un opérande (There is a missing Operand) – Kl3m Michard Nov 01 '19 at 04:15
  • Then remove the `@echo off` and figure out a more specific location than *at the top*. Where exactly is it producing that error? We're happy to try to help, but you need to do the basic work of telling us where the problem is located, and *at the top* does not do so. Batch files do not run to the end and then randomly say *there was an error somewhere before here*. They output the error at the line where it happens. If need be, insert a few `pause` statements every few lines to help isolate the issue. – Ken White Nov 01 '19 at 04:16
  • Ok, what you meant to say is "@echo off is a command that prevent the console from showing you where the problem is, you'd better delete the line for now". The problem is at line 5 > set /a txt1="" – Kl3m Michard Nov 01 '19 at 04:21
  • There is bountiful help on this site, if you make an effort to clearly state the problem and provide the details. I've explained the things that are unclear in your question as stated, and offered suggestions to help you narrow the scope of your question in order to try to help. It's unfortunate that you feel that asking you to cooperate in solving your problem is inconvenient. (I do see now that you snuck in an edit to your comment while I was writing my last one. Sorry I couldn't see what wasn't there when I responded. I also see you made other edits to your comments as well.) – Ken White Nov 01 '19 at 04:23
  • My answer is above. I'm sorry but your explanations were not clear to me. Not all who code are professionals and not all who come here are long-time coders in any languages they code in. EDIT : does the problem resides with the text variable ? How can I set a text variable other than blank "" ? Thanks – Kl3m Michard Nov 01 '19 at 04:27

2 Answers2

1

I figured it out.

The problem was that the /a switch is to set numerical values. I removed it from line 5 for the txt1 variable since it should be text.

Found the answer on tutorialspoint.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • The `/A` option is used when performing an arithmetic function. As neither of your `set /a` commands, _(lines `6` and `7`)_, are performing arithmetic, they're both technically incorrect, as they should just be `set` commands, _(`Set "txt1="` and `Set "tmp=0"`)_. When creating a variable from the result of that arithmetic, the value, although visibly an integer is still a string value, not numerical. – Compo Nov 01 '19 at 15:00
0

Your batch file's first Set /P command would be better, (as it is expecting specific entries only), using choice.exe instead. In the case of your second Set /P command, you need to understand that the end user, unlike with the choice command, is able to enter nothing at all or absolutely anything. This means that unless you validate the input the subsequent commands may not work. Also your code was very repetetive, so I have restructured it for you, and in doing so, have removed your problematic set /a:

@Echo Off
ChCp 65001 >NUL
choice /C hmsa /N /M "Veuillez préciser une unité : (h)eures (m)inutes ou (s)econdes. (a)nnuler pour sortir du programme."
If ErrorLevel 4 GoTo :EOF
If ErrorLevel 3 Set "str=e secondes"&Set "mult=1"
If ErrorLevel 2 Set "str=e minutes"&Set "mult=60"
If ErrorLevel 1 Set "str='heures"&Set "mult=3600"

:Combien
Set "dur="
Set /P "dur=Dans combien d%str% souhaitez-vous mettre en veille prolongée? "
Echo "%dur%"|findstr /R  "^\"[0-9]*\"$">NUL||GoTo Combien
Set /A dur *=mult
Timeout /T %dur% /NoBreak
rundll32 powrprof.dll,SetSuspendState Sleep
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thanks. I can't promote your answer yet, no enough reputation. – Kl3m Michard Nov 01 '19 at 18:53
  • It actually doesn't work properly. – Kl3m Michard Nov 05 '19 at 18:35
  • @Kl3mMichard, how about you try to explain what you mean? My answer works perfectly to remove the problematic `Set /A` command 'missing operand' errors. The script posted exactly as above works properly. Possible problems are, you'll need to ensure before running it that your default `cmd.exe` font is not `Raster Fonts`, _(should not be if using `Windows 10`)_. The next thing I'd suggest is that you do not use code page `65001`, instead opting for `1252`. Finally your suspend command will only sleep if you do not have hibernate enabled, if you do, then you should issue `PowerCfg -H Off` first. – Compo Nov 05 '19 at 19:39
  • Well, it's quite simple. When I start the program, it offers me the first question. Whatever I chose (m, h, s, or a) h is the answer he gets, so only hours get selected. Then I type any number, and if there is a 0 inside, it restarts the program to the first question (like 10, or 350). Finally, when I hit the enter key to run the timer, the program stops and the invite closes. – Kl3m Michard Nov 09 '19 at 17:03
  • @Kl3mMichard, what you're telling me is happening is absolutely not possible, unless you've changed something. When you test my script you must only test the script exactly as posted, my answer is not to be modified in any way until it has been verified as a solution. The code was tested on systems I had available to me, Windows Vista and Windows 7, _(both with the font changed to Lucida Console)_, and Windows 10. In all cases the codepage was changed to `1252` as previously advised, _(their defaults were `850`)_. **The code works exactly as designed and stated for the purposes of the task.** – Compo Nov 09 '19 at 18:32
  • Well, it seems on my computer it doesn't. I changed nothing. Thanks for all you've done. – Kl3m Michard Nov 09 '19 at 19:55
  • @Kl3mMichard, please provide additional details about your computer, and we may be able to determine whether the issue is the [tag:batch-file] content, `cmd.exe`, the locale/PC settings, or the Operating System. If there's something I can do to make my script more robust, I'd sure appreciate the opportunity. – Compo Nov 09 '19 at 20:10
  • Tell me what command you want me to input to the invite to get infos, I'll get it. I'm on windows 10, it's a tower, built 7 years ago. I'm usually on Ubuntu, but this one has games and lots of things, so I kept it this way. – Kl3m Michard Nov 09 '19 at 20:27