Why doesnt this code work? - batch-file

This is my first question so sorry if its a litle vague.
I have been working on a program for a wile now, it is supposed to be a schedule of tasks and the time those tasks have to be completed.
Everything works just fine but i cant figure out why this part doesn't work.
:edit
echo Select Task
echo (from 1 to 7)
set /p sel=
if %sel%=1 ( goto task1 )
And then its supposed to bring me here
:task1
pause
echo Insert Task
echo.
set /p task1=
echo.
echo Set Time
echo.
set /p %time1%=
echo.
pause

It's your IF statement. a single equals sign "=" is used as an assignment operator in batch:
set var=fubar
to compare two values use a double equals "==":
if "%var%"=="fubar" echo It'll be reet!
Hope that helps, this was one of the things that always got me when I was starting to code and you will get used to it.

As #Antonio said, there is a problem with the if statement. I also find another problem in :task1
set /p %time1%=
should be
set /p time1=
...and go to www.ss64.com/nt and take a little lesson of batch scripting.

The line: if %sel%=1 ( goto task1 ) is very incomplete;
Write it so:
if "%sel%"=="1" goto task1

Related

How to compare a variable in a FOR command with one outside of the FOR command?

I'm working on a program that's based on a device used in the early 20th century. While testing on parts of the program, a part of the code doesn't work like I thought it would. Here is some code that is similar to it:
#echo off
setlocal enabledelayedexpansion
cls
set /p ar=Type a number:
:loop
for /l %%a in (1,1,100) do (
set var1=%%a
if !var1! equ %ar%(
echo It worked
goto pause1
) else (
echo It didn't work
)
)
:pause1
pause
What I'm trying to do here is comparing the "%%a" variable with a variable set outside of the FOR command, but every time I tested the program, it would suddenly exit from the command prompt. I have tried changing the order of the commands, and changing the symbols, but the output is the same. I've checked many references and I haven't found any that tackles this certain scenario. Can you tell me what I'm doing wrong here and what to do to fix it? Thank you.

BATCH program crashes after goto command

This code is part of a chat program that I am currently working on. The 'else' part of my program is the one that doesn't work. The program quits instead of going to :home
:join
cls
if not exist "C:/Users/Public/room.cmd" (
echo No room has been found.
echo.
set /p choiceretry=Do you want to retry? y/n
if "%choiceretry%"=="y" goto join
if "%choiceretry%"=="n" goto home
) else (
cls
"C:/Users/Public/room.cmd"
echo A room has been found.
pause >nul
echo Joining
set roomjoined=1
echo %roomjoined%
goto home
)
:home
echo this finally works
pause
I have tried changing the code several times starting from 'echo Joining'
Anyone know why cmd quits?...
:) :) :)
Thanks in advance
The problem is the way you run room.cmd; you must use call to return from it:
call "C:/Users/Public/room.cmd"
Otherwise, execution will not return from room.cmd to the original batch file that ran it.
Hint: Consider to use choice instead of set /P for Y/N decisions.
Firstly, please don't left justify your code blocks. It's much easier to read code that's properly indented.
Secondly, when retrieving values within a code block, you need delayed expansion. See setlocal /? in a cmd prompt for more information. This is the reason for the unexpected behavior. Your variables retrieved within the same parenthetical code block in which they were set won't contain the values you expect unless you retrieve them with delayed expansion syntax. As an alternative, you could use the choice command and if errorlevel, which would result in a bit nicer user experience I think.
Thirdly, when testing user input, you should use the /i switch in your if statements for case-insensitivity. This isn't relevant if using choice / if errorlevel though.
Fourthly, Windows paths use backslashes, not forward slashes.
I'd fix it this way:
#echo off
setlocal
:join
cls
if errorlevel 1 set /P "=Retrying... "<NUL
if not exist "C:\Users\Public\room.cmd" (
echo No room has been found.
echo.
choice /c yn /n /m "Do you want to retry? [y/n] "
if errorlevel 2 goto home
goto join
) else (
"C:\Users\Public\room.cmd"
echo A room has been found.
pause >nul
echo Joining
set roomjoined=1
)
:home
echo this finally works
pause

Batch code: how to pass a parameter choosing from an input list?

