program quits at a certain point. batch/cmd - batch-file

sup, I am making a program that takes and stores user input in a .txt file (basically stores a password) after that you can go and check the password (to maybe unlock or open something, etc)
for some reason when I try to compare the two passwords the program quits in both ways, this is the problem, thx if you help
this is the code:
#echo off
:start
echo -create (make a password)
echo -check (check a password)
set /p PROGRAM= enter opperation:
goto %PROGRAM%
:create
cls
set /p data= enter data:
echo %data% > C:\Users\Hp\testfile.txt
echo done!
pause
goto start
:check
cls
set /p data2= what is your password?
for /f "Delims=" %%realdata in (C:\Users\Hp\testfile.txt) do (
set TEXT=%%realdata
)
if %data2%==%TEXT%
(
pause
goto correct
)
echo wrong, try again
pause
goto start
:correct
echo good job
pause
goto start

Try change this:
if %data2%==%TEXT%
(
pause
goto correct
)
to this:
if %data2% == %TEXT% (
pause
goto correct
)
If is it not work, then try changing this:
for /f "Delims=" %%realdata in (C:\Users\Hp\testfile.txt) do (
set TEXT=%%realdata
)
to this:
set tmp=<C:\Users\Hp\testfile.txt
set /p password=Enter password:
if %tmp% == %password% (
::something eg. echo PASSWORD is Good!
) ELSE (
::Something
)
Good Luck!

Related

Best way to simulate a CMD prompt from a .bat script

So basically, I have a script we use out on the field to do various functions. Part of the script will include a 'Debug Terminal' (For setting and checking variables, running functions directly etc.). I ran into some problems initially with echo and set commands (echo Hi would result in Hi is not recognized..; set would not process).
Here's what I wound up with:
:DebugConsole
%title%Debug Console %end%
:DebugConsoleTop
echo.
echo Enter a command or type 'exit' to finish.
set /p ConsoleInput=
if "%ConsoleInput%"=="exit" (
goto DebugConsoleEnd
)
:if set used
if "%ConsoleInput:~0,3%"=="set" (
echo %ConsoleInput%> "%datapath%\debugcmd.txt"
set /p ConsoleOutput=<"%datapath%\debugcmd.txt"
del /f "%datapath%\debugcmd.txt"
) else (
:if echo
if "%ConsoleInput:~0,4%"=="echo" (
set "echofix=echo echo"
::set ConsoleInput=%ConsoleInput:%echofix%=%
call set ConsoleInput=%%ConsoleInput:echo=%echofix%%%
%ConsoleInput%
goto DebugConsoleTop
)
:all else
FOR /F "delims=" %%i IN ('%ConsoleInput%') DO (
SET ConsoleOutput=%%i
echo %ConsoleOutput%
pause
)
)
%ConsoleOutput%
goto DebugConsoleTop
:DebugConsoleEnd
cls
exit /b
The problem I'm facing now is that you can not echo variables correctly. If I input "set var=test" and then "echo %var%" the output is "%var%" and not "test". However, if I input "%var%" i receive "test is not recognized.."

Batch IF doesn't work

