Why does my batch file generated text not format properly? - file

The Problem:
I'm trying to create a batch file which randomly generates passwords, but I cannot seem to get the generated passwords to format properly.
Before, I just chose to generate numbers, but I realized that it wasn't very secure for users, and I needed to include letters and numbers. I attempted, but it output the wrong thing (kind of).
I want the generated passwords to be at least 8 characters long, and 32 characters maximum. I tried it, but it completely broke the code and it wouldn't output anything at all. Also, it outputs the same passwords for each one on every option. For example, if I chose option 1 (generate 2 passwords), it would output ud8aqas and ud8aqas. If I chose option 2 (generate 5 passwords), it would output p9dkda, p9dkda, p9dkda, p9dkda, and p9dkda, and so on.
The code that I want to use:
#echo off
:Start2
cls
goto Start
:Start
title DotPass Password Generator [BETA]
echo Thanks for using DotPass! If you have any questions, feel free to email us at mhartmix#gmail.com.
echo Please maximise this program so you can see all of the details we have shown!
echo ------------------------------------------------------------------------------------------------------
echo DotPass will generate you a new PIN (or password, if that's what you're going to use it for).
echo You can use these for any of your online accounts! Remember to always note the PIN/password down somewhere!
echo -------------------------------------------------------------------------------------------------------
echo Note: The passwords/PIN's can only be seen by you, not us. To read more, go to github.com/TechGeekMic/DotPass/blob/Main/security.md
echo Passwords generated are never stored on the cloud, or locally, so that's why you should make a note of your randomly generated PIN/password.
echo -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
echo How many passwords do you want DotPass to generate?
echo 1) 2 Generated Random Password
echo 2) 5 Generated Random Passwords
echo 3) 10 Generated Random Passwords
echo Type the number of your choice below. ( i.e 2 )
set input=
set /p input= Choice:
if %input%==1 goto A if NOT goto Start2
if %input%==2 goto B if NOT goto Start2
if %input%==3 goto C if NOT goto Start2
:A
cls
Setlocal EnableDelayedExpansion
Set _RNDLength=12
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set _Str=%_Alphanumeric%987654321
:_LenLoop
IF NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop
SET _tmp=%_Str:~9,1%
SET /A _Len=_Len+_tmp
Set _count=0
SET _RndAlphaNum=
:_loop
Set /a _count+=1
SET _RND=%Random%
Set /A _RND=_RND%%%_Len%
SET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
If !_count! lss %_RNDLength% goto _loop
echo Your two randomly generated passwords are !_RndAlphaNum! and !_RndAlphaNum!
)
echo What do you want do to? Here are a few options.
echo 1) Go back to the beginning
echo 2) Exit DotPass
echo 3) Shutdown your computer
echo Type the number of your choice below. ( i.e 1 )
set input=
set /p input= Choice:
if %input%==1 goto Start2 if NOT goto Start 2
if %input%==2 goto Exit if NOT goto Start 2
if %input%==3 goto Shutdown if NOT goto Start 2
:Shutdown
shutdown
:Exit
exit
:B
cls
Setlocal EnableDelayedExpansion
Set _RNDLength=12
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set _Str=%_Alphanumeric%987654321
:_LenLoop
IF NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop
SET _tmp=%_Str:~9,1%
SET /A _Len=_Len+_tmp
Set _count=0
SET _RndAlphaNum=
:_loop
Set /a _count+=1
SET _RND=%Random%
Set /A _RND=_RND%%%_Len%
SET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
If !_count! lss %_RNDLength% goto _loop
echo Your five randomly generated passwords for you are !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum! and !_RndAlphaNum!
)
echo What do you want do to? Here are a few options.
echo 1) Go back to the beginning and start again
echo 2) Exit DotPass
echo Type the number of your choice below. ( i.e 1 )
set input=
set /p input= Choice:
if %input%==1 goto Start2 if NOT goto Start 2
if %input%==2 goto Exit if NOT goto Start 2
if %input%==3 goto Shutdown if NOT goto Start 2
:C
cls
Setlocal EnableDelayedExpansion
Set _RNDLength=12
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set _Str=%_Alphanumeric%987654321
:_LenLoop
IF NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop
SET _tmp=%_Str:~9,1%
SET /A _Len=_Len+_tmp
Set _count=0
SET _RndAlphaNum=
:_loop
Set /a _count+=1
SET _RND=%Random%
Set /A _RND=_RND%%%_Len%
SET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
If !_count! lss %_RNDLength% goto _loop
echo Your ten randomly generated passwords for you are !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum!, !_RndAlphaNum! and !_RndAlphaNum!
echo What do you want do to? Here are a few options.
echo 1) Start again
echo 2) Exit DotPass
set input=
set /p input= Choice:
if %input%==1 goto Start2 if NOT goto Start 2
if %input%==2 goto Exit if NOT goto Start 2
if %input%==3 goto Shutdown if NOT goto Start 2
You can try it out for yourself as long as you don't publish it or modify it.
I am very new to batch scripts! Can someone help?

