SEARCH

The How-To Geek Forums Have Migrated to Discourse

How-To Geek Forums / Windows 7

variables in batch

(10 posts)
  • Started 1 year ago by needshelp
  • Latest reply from ispalten
  • Topic Viewed 386 times

needshelp
Posts: 158

Alright so I'm making a program that changes where you "goto" depending on what the user inputs. I've got a little example set up to test if it works.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@echo off
:start
set /a y=1
echo Hello there.
set /p y= next part should be whatever you type in here (you're supposed to type a number).
goto %y%

:next
echo if something went wrong you should be here.
pause

:%y%
echo you typed %y%
pause
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Except whenever I run it I get this error that says
"The system cannot find the batch label specified - 5"
'5' being the random number I typed in.

So my guess is that it isint making :%y% turn into :5 ?

Why is this? help?

Posted 1 year ago
Top
 
ispalten
Posts: 6259

Looks like the problem is

---------
:%y%
echo you typed %y%
pause
----------

If you change :%y% to a '5' and enter 5 it will work.

I don't think variables are 'legal' labels for a GOTO.

Irv S.

Posted 1 year ago
Top
 
needshelp
Posts: 158

any options to make it work?

Posted 1 year ago
Top
 
ispalten
Posts: 6259

Well, if you KNOW the limits of the number to be provided you can make individual goto's for it using IF statement.

I guess, not knowing what you REALLY want to do, there are other options...

1) Still get a variable, but use the CALL statement, like CALL %y%.bat and have a bat file for each possible choice, but error check %y% before issuing the call.

2) The CHOICE command might work, instead of %y% you get an index number that can be used to either do the same as 1 or used in IF statements.

Irv S.

Posted 1 year ago
Top
 
needshelp
Posts: 158

Thanks that's helpful, and I'll see what I can do.
for this bit of the code I'm trying to allow the user to input as many items as they want, so I don't know the total amount but I suppose I could just code for hundreds of statements just to be safe and hope noone inputs more than that.

Posted 1 year ago
Top
 
Xhi
Posts: 6298

If you just want to type the number back you do not need a label. Your program makes no sense. Just echo You have typed in Y after the input.

Posted 1 year ago
Top
 
needshelp
Posts: 158

The "echo you have just type %y%" is just for testing purpose to see that it worked.

Posted 1 year ago
Top
 
Xhi
Posts: 6298

You need to put into words what you are tying to do. If you are trying to do something complex you should use a programming language, or an excell spreadsheet, not a batch file. Batch files are for executing computer commands, not mathematical calculations.

Posted 1 year ago
Top
 
needshelp
Posts: 158

I apologize for any confusion I may be causing, my good sir, I intend only to push the limits of batch programming. While I have a moderate understanding of Java,Python, and C++; my understanding of batch is almost complete and I simply wish to learn more by gaining insight from people who may know more.

If I stated what I'm trying to do it would add too much confusion and draw away from the point of this one confliction in variables and statements.

so back to my original question. ispalten, You explained that doing ":%y%" in batch doesn't work. could I declare a variable in a different way then set the label to that?

Posted 1 year ago
Top
 
ispalten
Posts: 6259

NO, I don't think so... the LABEL must be a constant.

Maybe there could be a way with some nested IF statement testing what the user enters, or best choice would be the CHOICE command and you'd get an index back and use the IF to route the index received to the proper call or label, but either of these would ONLY be good if the input wasn't unlimited, that is could be any number of any size.

How about a little more info? Like how many possible numbers could be entered and are all going to do different things?

And no, batch processing is severely limited compared to any programing language. It was meant more to automate DOS operations with limited data manipulation. Next step up was the OLD BASIC and a BASIC COMPILER... wasn't 'optimized' but with work and extensions you could do most anything when it was in vogue.

You can STILL get some BASIC versions for Windows --> http://download.cnet.com/BBC-B.....92431.html and others too. http://www.qbcafe.net/en/qbasi.....mpiler.htm.

Still, Visual Basic is another possibility. Windows WILL run these natively if you write a VBS script, here is a SIMPLE one that will time system Reboots (save as ReBoot-Time.VBS on your desktop and then just execute it) :

=====================
Option Explicit
On Error Resume Next
Dim Wsh, Time1, Time2, Result, PathFile, MsgResult, MsgA, AppName, KeyA, KeyB, TimeDiff
MsgA = "Please close all running applications and click on OK."
KeyA = "HKEY_CURRENT_USER\Software\RestartTime\"
KeyB = "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run\RestartTime"
AppName = "ReBoot-Time"
Set Wsh = CreateObject("WScript.Shell")
PathFile = """" & WScript.ScriptFullName & """"
Result = wsh.RegRead(KeyA & "Times")
if Result = "" then
MsgResult = Msgbox (MsgA, vbOKCancel, AppName)
If MsgResult = vbcancel then WScript.Quit
Wsh.RegWrite KeyA & "Times", left(Time,8), "REG_SZ"
Wsh.RegWrite KeyB, PathFile, "REG_SZ"
Wsh.Run "cmd /c Shutdown -r -t 00", false, 0
else
Wsh.RegDelete KeyA & "Times"
Wsh.RegDelete KeyA
Wsh.RegDelete KeyB
TimeDiff = DateDiff("s",Result,left(Time,8))
MsgBox "Your computer reboots in " & TimeDiff & " seconds", VbInformation, AppName
end if
wscript.Quit
================

HELP and DOCUMENTATION is available on-line to VB.

Irv S.

Posted 1 year ago
Top
 



Topic Closed

This topic has been closed to new replies.