I made a simple BATCH file for notes, it just deletes a folder, makes a new one, and puts in a file called note.txt and then edits it... for some reason, it doesn't do ANYTHING at all! not even removing the folder, here's the code:
goto start
:abort
ECHO Aborted!
PAUSE
GOTO start
:replace
del /q "D:\Users\Eldar\Desktop\Note Folder"
mkdir "Note Folder"
#echo>"D:\Users\Eldar\Desktop\Note Folder\note.txt"
#echo %note%> "D:\Users\Eldar\Desktop\Note Folder\note.txt"
pause
:start
#ECHO OFF
color e
cls
echo Put the note in here:
set /p LastNote=<"D:\Users\Eldar\Desktop\Note Folder\note.txt"
SET /P note=
cls
echo Note has been saved!
echo also, the last note you saved was: "%LastNote%".
echo so are you sure you want to replace this note with a new one (Y/N)?
SET /P agree=
IF "%agree%"=="y" (
ECHO goto replace
) ELSE (
GOTO abort
)
There are few errors in your script:
del can delete only empty folders. Use rmdir /s /q folder_name to remove folder with files
Remove ECHO in ECHO goto replace
You are using full paths everywhere except mkdir. Is it correct?
My working version:
#echo off
goto start
:abort
echo Aborted!
pause
goto start
:replace
rmdir /s /q ".\TestF"
mkdir "TestF"
echo>".\TestF\note.txt"
echo %note%> ".\TestF\note.txt"
pause
:start
color e
cls
echo Put the note in here:
set /p LastNote=<".\TestF\note.txt"
set /p note=
cls
echo Note has been saved!
echo also, the last note you saved was: "%LastNote%".
echo so are you sure you want to replace this note with a new one (Y/N)?
set /p agree=
if "%agree%"=="y" (
goto replace
) else (
goto abort
)
There appears to be no reason to remove the Note Folder directory or even to delete the note.txt file.
How does it perform if you rewrite it like this?
#Echo Off
Color 0E
Set "LastNote="
If Exist "%UserProfile%\Desktop\Note Folder\note.txt" (
Set/P LastNote=<"%UserProfile%\Desktop\Note Folder\note.txt"
)
If Not Defined LastNote GoTo replace
:ask
ClS
Echo=The last note you saved was:
Echo=%LastNote%
Echo=
Choice /M "Would you like to replace this note with a new one"
If "%ErrorLevel%"=="1" GoTo replace
Echo=
Echo=Aborted!
Timeout -1
GoTo ask
:replace
ClS
Set/P "note=Put the note in here: "
If "%note%"=="" GoTo ask
Echo=Note has been saved!
If Not Exist "%UserProfile%\Desktop\Note Folder\" (
MD "%UserProfile%\Desktop\Note Folder"
)
(Echo=%note%)>"%UserProfile%\Desktop\Note Folder\note.txt"
Timeout -1
GoTo ask
For the benefit of the OP, here is your version of your script with the major flaws removed and with unnecessary lines kept in.
#goto start
:abort
ECHO Aborted!
PAUSE
GOTO start
:replace
rmdir "D:\Users\Eldar\Desktop\Note Folder"
mkdir "D:\Users\Eldar\Desktop\Note Folder"
echo.>"D:\Users\Eldar\Desktop\Note Folder\note.txt"
>"D:\Users\Eldar\Desktop\Note Folder\note.txt" echo %note%
pause
:start
#ECHO OFF
color e
cls
echo Put the note in here:
set /p LastNote=<"D:\Users\Eldar\Desktop\Note Folder\note.txt"
SET /P note=
cls
echo Note has been saved!
echo also, the last note you saved was: "%LastNote%".
echo so are you sure you want to replace this note with a new one (Y/N)?
SET /P agree=
IF "%agree%"=="y" (
goto replace
) ELSE (
GOTO abort
)

CMD - Input from another cmd