Related

Batch Random Charachters Err

I was playing around with batch scripts on windows and I was trying to make a basic password generator.
( Maximum setlocal recursion level reached. )
My code :
#Echo Off
color 0a
set /P lengthnumberuser="What length do you want your password to be? "
pause
cls
:code
Setlocal EnableDelayedExpansion
set _RNDLength=16
set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
set _Str=%_Alphanumeric%987654321
:_LenLoop
if NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop
set _tmp=%_Str:~9,1%
set /A _Len=_Len+_tmp
set _count=0
sET _RndAlphaNum=
:_loop
set /a _count+=1
sET _RND=%Random%
set /A _RND=_RND%%%_Len%
sET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
if !_count! lss %_RNDLength% goto _loop
echo !_RndAlphaNum!
goto code
pause
Just delete the goto code at the end of the your code.
See Here : Link

Issues with rpg stat system

Im making an rpg game in batch, and here is the code for the stat system so far
:SPSPEND
CLS
Echo You have %SP% stat points to spend, every stat point used increases that skill by 5.
Echo 1. Strength=%Strength%
Echo 2. Agility=%Agility%
Echo 3. Magic=%Magic%
Echo 4. Vitality=%MaxHP%
Echo 5. Defence=%Defence%
Echo 6. Archery=%Archery%
set /p choice=Choose what to put points into:
if %choice%=="1" Set /a Strength==%strength%+5
if %choice%=="2"
if %choice%=="3"
if %choice%=="4"
if %choice%=="5"
if %choice%=="6"
cls
set /a SP==%SP%-1
goto :rest
Not sure how to format soz. i need help with two things : how can i make it so that it doesnt close out and actually works, and how can i make a failsafe incase a person enters something other than what i already have set up.
Thanks in advance
I believe this is what you want.
I have tested it and am fairly certain it will work
:SPSPEND
CLS
if %SP% leq 0 goto end
Echo You have %SP% stat points to spend, every stat point used increases that skill by 5.
Echo 1. Strength=%Strength%
Echo 2. Agility=%Agility%
Echo 3. Magic=%Magic%
Echo 4. Vitality=%MaxHP%
Echo 5. Defence=%Defence%
Echo 6. Archery=%Archery%
set /p choice=Choose what to put points into:
if "%choice%"=="1" Set /a Strength=%Strength%+5& set /a SP=%SP%-1& goto SPSPEND
if "%choice%"=="2" Set /a Agility=%Agility%+5& set /a SP=%SP%-1& goto SPSPEND
if "%choice%"=="3" Set /a Magic=%Magic%+5& set /a SP=%SP%-1& goto SPSPEND
if "%choice%"=="4" Set /a MaxHP=%MaxHP%+5& set /a SP=%SP%-1& goto SPSPEND
if "%choice%"=="5" Set /a Defence=%Defence%+5& set /a SP=%SP%-1& goto SPSPEND
if "%choice%"=="6" Set /a Archery=%Archery%+5& set /a SP=%SP%-1& goto SPSPEND
echo invalid choice
pause
goto SPSPEND
cls
:end
pause
goto :rest

How does this batch password generation work?

