CMD - Input from another cmd - batch-file

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

Related

when writing to a file cmd just crashes

I'm making a game in batch, but when i try to write to a file, cmd just crashes. I'm writing to a file to save data. So that the user can just come back and load it up.
Create User Code Block:
:cruser1
cls
set /a created=1
echo(
set /p name=Enter The Name of The User::
goto save
Load User Code Block:
:loaduser
cls
set /a created=0
echo(
set /p loaduser=Enter The Name of The User to Load::
if exist "%loaduser%.dll" (
< %loaduser%.dll (
set /p MHP=
set /p HP=
set /p DMG=
set /p GOLD=
set /p EXP=
set /p LVL=
set /p MEXP=
)
goto menu
)
if not exist "%loaduser%.dll" (
echo File not found.
timeout /t 3 /NOBREAK >nul
goto loaduser
)
Save Data Code Block:
:save
cls
echo(
if %created% equ 1(
(
echo %MHP%
echo %HP%
echo %DMG%
echo %GOLD%
echo %EXP%
echo %LVL%
echo %MEXP%
) > %name%.dll
attrib +h "%name%.dll"
goto menu
)
if %created% equ 0(
(
echo %MHP%
echo %HP%
echo %DMG%
echo %GOLD%
echo %EXP%
echo %LVL%
echo %MEXP%
) > %loaduser%.dll
attrib +h "%loaduser%.dll"
goto menu
)
Does anyone know how to fix this and why it's happening?
Edit: I debugged it, after saving echoing it worked, after i killed the enemy, It did say it worked, but also Access is denied.
Hopefully this helps.
You have errant parentheses, Issue one. issue 2 failure to test existance for existing hidden file
If exist "%loaduser%.dll" attrib -h "%loaduser%.dll"
if %created% equ 0 (
echo %MHP%
echo %HP%
echo %DMG%
echo %GOLD%
echo %EXP%
echo %LVL%
echo %MEXP%
) > %loaduser%.dll
attrib +h "%loaduser%.dll"
goto menu

I'm trying to create an admin "login" on batch and it just closes the cmd

I am trying to make an "admin" login and it isn't working.
#echo off
title RPG BETA
color 0b
set /p player=Welcome to Xero, what's your name?:
if %player% == Admin goto admin
:intro
echo Hello %player%!
pause
goto start
:admin
set /p password:What is the admin password?:
if %password% == Insertanypassword goto true
if %password not insertanypassword goto false
:true
cls
echo Welcome, Admin!
set /p af=Would you like to enable admin features?[y/n]:
if %af% == y goto true1
if %af% == n goto start
:true1
cls
set CPU=20
set CPUN=Scorpion
set CPUD=3
set gun=9999999
set gund=9999999
set playerh=9999999
goto fight
:start
cls
echo In the distant future, the world was on the brink of destrucion.
timeout 3 >nul
echo In the midst of the wasteland, a single man, named %player%, will overcome the odds
pause
goto BFight
:BFight
cls
set CPU=20
set CPUN=Scorpion
set CPUD=3
set gun=1
set gund=10
set playerh=20
:Fight
cls
if %CPU% leq 0 goto win
if %playerh% == 0 goto lose
cls
echo You encounter a %CPUN%!
echo %CPUN% Health: %CPU%
echo.
echo Your Health: %playerh%
echo [1]Shoot (%gund% damage) (%gun%)
echo [2]Punch (3 damage)
echo [3]Flee
set /p fp=What do you do?
if %fp% == 1 goto gun
if %fp% == 2 goto punch
if %fp% == 3 goto flee
:gun
cls
if '%gun%'=='0' goto egun
set /a gun = gun - 1
echo You fire at the %CPUN%
timeout 4 >nul
echo It hits!
set /a CPU = CPU - gund
pause
goto cpufightp
:punch
cls
echo You punch the %CPUN%
timeout 4 >nul
echo It hits!
set /a CPU = CPU - 3
pause
goto cpufightp
:egun
echo You have no bullets!
pause
goto Fight
:cpufightp
if %CPU% leq 0 goto win
cls
echo %CPUN% Health: %CPU%
echo.
echo Your Health: %playerh%
pause
cls
goto cpufight
:cpufight
cls
echo The %CPUN% Attacks!
timeout 4 >nul
echo It hits!
set /a playerh = playerh - CPUD
pause
goto fight
:flee
goto losef
:losef
cls echo you have fled
pause
goto suggestion
:lose
cls
echo You died
pause
cls
:win
cls
echo Congradulations, %player%! You win!
pause
cls
:suggestion
set /p suggest=What should I add to Xero?:
echo %player%: %suggest% >> xerosuggest.word
Whenever I type in "Admin" instead of going to the password screen, it just closes cmd, can anyone tell me why? i have experimented with ''s and other commands but I just can't figure it out.
The problem with respect to your specific issue is a typo.
You have:
set /p password:What is the admin password?:
Instead of:
Set /P "password=What is the admin password?: "
The doublequotes are simply good practice, your error was quite clearly that you used a : instead of a =.

Batch file closes after a goto command

Here's the code
#echo off
color 0a
:user_login
set /p username=What is your username?
if %username% == admin (
cls
goto admin_request_password
)
echo Incorrect username please try again.
cls
goto user_login
:admin_request_password
set /p Password=What is your Encryption Key?:
if %Password% == ecca1924889236 (
pause
cls
goto admin_menu
pause
)
echo Incorrect password
pause
cls
goto user_login
:admin_menu
echo Type acct_info for account info.
echo Type site_key for website file manager.
echo type ip_info for your ip info.
echo Type close to close the Net-Sec portal
goto admin_menu_process
:admin_menu_process
set /a choice=Waiting for Request:
if %choice% == acct_info (
cls
goto acct_info ) echo Incorrect term cls goto admin_menu
if %choice% == site_key (
goto site_key )echo Incorrect term
cls
goto admin_menu
if %choice% == ip_info (
goto ip_info ) echo Incorrect term
cls
goto admin_menu
if %choice% == close (
goto close ) echo Incorrect term
cls
goto admin_menu
:ip_info
ipconfig
pause
cls
goto admin_menu
:acct_info
echo Your username is: admin
echo your password is: ecca1924889236
pause
cls
goto admin_menu
:site_info
The username is: net-sec
The password is: netsec127
pause
cls
goto admin_menu
:close
cls
Suggestions:
Do not set a variable to %username% as that is already an existing system variable.
Enclose both strings with double quotes in a string comparison.
Use the choice command when you have a known set of choices.
Try not to include unnecessary labels.
Here is a rewrite of your code to hopefully help you in your scripting exploits:
#Echo Off
Color 0A
:getName
ClS
Set/P "uname=What is your username? "
If /I Not "%uname%"=="admin" (Echo Incorrect username please try again.
>Nul Timeout 3
GoTo getName)
:getPass
ClS
Set/P "password=What is your encryption key? "
If Not "%password%"=="ecca1924889236" (Echo Incorrect password please try again.
>Nul Timeout 3
GoTo getPass)
:menu
ClS
Echo [1] Account information.
Echo [2] Website File Manager.
Echo [3] IP information.
Echo [4] Close the Net-Sec portal.
Choice /C 1234 /M "Enter your request"
If ErrorLevel 4 GoTo close
If ErrorLevel 3 GoTo ip_info
If ErrorLevel 2 GoTo site_info
If ErrorLevel 1 GoTo acct_info
GoTo :EOF
:acct_info
Echo Your username is: admin
Echo your password is: ecca1924889236
Timeout 3
GoTo menu
:site_info
Echo The username is: net-sec
Echo The password is: netsec127
Timeout 3
GoTo menu
:ip_info
IPConfig
Timeout -1
GoTo menu
:close
ClS
Echo Closing...
Timeout 3

There has to be a better way to do this. (Batch file help)

Alright, so I'm looking for a better way to do this and I know there has to be since this is getting ridiculous. I want to add a 3rd service to the file, but that is a lot of fiddling around with the numbers I was using and I'll surely break something.
I'm really wanting to learn, but I'm really new to this so don't be too harsh!
#echo off
echo This will start your SERVICES.
echo If you do not wish to do this, please close this window, otherwise:
pause
:CORE
:DISABLED
for /F "tokens=3 delims=: " %%H in ('sc qc "SERVICE1" ^| findstr " START_TYPE"') do (
if /I "%%H" EQU "DISABLED" (
echo.
echo SERVICE1 is disabled!
echo.
set A=1
set C=3
set E=0
set M=1
goto FILE
)
if /I "%%H" NEQ "DISABLED" (
set A=0
set M=0
)
)
:STATUS
for /F "tokens=3 delims=: " %%H in ('sc query "SERVICE1" ^| findstr " STATE"') do (
if /I "%%H" NEQ "RUNNING" (
echo.
echo Starting SERVICE1... & SC Start "SERVICE1" | findstr /i /c:"1069" && (Echo.
echo ERROR 1069: SERVICE1 failed to start due to a unknown
echo username or bad password.
echo.
set E=3
set C=3
) || (
echo Successful
echo.
set C=0
set E=0
)
)
if /I "%%H" EQU "RUNNING" (
echo.
echo SERVICE1 is already started!
echo.
set C=1
set E=0
)
)
:FILE
:DISABLED
for /F "tokens=3 delims=: " %%H in ('sc qc "SERVICE2" ^| findstr " START_TYPE"') do (
if /I "%%H" EQU "DISABLED" (
echo.
echo SERVICE2 is disabled!
echo.
set B=1
set D=3
set F=0
set N=1
goto FIX
)
if /I "%%H" NEQ "DISABLED" (
set B=0
set N=0
)
)
:STATUS
for /F "tokens=3 delims=: " %%H in ('sc query "SERVICE2" ^| findstr " STATE"') do (
if /I "%%H" NEQ "RUNNING" (
echo Starting SERVICE2... & SC Start "SERVICE2" | findstr /i /c:"1069" && (Echo.
echo ERROR 1069: SERVICE2 failed to start due to a unknown
echo username or bad password.
echo.
set F=3
set D=3
) || (
echo Successful
echo.
set D=0
set F=0
)
)
if /I "%%H" EQU "RUNNING" (
echo.
echo SERVICE2 is already started!
echo.
set D=1
set F=0
)
)
:MESSAGE
set /a G=%C%+%D%
if "%G%" LEQ "1" (
echo.
echo Your services should now be started!
echo.
)
if "%G%" EQU "2" (
echo.
echo No problem found!
echo.
)
:FIX
set /a O=%M%+%N%
if "%O%" EQU "2" (set S=Services&& goto HELP)
if "%M%" EQU "1" (set S=SERVICE1&& goto HELP)
if "%N%" EQU "1" (set S=SERVICE2&& goto HELP)
:HELP
set /a H=%A%+%B%
if "%H%" NEQ "0" (
echo.
echo To enable the %S%, follow these steps:
echo ^(1^) Open your start menu.
echo ^(2^) Type in ^"Services.msc" and click on the top option.
echo ^(3^) Scroll down in the list to find the ^"SERVICE1" and
echo the "SERVICE2".
echo ^(4^) Right-click on each respective service and select properties.
echo ^(5^) In the middle of the screen, change ^"Startup type" to "Automatic".
echo ^(6^) Once this is done for the respective service^(s^), run this file again.
echo.
set P=1
)
set /a I=%E%+%F%
if "%I%" GEQ "3" (
echo.
echo To correct the 1069 error, follow these steps:
echo ^(1^) Open your start menu.
echo ^(2^) Type in ^"Services.msc" and click on the top option.
echo ^(3^) Scroll down in the list to find the ^"SERVICE1" and
echo the "SERVICE2".
echo ^(4^) Right-click on each respective service and select properties.
echo ^(5^) At the top click on the ^"Log On" tab.
echo ^(6^) Type in the correct password for the named user and confirm it just below.
echo ^(7^) Once this is done for the respective service^(s^), run this file again.
echo.
set P=1
)
if "%P%" EQU "1" (
echo.
echo If any additional errors came up or if this same message comes up after
echo following the above steps, be sure to give Tech Support a call at
echo 1-800-XXX-XXXX.
echo.
)
:FOR DEBUG USE
:echo A^=%A%
:echo B^=%B%
:echo C^=%C%
:echo D^=%D%
:echo E^=%E%
:echo F^=%F%
:echo G^=%G%
:echo H^=%H%
:echo I^=%I%
:echo M^=%M%
:echo N^=%N%
:echo O^=%O%
:echo P^=%P%
:echo S^=%S%
echo To close this window:
pause
Thanks in advance! - Trevor
The principal is functions. You are doing the same thing over and over again.
Create a file with a list of services.
win32time
wscsvc
wuauserv"
To start get a list of services.
for /f %%A in (c:\services.txt) do call Main %%A
goto :EOF
Then the main worker function I just changed service name to %1 and added :main at top and goto :eof at bottom. This will be called three times which each of the three service above as %1. I haven't checked your logic though.
:MAIN
for /F "tokens=3 delims=: " %%H in ('sc qc "%1" ^| findstr " START_TYPE"') do (
if /I "%%H" EQU "DISABLED" (
echo.
echo %1 is disabled!
echo.
set A=1
set C=3
set E=0
set M=1
goto FILE
)
if /I "%%H" NEQ "DISABLED" (
set A=0
set M=0
)
)
:STATUS
for /F "tokens=3 delims=: " %%H in ('sc query "%1" ^| findstr " STATE"') do (
if /I "%%H" NEQ "RUNNING" (
echo.
echo Starting %1... & SC Start "%1" | findstr /i /c:"1069" && (Echo.
echo ERROR 1069: %1 failed to start due to a unknown
echo username or bad password.
echo.
set E=3
set C=3
) || (
echo Successful
echo.
set C=0
set E=0
)
)
if /I "%%H" EQU "RUNNING" (
echo.
echo %1 is already started!
echo.
set C=1
set E=0
)
)
goto :eof
In programming we don't check something then do, we do then check if it worked. It usually takes a similar time to check as to do. Therefore you are using twice the battery power and putting twice the carbon in the atmosphere, etc. If on a server you may need a bigger one.
This is what you seem to want to do.
sc config %1 start= auto
sc start %1
Then do your test for wrong password (1069), and if already running you'll get error 1056.

using wildcards to copy directories in .bat files

I have created a .bat file where i input a job number. I want to copy the contents of one folder using the input value with wildcards to another with only the input value. It will not copy the contents of this folder using wildcards. Any suggestions?
sample:
Input = 19000
c:\test2\19000 project\test.doc
c:\test\19000
.bat file below
COLOR 0a
:USERID
CLS
ECHO Enter User Name:
ECHO > NUL
SET /p USERID=
IF %USERID% EQU c GOTO PASSWORD
IF %USERID% LSS c GOTO INVALID
IF %USERID% GTR c GOTO INVALID
:PASSWORD
CLS
ECHO Enter Password:
SET /p PASSWORD=
IF %PASSWORD% EQU c GOTO :job number
IF %PASSWORD% LSS c GOTO INVALID
IF %PASSWORD% GTR c GOTO INVALID
CLS
:INVALID
ECHO INAVLID LOGIN.....
PING -n 3 127.0.0.1 > NUL
ECHO Please try again...
PING -n 3 127.0.0.1 > NUL
GOTO USERID
:Job Number
Set input1=
set /p input1= Job Number:
cls
ECHO OFF
c:
cd\
xcopy "c:\test2\%input1% "*" " "c:\test\%input1%" /e
cd\
c:
cls
EOF
exit
This should xcopy any folder that matches the query with a wildcard after it, to the c:\test\ folder with the same folder structure.
#echo off
COLOR 0a
CLS
:USERID
SET /p "USERID=Enter User Name: "
IF "%USERID%" NEQ "c" GOTO INVALID
ECHO(
SET /p "PASSWORD=Enter Password: "
IF "%PASSWORD%" NEQ "c" GOTO INVALID
ECHO(
Set "input1="
set /p "input1=Job Number: "
for /d /r "c:\test2\" %%a in ("%input1%*") do (
xcopy "%%a\*.*" "c:\test\%%~nxa\" /s/h/e/k/f/c
)
cls
exit
goto :EOF
:INVALID
ECHO INVALID LOGIN.....
PING -n 3 127.0.0.1 > NUL
ECHO Please try again...
PING -n 3 127.0.0.1 > NUL
goto :userid

Resources