I have one cmd where you do a choice (1 for login, 2 for register, ...) and then you enter the user, and password (set /p user="User: " , set /p password="Password: ") and what I want to do is enter the choice, username and password with another batch file that I make...
What I tried:
#echo off
title Wizzard-Cracker
color a
cd C:\Users\Wolfy\Documents\Wolfy\Subory .BAT\Wizzard
:A
start wizzard.cmd /K set choice=1 & set user=wolfy & set password=sombest
Login Batch:
#echo off
title Login and Register
color F
setlocal enableDelayedExpansion
:A
echo [Login and Register]
echo [1.][Login]
echo [2.][Register]
echo [3.][Exit]
set /p input="Choose: "
if "%input%"=="1" (
cls
goto login
)
if "%input%"=="2" (
cls
goto register
)
if "%input%"=="3" (
cls
exit
)
:login
echo [Login]
set /p user=User:
echo.
set /p password=Password:
echo.
if NOT EXIST data/%user%.userfile (
echo User %user% isn't registered.
ping localhost -n 2 >nul
cls
goto login
)
set /p correct=<data/%user%.userfile
set chars=0123456789abcdefghijklmnopqrstuvwxyz
for /L %%N in (10 1 36) do (
for /F %%C in ("!chars:~%%N,1!") do (
set "correct=!correct:-%%N=%%C!"
)
)
if "%password% "=="!correct!" (
echo Login successful.
ping localhost -n 2 >nul
cls
goto console
) else (
echo Incorrect password entered.
ping localhost -n 2 >nul
cls
goto login
)
:register
echo [Register]
set /p rUser=Register User:
echo.
set /p rPassword=Register Password:
echo.
if EXIST data/%rUser%.userfile (
echo User is already registered.
goto register.choice
)
set chars=0123456789abcdefghijklmnopqrstuvwxyz
for /L %%N in (10 1 36) do (
for /F %%C in ("!chars:~%%N,1!") do (
set "rPassword=!rPassword:%%C=-%%N!"
)
)
echo !rPassword! > data/%rUser%.userfile
echo Registration was successful!
ping localhost -n 2 >nul
cls
goto A
:register.choice
set /p choice="Go to Login [Y/N]: "
if %choice%==y (
cls
goto login
)
if %choice%==Y (
cls
goto login
)
if %choice%==n (
cls
goto register
)
if %choice%==N (
cls
goto register
)
How do I do that?
Here's a quick rewrite, there may be errors or things you would prefer not to change but it should show you a way of doing it.
#ECHO OFF
TITLE Login and Register
COLOR F
SETLOCAL ENABLEDELAYEDEXPANSION
IF "%~1" NEQ "1" (GOTO :A) ELSE (IF "%2" EQU "" (GOTO :A) ELSE (SET "user=%~2"
IF "%~3" EQU "" (SET password=)
GOTO :vlog))
:A
CLS
ECHO [Login and Register]
ECHO [1.][Login]
ECHO [2.][Register]
ECHO [3.][Exit]
CHOICE /C 123 /N
IF ERRORLEVEL 3 EXIT/B
IF ERRORLEVEL 2 GOTO :register
:login
CLS
ECHO [Login]
SET /P user=User:
IF NOT DEFINED user GOTO :login
:vlog
IF "%user:*=%" NEQ "%user%" GOTO :login
IF NOT EXIST data/%user%.userfile (
ECHO User %user% isn't registered.
TIMEOUT 2 >NUL
GOTO :login
)
:pass
SET /P correct=<data/%user%.userfile
ECHO.
IF NOT DEFINED password (
SET /P password=Password:
)
IF NOT DEFINED password GOTO :pass
SET chars=0123456789abcdefghijklmnopqrstuvwxyz
FOR /L %%N IN (10 1 36) DO (
FOR %%C IN (!chars:~%%N,1!) DO (
SET "correct=!correct:-%%N=%%C!"
)
)
IF "%password%"=="%correct%" (
ECHO.
ECHO Login successful.
TIMEOUT 2 >NUL
GOTO :console
)
ECHO Incorrect password entered.
TIMEOUT 2 >NUL
GOTO :pass
:register
CLS
ECHO [Register]
SET /P rUser=Register User:
IF NOT DEFINED rUser GOTO :register
IF "%rUser:*=%" NEQ "%rUser%" GOTO :register
IF EXIST data/%rUser%.userfile (
ECHO.
ECHO User is already registered.
GOTO :register.choice
)
:rpass
SET /P rPassword=Register Password:
IF NOT DEFINED rPassword GOTO :rpass
SET chars=0123456789abcdefghijklmnopqrstuvwxyz
FOR /L %%N IN (10 1 36) DO (
FOR %%C IN (!chars:~%%N,1!) DO (
SET "rPassword=!rPassword:%%C=-%%N=!"
)
)
>data/%rUser%.userfile ECHO %rPassword%
ECHO.
ECHO Registration was successful!
TIMEOUT 2 >NUL
GOTO :A
:register.choice
ECHO.
CHOICE /C YN /M "Go to Login"
IF ERRORLEVEL 2 GOTO :register
GOTO :login
:console
REM Section is Missing
You should be able to run it in one of these ways, depending upon your requirements:
"X:\SomePathTo\wizzard.cmd" 1 wolfy sombest
start "" "X:\SomePathTo\wizzard.cmd" 1 wolfy sombest
call "X:\SomePathTo\wizzard.cmd" 1 wolfy sombest
Of course if you cannot change the actual wizzard.cmd script then I'm afraid that you have no way of bypassing the prompts' which should, after all, only waste a couple of seconds of your precious time.
instead of set /p correct = <C:\file , get rid of the p and type set correct =<C:\file
Why would you use a different batch file? But however, if you want to parse data between to batch instances, make use of command line arguments. A simple google to that gives you a clear explanation. But be carefull. Batch files are not safe to use passwords. You have to learn programming for that. VB.net is an example language which is easy to learn