I am creating a password generator in batch, and had to use some help from the internet to finish it, but I am having difficulty interpreting the section of the code I had to borrow. Would someone please explain it?
:generate
#Echo Off
color 0a
set /P usernumberlength="What length do you want your password to be? "
pause
cls
Setlocal EnableDelayedExpansion
Set RNDLength=%usernumberlength%
/// from here
Set Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set Str=%Alphanumeric%987654321
:loop
IF NOT "%Str:~18%"=="" SET Str=%Str:~9%& SET /A Length+=9& GOTO :loop
SET tmp=%Str:~9,1%
SET /A Length=Length+tmp
Set count=0
SET RndAlphaNum=
:loop2
Set /a count+=1
SET RND=%Random%
Set /A RND=RND%%%Length%
SET RndAlphaNum=!RndAlphaNum!!Alphanumeric:~%RND%,1!
If !count! lss %RNDLength% goto loop2
/// to here
Echo Password is: is !RndAlphaNum!
Echo Now choose what you want to do.
Echo 1) Go back to the beginning
Echo 2) Exit
set input=
set /p input= Choice:
if %input%==1 goto generate
if %input&==2 exit
pause
This working example (from the code you provided) contains the all-you-need section.
#echo Off
color 0a
setlocal enabledelayedexpansion
:generate
set /p usernumberlength="What length do you want your password to be? "
rem ----- begin section all you need -----
Set Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
rem "Length" is the string length of %alphanumeric%
Set Length=62
set count=0
rem "RndAlphaNum" is the output
SET RndAlphaNum=
:loop
Set /a count+=1
Set /A RND=%Random% %% %Length%
SET RndAlphaNum=!RndAlphaNum!!Alphanumeric:~%RND%,1!
if !count! lss %usernumberlength% goto loop
rem ----- end section all you need -----
echo Password is: !RndAlphaNum!
goto generate

Batch File Closing When I Try To Drag Variables

I am currently coding (in batch) a mini tycoon game. However,
I'm having some difficulties when executing this command:
:cantbuy
echo Insufficient Funds!
pause
goto shop
:buy1
set /p money<"m2df.dll"
set /p cps<"m3df.dll"
if %money% GTR 100 goto canbuy1
goto cantbuy
:canbuy1
set /a "newmoney=%money%-100"
set /a "newcps=%cps%+2"
set cps=%newcps%
set money=%newmoney%
echo %money%>m2df.dll
echo %cps%>m3df.dll
echo Bought!
pause
goto shop
The program seems to exit straight after I input "1"
Any ideas how to fix this?
Update:
Found It Out. I decided to put a pause after everything to see what was wrong, and the syntax was bad and it went straight to another command crashing the script. here's the fixed script:
:cantbuy
echo Insufficient Funds!
pause
goto shop
:buy1
set /p money=<"m2df.dll"
set /p cps=<"m3df.dll"
if %money% GEQ 100 goto canbuy1
goto cantbuy
:canbuy1
set /a "newmoney=%money%-100"
set money=%newmoney%
set /a "newcps=%cps%+2"
set cps=%newcps%
echo %cps% >m3df.dll
echo %money% >m2df.dll
echo Bought!
pause
goto shop

How to choose only the options available Batch file