I'm really new in this forum so I hope to respect all your rules, if not please forgive me!
I've just started studying something about batch files and I'm trying to execute a simple program from batch passing a parameter (the last aim is to submit a SAS program passing a date parameter).
Is it possible to activate a sort of list where I can choose some between pre-defined parameters?
--> This is the real aim of my work
I'm trying to "play" with this code:
#echo off
title Setting up execution period
echo Insert your date in the format GGMMMAAAA (es: '31DEC2003'D).
SET /p data_par=Insert the date to filter datas:
SET first_byte=%data_par:~1,1%
if "%first_byte%"=="" (
GOTO tag1
) else (
GOTO tag2
)
:tag1
msg * Missing value
:tag2
msg * Well done!
pause
I've tried in a lot of ways but it looks like the IF statement is not executed, I don't know where am I wrong.
Another question: why the prompt closes after i press "Enter" (afte the set/p command is executed)?
--> this has been resolved putting the "pause" command at the end of the script.
Thank you all for the attention,
Best regards!
Squotty
Put a pause at the end of your code to see the errormessages.
correct syntax for if when using else is:
if "a"=="b" (dosomething) else (dosemethingelse)
You can write it in several lines, but there are rules, where to set the paratheses:
if "a"=="b" (
echo this is code for something
rem more lines possible
) else (
echo this is code for something else
rem more lines possible
)
The first ( has to be on the same line than if.
) else ( have to be on one line.
If you press just enter with set /p, the variable remains unchanged (propably empty), so your code will go on with the code and hits the line else. Here it will tell you "else is not recognized as a command..."
at your tagx you should tell batch, where to stop execution. Use goto :eof to stop execution or goto somewhere to continue somewhere else. If you don't, it will just continue with the next lines.
Example:
:tag1
msg * Missing value
goto :eof
:tag2
msg * Well done!
goto :continue
pause
:continue
REM go on with the program...
(note: the pause will never be reached. I let it there to show you, how things work)
EDIT instead of just checking for some input you can check for the correct format:
echo %data_par%|findstr /r "[0-3][0-9][A-Z][A-Z][A-Z][1-2]0[0-9][0-9]">nul && (
echo correct format
goto continue
) || (
echo wrong format
goto startover
)
It's not bullet proof (eg. 38ABC2019 would be considered "correct"), but at least it checks for the correct format (e.g. 15.12.2019 or 12/15/2014 would be "not correct")
#ECHO OFF
SETLOCAL
SET "item1=date1"
SET "item2=date2"
SET "item3=date3"
SET "item4=date4"
FOR /l %%a IN (1,1,4) DO CALL ECHO(%%a. %%item%%a%%
ECHO(U. User-specified
choice /c 1234u
CALL SET selection=%%item%errorlevel%%%
IF NOT DEFINED selection (
SET /p selection="Your date-selection ? "
)
IF NOT DEFINED selection ECHO No selection made&GOTO :EOF
ECHO selection is %selection%
GOTO :EOF
This code may be of assistance.
It's normal to develop batch code using a batch window. Simply set up a shortcut to command prompt (Start>Programs>Accessories) which would allow you to run the script over and over and retain the results on-screen without using 'pause'. Editing can be accomplished by using notepad batchfilename.bat from the prompt (if you are using notepad for an editor - if using something better, then substitute that program's name). You can exit from the batch window by executing an exit command.
You can also get help on batch commands by using commandname /? - it's often cryptic and there are plenty of quirks. Extensive help available here on SO.

Endless loop in batch file using 'goto'

Well, I have this problem with a little program that I'm making because my loop is endless. Right now I'm trying to use goto command but it doesn't work couse it is endless. Anyone please help.
#echo off
color 1a
:i
echo Hi, mate Let's start off by getting each others names.
timeout 5 > nul
echo What is your name ?:
set /p name=
echo Hi noob %name% my name is Youke
timeout 2 > nul
echo %name% We are going on an advenutre quest today :D
timeout 2 > nul
goto i
Somewhere between the i label on line 3 and the goto command, you need to add code that will break out of the loop—likely an if statement with another goto inside it that points to a label after your current goto.
You can see plenty of examples here: https://stackoverflow.com/a/4711984/436282

Missing Operator in batch game

This is my script. Whenever the player goes to this part of the script it says missing operator. But it only says it the first time. Help.
:M221
(
set /p Gold=<"%CD%\Data Files\gold.dat"
)
set /a gold=%gold%+60
echo You have earned 60 gold today. Time to go home and go to bed.
pause
cls
goto home3
:M321
(
set /p Gold=<"%CD%\Data Files\gold.dat"
)
set /a gold=%gold%+80
echo You have earned 80 gold today. Time to go home and go to bed.
pause
cls
goto home3
:M421
(
set /p Gold=<"%CD%\Data Files\gold.dat"
)
set /a gold=%gold%+150
echo You have made a spectacular sword and earned 150 gold today. Time to go home and go to bed.
pause
cls
goto home3
Not really enough information.
I'd check the contents of gold.dat.
If the file does not exist, it will give you a different response.
If it is empty ( a zero-byte file) or contains simply a space newl-line or a new-line alone, the code will work happily.
If the file contains , and a newline, you'll get the error you report.
you should post the content of "%CD%\Data Files\gold.dat", and, btw. you should remove the part %CD%\ from the path, it is not neccesarry.
That's because gold may not be a number on your first pass!

Resources