Having problems with batch if statements

I've tried hard to find out what is wrong with my if statements... I probably missed something very obvious and stupid, but I can't find it.
#Echo OFF
:Home
set Password= "Pass"
set /p input= Password:
if "%input%" == "%Password%" (
Echo Password Correct!
pause
)
if "%input%" "%Password%" (
Echo Password Incorrect!
cls
goto Home
)
This doesn't work either:
#Echo OFF
:home
set Password= "Pass"
set /p input=Password:
if "%input%" == "%Password%" goto correct
if "%input%" != "%Password%" goto incorrect
:correct
Echo Password Correct!
pause
:incorrect
Echo Password Incorrect!
cls
goto home
Try this
#Echo OFF
:Home
set Password=Pass
set /p input= Password:
if "%input%" == "%Password%" (
Echo Password Correct!
pause
) else (
Echo Password Incorrect!
goto Home
)
3 changes made
Removed the quotes from the set Password line - in batch files, that is not necessary & will actually make quotes as part of the variable - so it will compare true only if the user also inputs quotes.
Removed the `cls1. Having a clear screen just after printing your error message will mean that you will never see the error message.
Change the 2 ifs to an if-else

Using CALL command with an IF condition in batch

I'm trying to set up a code with passwords, where a different password will take you to a different directory. All the directories are in the same .bat file, but i want to use another .bat to call the passwords and take me to the directory. I'm not sure how to do it, and am finding it difficult to locate in the topic searches.
My code looks like this:
#echo off
:takemeto
cls
echo Enter Password
set /p name=
call C:\Users\Username\Desktop\Batch files\passwords.bat
:password1
cls
echo You have chosen Password 1
:password2
cls
echo You have chosen Password 2
The CALL is meant to locate and use my file with the passwords. the files has the passwords and the directories, which looks like this:
:Passwords
if "%name%" == "password1" goto password1
if "%name%" == "password2" goto password2
/end
Hope this is clear,
Thanks-Rob
Have your external batch script echo its output. Capture it with a for /f loop.
#echo off
setlocal
set extbat=C:\Users\Username\Desktop\Batch files\passwords.bat
:takemeto
cls
set /p name="Enter Password: "
for /f %%I in ('"%extbat%" %name%') do goto %%I
:invalid
echo Invalid password entered.
set name=
pause
goto takemeto
:password1
cls
echo You have chosen Password 1
goto :EOF
:password2
cls
echo You have chosen Password 2
goto :EOF
example C:\Users\Username\Desktop\Batch files\passwords.bat:
#echo off
if #%1==# (
echo takemeto
) else if #%1==#correct1 (
echo password1
) else if #%1==#correct2 (
echo password2
) else echo invalid
... where correct1 and correct2 are the passwords you expect your user to enter.

Resources