I found this tutorial to make a game on Notepad++ and make a batch file to run on CMD. Well here's what I have so far:
:ChooseWeapon
cls
echo "I almost forgot. Here are the weapons I have avaliable, choose one and begin your quest."
echo.
set /p weapon=What is your weapon? (Sword, Double-Bladed Axe, Dagger):
The point to this is to choose a weapon you wish to use by typing what you want. Now, the thing is I'm able to type "ghregff" and it would say that is my weapon. How do I make it so you have to choose either: Sword, Double-Bladed Axe, or Dagger?
You could make it a choice menu like below, and if they choose a number not 1 2 or 3, it will kick them back to enter a selection again.
#echo off
:chooseweapon
cls
echo "I almost forgot. Here are the weapons I have avaliable, choose one and begin your quest."
echo.
echo What is your weapon? (Sword, Double-Bladed Axe, Dagger):
echo 1 - Sword
echo 2 - Double-Bladed Axe
echo 3 - Dagger
echo.
set /P Weapon="Enter a choice: "
echo --------------------------------------------------------------------
for %%I in (1 2 3) do if #%Weapon%==#%%I goto wp%%I
goto chooseweapon
:wp1
Set weapon=Sword
goto end
:wp2
Set weapon=Double-Bladed Axe
goto end
:wp3
Set weapon=Dagger
goto end
:end
echo %weapon%
pause
#echo off
set "NumberWeapons=3"
:ChooseWeapon
color 07
cls
echo."I almost forgot. Here are the weapons I have avaliable, choose one and begin your quest."
echo.
echo.1 Sword
echo.2 Double-Bladed Axe
echo.3 Dagger
set /p Weapon=What is your weapon? (1-%NumberWeapons%):
echo.
if %Weapon% lss 1 goto :ChoiceError
IF %Weapon% gtr %NumberWeapons% goto :ChoiceError
GOTO :Start
:ChoiceError
COLOR CF
ECHO.Error! Choose a valid choice.
pause
goto :ChooseWeapon
:Start
echo.Replace this line with the rest of your stuff & pause
#ECHO OFF
SETLOCAL
SET setprompt=What is your weapon?
CALL :chooselist Sword, "Double-Bladed Axe", Dagger
ECHO(Choice made was %response%
CALL :chooselist2 Sword, "Double-Bladed Axe", Dagger
ECHO(Choice made was %response%
CALL :choosemenu Sword, "Double-Bladed Axe", Dagger
ECHO(Choice made was %response%
GOTO :EOF
:chooselist
SET valid=%*
SET "nchoices="
CALL :choose "%setprompt% (%valid:"=%):"
GOTO :eof
:chooselist2
SET valid=%*
SET "nchoices="
FOR %%Z IN (%*) DO ECHO %%~Z
CALL :choose "%setprompt%:"
GOTO :eof
:choosemenu
SET valid=%*
SET /a nchoices=0
FOR %%Z IN (%*) DO SET /a nchoices+=1&CALL ECHO %%nchoices%% : %%~Z
CALL :choose "%setprompt%:"
GOTO :eof
:choose
SET /p "response=%~1 "
IF NOT DEFINED response GOTO choose
IF DEFINED nchoices FOR /l %%Z IN (1,1,%nchoices%) DO IF "%%Z"=="%response%" CALL :setresp %valid%&GOTO :eof
SET /a cmatch=0
CALL :countresp %valid%
IF NOT %cmatch%==1 GOTO choose
GOTO :eof
:setresp
IF %response% neq 1 SET /a response-=1&shift&GOTO setresp
SET response=%~1
GOTO :eof
:countresp
SET $1=%~1
IF NOT DEFINED $1 (
IF %cmatch%==1 (SET response=%$3%)
GOTO :eof
)
CALL SET $2=%%$1:*%response%=%%
IF /i "%response%%$2%"=="%$1%" SET /a cmatch+=1&SET "$3=%~1"
shift&GOTO countresp
GOTO :eof
This is a flexible piece of code that allows you to make your choice in ne of three manners:
Using chooselist, the parameter-list is appended to setprompt and the user may choose to enter any response in full, or simply sufficient to unambiguously define the requirement. "s" would define Sword, for instance, but "do" would be required for "Double-Bladed Axe" and "da" for "Dagger" since "d" is ambiguous.
Using chooselist2 is similar, it simply lists the choices each on its separate line and asks for input.
Finally, choosemenu does the same, but numbers the line and the choice can be made using either the number or an unambiguous start-string.
The response will always be in response
The easiest way seems to be doing it through the findstr command.
:Choice
set /p weapon=What is your weapon? (Sword, Double-Bladed Axe, Dagger):
echo %weapon%| findstr /r "^Sword$ ^Double-Bladed Axe$ ^Dagger$">nul
if errorlevel 1 (
echo %weapon% was sadly not an option.
goto :Choice
)
echo you chose %weapon%.
(you need ^ and $. ^ means start of line, $ means end of line)
OR
if you want to use that system several times you could use findstr with parameters and a return value
:MakeChoice
set /p answer= :
echo %answer%| findstr /r "^%~2$ ^%~3$ ^%~4$ ^%~5$ ">nul
if errorlevel 1 (
echo "%answer%" is sadly not a valid choice, choose again.
goto :MakeChoice
)
set %~1=%answer%
goto :eof
You can than call the function this way:
echo choose a weapon(Sword, Axe, Dagger)
call :MakeChoice chosenOption "Sword" "Double-Bladed Axe" "Dagger"
echo you chose %chosenOption%
This works with 1-4 variables, but if you want more, you can always add ^%~6$ ^%~7$... after ^%~5$.

Resources