Making inputs pertain to specific topics

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
UghThatGuyAgain
Posts: 1
Joined: 08 Dec 2016 21:54

Making inputs pertain to specific topics

#1 Post by UghThatGuyAgain » 08 Dec 2016 22:08

Hey there,

I've recently taken up Batch just as a hobby, so I'm still fairly new to all this. Currently, I've made a simple program that allows you to create a password with the input of a master key, and allows it to store it into a document that will be hidden to sight. Now, I was wondering if you could make it so that, in a text document, you can put a topic down, and then make the file put whatever you input into the program sort it through each topic.

So, say I have a topic called emails, and I have my program ask me "Which topic would you like to put this password under?" I would input emails, and then it would ask "What account would you like to put this password under?" I input Gmail, and then it asks for the password, then writes the password as Gmail- (password) inside the document. Is this possible? Or do I have to code in each and every single different variable that it could be as, such as

Code: Select all

set /p topic= What topic will this file under?
if %topic%==email goto email
:email
set /p email= What email will this file under?
if %topic%==gmail goto gmail
if %topic%==aol goto aol


sidenote, code looks like this so far:

Code: Select all

@echo off
:start
cls
echo 1.) Create Password
echo 2.) Check Password
echo 3.) Open Passwords Document
echo.
set /p program= What do you want to do?
if %program%== create goto security
if %program%== check goto check
if %program%== open passwords document goto security2
if %program%== open goto security2
if %program%== open passwords document goto security2
if %program%== passwords goto security2
if %program%== 1 goto security
if %program%== 2 goto check
if %program%== 3 goto security2

:security
cls
set /p masterkey= Please enter the master key:
if %masterkey%== testingkey goto create
pause>nul

:security2
cls
set /p masterkey2= Please enter the master key:
if %masterkey2%== testingkey2 start random.txt
pause>nul

:create
cls
set /p password= What would you like your password to be?
echo %password% >> random.txt
pause
goto start


:check
cls
set /p password1=What is your password?

for /f "Delims=" %%a in (random.txt) do (
set TEXT=%%a
)

if %password1%==%text% goto correct
echo Incorrect password.
pause>nul
goto start

:correct
echo Correct password.
set /p open=Select "o" to open the document of passwords, or select "n" to return to the menu.
if %open%==o goto pass
if %open%==n goto start

pause>nul

:pass
start random.txt

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Making inputs pertain to specific topics

#2 Post by Aacini » 09 Dec 2016 13:09

The answer to your question is comprised of two parts.

First of all, the format of IF command, that you may show in the screen via IF /?, is similar to this:

Code: Select all

IF [NOT] string1==string2 command

If you compare this format vs. your command:

Code: Select all

if %program%== open passwords document goto security2

you surely will realize that the string %program% will be compared vs. the string open and, if both are equal, the passwords document goto security2 command is executed. Of course, this is wrong. Also, you should anticipate what happen if the user enters NO INPUT! The Batch processor just replace %program% by nothing and issue an error because the second string is not a comparison.

The way to solve both problems is enclosing the several words in quotes, so the Batch processor takes all of them like a single string. For example:

Code: Select all

if "%program%" == "open passwords document" goto security2

This same point apply to filenames:

Code: Select all

copy "Filename with spaces.txt" destination

Note that in the IF command both string must be identical! If the input include an additional space at any place or if the case of letters don't match, the comparison fails, although this last point may be fixed using /I switch.

After you understand all these points, I strongly suggest you to use CHOICE command for this type of option selection instead SET /P, because all these details are avoided.


Now, go back to your original question. I will answer it via a comparison with a similar situation, but applied to a numeric variable.

Suppose that your program ask the user to enter a digit and, after that, the program must add such a digit to "result" variable via SET /A command. This could be the original code:

Code: Select all

set /P "digit=Enter the digit to add: "
if "%digit%" == "1" set /A result=result+1
if "%digit%" == "2" set /A result=result+2
if "%digit%" == "3" set /A result=result+3
if "%digit%" == "4" set /A result=result+4
if "%digit%" == "5" set /A result=result+5
if "%digit%" == "6" set /A result=result+6
if "%digit%" == "7" set /A result=result+7
if "%digit%" == "8" set /A result=result+8
if "%digit%" == "9" set /A result=result+9

Note that this code is valid, although cumbersome. The key point here is this: if you already know that %digit% variable contains a valid digit (note that in previous code this point had NOT been assured!), why test each possible value and execute a similar action in each case? Why not just use the value of the variable directly in the expression?

Code: Select all

set /A result=result+digit

Of course, this point is obvious in this numeric expression example, but the same conclusion may also be applied to your %topic% and %program% variables...

Antonio

Post Reply