Exit from cmd on error in connection to Oracle database - database

The following code is for connecting to Oracle database through batch file by prompting for Username, password and database instance. What I want is to exit from cmd itself if any of the credentials is invalid, else it should proceed as normal.
#ECHO OFF
:START
set /p U="Enter Username(User ID):"
set /p P="Enter Password(Password#o1234abc):
:NEXT
set /p a="Do you want to display details of 1a?(y/n)"
ECHO "%a%"
if "%a%" == "y" sqlplus %U%/%P% #".\AB_1a.sql"
if "%a%" == "n" goto A
set /p id1= "Please enter ID for 1a(press 's' to SKIP):"
if "%id1%" == "s" goto A
sqlplus %U%/%P% #".\AB_.sql" %id1%
goto A
:End
Please suggest any solution.

You could do something like this:
#echo off
setlocal enableDelayedExpansion
set /p U="Enter Username(User ID):"
set /p P="Enter Password(Password#o1234abc):
rem Check creds!
if "!U!" == "JoeBloggs" if "!P!" == "Pa$$w0rd" goto :CREDSOK
exit >nul
:CREDSOK
set /p a="Do you want to display details of 1a?(y/n)"
if "%a%" == "y" sqlplus %U%/%P% #".\AB_1a.sql"
if "%a%" == "n" goto A
set /p id1= "Please enter ID for 1a(press 's' to SKIP):"
if "%id1%" == "s" goto A
sqlplus %U%/%P% #".\AB_.sql" %id1%
goto A
:End
If creds are correct it will skip the line to exit and run the rest, if either of the creds are wrong it will hit the exit line and terminate cmd.
Thanks to #jeb who suggested delayed expansion for special characters, see comment below.
Hope this helps.

Related

The batch file keeps crashing when the user enters text

I've been trying to get this code to work...
I've tried re doing the code... to no avail also, the code below is a bit big...
:ST
if exist password.txt goto login
if not exist password.txt goto signup
:login
set /p user=<username.txt
set /p pass=<password.txt
set /p answer=Username:
if %answer% = %user% goto p1
if NOT %answer% = %user% goto errlog111
exit
:p1
set /p answer=Password:
if %answer%=%pass% goto
if NOT %answer% = %pass% goto errlog111
echo success
pause
exit
:signup
title Username needed for account creation! / Newex / Unregistered
set /p answer=Username:
echo %answer% > username.txt
cls
set /p answer=Password:
echo %answer% > password.txt
cls
echo Account created!
goto login
exit
:errorlog111
echo Username / Password incorrect!
pause
goto login
So, what I want is for it to check the contents of password.txt, (or username.txt) for what the user inputs, and if the user inputs the right thing, it goes on to the next thing. However, if they get the password, or username wrong then I want it to say "Username / Password wrong!" but for some reason when I enter the username, the batch file just closes! Please help!
if %answer% = %user% goto p1
The correct syntax is to use == or EQU.
Better syntax is
if "%answer%" == "%user%" goto p1
the quotes protect against some illegal inputs and where the arguments contain certain characters like , ; and would be advised for any comparison between strings.
Note that if /i ... makes the comparison case-insensitive (for future reference)
Also - a hard-to-see error
echo %answer% > username.txt
will include the space which follows the value of answer into the file
> username.txt echo %answer%
will not include this (presumably) unwanted space.

My batch file skips specific loops

My batch file skips specific loops and just does certain loops (it only does the first three loops and ignores the rest).
I tried rewriting the batch file, incorporating the new code into an old existing batch file I wrote, but I can't get it working.
echo 1. Login
echo 2. Exit
echo.
set /P M = Command :
IF M == 1 GOTO 1
:1
cls
echo Login
echo.
Set Pass1 = MCTh3sn3r
Set /P Pass = Enter Password :
IF Pass == %Pass1% GOTO 2
:2
cls
echo.
echo Login Successful GOTO 4
:3
cls
echo.
echo Incorrect Password
:4
echo.
echo 1. Shutdown (S)
echo 2. Reboot (R)
echo 3. Logoff (L)
echo 4. Task Manager (TM)
echo 5. Command Prompt (CP)
echo 6. File Locker (FL)
echo. Set /P Choice = Command :
IF Choice == "S" GOTO 5
IF Choice == "R" GOTO 6
IF Choice == "L" GOTO 7
IF Choice == "TM" GOTO 8
IF Choice == "CP" GOTO 9
IF Choice == "FL" GOTO 10
:5
cls
echo.
echo System will now shutdown
Timeout /t 5
Shutdown /s
GOTO End
:6
cls
echo.
echo System will now reboot
timeout /t 5
Shutdown /r
GOTO End
:7
cls
echo.
echo System will now logoff
timeout /t 5
Logoff
GOTO End
:8
gpupdate /force
gpupdate /force
gpupdate /force
gpupdate /force
start taskmgr
GOTO End
:9
gpupdate /force
gpupdate /force
gpupdate /force
gpupdate /force
start cmd
GOTO End
:10
(
if EXIST "PrivateFolder" goto
11
if NOT EXIST Locker goto 12
:13
echo Are you sure you want to
lock the folder (Y/N) ?
set /p Choice = Command :
IF Choice == "Y" goto 14
IF Choice == "y" goto 14
IF Choice == "N" goto 15
IF Choice == "n" goto 15
echo Invalid Choice goto 13
:16
ren Locker "PrivateFolder"
attrib +h + s "PrivateFolder"
echo Folder Locked GOTO End
:11
echo Enter password to unlock
file :
set /p Pass = Password :
IF NOT %Pass% == MCTh3sn3r goto
17
attrib -h -s "PrivateFolder"
ren "PrivateFolder" Locker
echo File locked successfully.
GOTO End
:17
echo Invalid password.
GOTO End
:12
md Locker
echo Locker created
successfully.
GOTO End
)
:End
Exit
Well lets start at the TOP.
This set of code has three things wrong with it.
Do not leave a space between your variable names and variable assignments.
When you need to use a variable you need to surround it with percent symbols.
Your GOTO command will GOTO label 1 regardless of the variable M equaling 1.
.
echo 1. Login
echo 2. Exit
echo.
set /P M = Command :
IF M == 1 GOTO 1
:1
This code should really look something like this.
echo 1. Login
echo 2. Exit
echo.
set /P M=Command :
IF "%M%"=="1" (
GOTO 1
) else (
GOTO END
)
:1
You make the same mistakes in the next set of code. Not sure why you chose to use the percent symbols for one variable but not the other. Again, you need to close up the spaces, use percent symbols for all your variables and your GOTO will go to the label 2 regardless so you need some other code to branch to.
cls
echo Login
echo.
Set Pass1 = MCTh3sn3r
Set /P Pass = Enter Password :
IF Pass == %Pass1% GOTO 2
:2
So again, using my best coding practices I would code it like this.
:1
cls
echo Login
echo.
Set Pass1=MCTh3sn3r
Set /P Pass=Enter Password :
IF "%Pass%"=="%Pass1%" (
GOTO 2
) else (
Echo Wrong Password
Pause
GOTO 1
)
Just going to keep repeating myself. Same problems. Close up the spaces and use percent symbols with your variables. Might want to throw in a case insensitive check for their choices on this one. Not sure how you thought their answers were ever going to match here. You have quotes surrounding one side of your comparison but not the other. Not even going to correct this code you should understand by now what you are doing wrong.
echo. Set /P Choice = Command :
IF Choice == "S" GOTO 5
IF Choice == "R" GOTO 6
IF Choice == "L" GOTO 7
IF Choice == "TM" GOTO 8
IF Choice == "CP" GOTO 9
IF Choice == "FL" GOTO 10
No clue why you are using parentheses here.
:10
(
And here.
)
:End
Exit
This one is dead obvious. You can't have your label on another line.
if EXIST "PrivateFolder" goto
11
And here again. Close up the spaces. Put quotes on both sides of your comparisons. Use percent symbols to reference a variable. And read the HELP for the IF command. Use the /I option!
:13
echo Are you sure you want to
lock the folder (Y/N) ?
set /p Choice = Command :
IF Choice == "Y" goto 14
IF Choice == "y" goto 14
IF Choice == "N" goto 15
IF Choice == "n" goto 15
echo Invalid Choice goto 13
While we are here I would advise that if you are going to keep using the same variable over and over and over again that you clear out the variable before you ask the question. Make this change above.
set "Choice="
set /p Choice=Command :
Couple of problems here. The word file: is on its own line. The number 17 is on its own line. You finally realized that you needed to use percent symbols for you variables but the variable %Pass% is not the same as the variable %Pass %. Close up the spaces and use quotes.
echo Enter password to unlock
file :
set /p Pass = Password :
IF NOT %Pass% == MCTh3sn3r goto
17

windows 7 bat file closes after user types response

I am trying to write a bat file that upon startup will prompt a user. I can get the user prompt but get an error and the bat file closes. Basically, if the answer is y then a vb at the path is called and if the answer is n then the bat exits. Is the syntax below close? Thank you :).
#ECHO OFF
:choice
set /P c=Do you want to send the DOSE report[y/n]?
if /I "%c%" EQU "Y" goto :L:\NGS\test_email_DOSE.xlsx
if /I "%c%" EQU "N" goto :exit
goto :choice
pause
exit
Try this:
#ECHO OFF
:choice
set /P c=Do you want to send the DOSE report[y/n]?
if /i %c%==y (
L:\NGS\test_email_DOSE.xlsx
) else if /i not %c%==n goto choice
pause
In this script the second if is only reached when the answer was not 'y'. Also, the goto you used always requires a label to jump to. A path is not a label.
I assume of course that L:\NGS\test_email_DOSE.xlsx is a valid path.
When I run it on my pc it produces the following (I saved the file as decide.cmd):
D:\tmp>decide
Do you want to send the DOSE report[y/n]?d
Do you want to send the DOSE report[y/n]?f
Do you want to send the DOSE report[y/n]?y // (the Excel file is opened)
Press any key to continue . . .
D:\tmp>
I'd probably go this way. The previous answer would do fine. But this approach looks cleaner in my opinion.
#ECHO OFF
:choice
SET /P c=Do you want to send the DOSE report [Y/N]?
IF /I %c%==Y (START "" "L:\NGS\test_email_DOSE.xlsx")
IF /I %c%==N GOTO :EOF
GOTO :choice

Set /p %foo% doesn't function as expected

I have a problem with my code. I am trying to make a "hacker tool" with the tree command. Here is my code:
#echo off
title $userOne ProxyMatrix
color a
echo Hello $userOne,
echo Please enter search function for today's commands:
set /p %commands%=""
:redo
echo Specify Storage Device
set /p %drive%=""
title $userOne ProxyMatrix: Running on %drive% drive at %random% bits per nano
color C
tree %drive% /f
:runagain
color a
echo Run again?
set /p %redo%=""
if %redo%="yes" goto redo
else if %redo%="y" goto redo
else if %redo%="Y" goto redo
else if %redo%="Yes" goto redo
else if %redo%="no" goto end
else if %redo%="No" goto end
else if %redo%="n" goto end
else if %redo%="N" goto end
else echo Thats not a valid answer!
pause
goto runagain
:end
echo Thank you for choosing InGen, inc.
pause
I realize that this won't "hack" anything, its more of a novelty. The problem is, the set /p %redo% and the if/else if statements don't work. They just quit the program. Can someone explain what i'm doing wrong? Thanks.
Syntax is set /p variable=prompt.
Instead of set /p %redo%="" write set /p redo="" or even better set /p "redo="
EDIT
your if syntax is broken too.
Syntax is: if value1==value2 command or if value1==value2 (command1) else (command2)
"Best Practice is to enclose both sides of the comparison with quotes (to avoid syntax errors with empty values or contained spaces):
if "%variable"=="value" echo yes
I would shorten the code to:
set /p %redo%=""
if /i "%redo:~0,1%"=="y" goto redo
if /i "%redo:~0,1%"=="n" goto end
else echo Thats not a valid answer!
/i tells if to ignore capitalization
%variable:~0,1% means "take a substring starting with the first letter (counting starts at 0) with length=1 (so it takes the first letter)
(there is no else needed)

Bat file to query service status with options

I have this query to check the status of a service and give options based on that status but I am having some issues in the error portion. IT only seems to work of there is not an option chosen. Even then it only works the first time. AS you can see I have 3 options but if i select option 4 it defaults to option 1. PLease take a look and explain what I am missing.
#echo off
ECHO Status of MGT System Manager Service
Goto Welcome
ECHO.
:Welcome
sc query MGTSM | findstr /i "STATE"
ECHO What would you like to do?
ECHO 1. Start Service
ECHO 2. Stop Service
ECHO 3. Exit
set /p choice=Select your choice.
if '%choice%'=='' ECHO "%choice%" is not valid please try again.
if '%choice%'=='1' goto Start Service
if '%choice%'=='2' goto Stop Service
if '%choice%'=='3' goto Exit
ECHO.
:Start Service
net start MGTSM
goto Welcome
:Stop Service
net stop MGTSM
goto Welcome
:Exit
exit
set /p does not touch the variable, if you enter nothing (just ENTER). Therefore you should delete the variable before:
:Welcome
....
set "choice=" //this deletes %choice%
set /p "choice=Select your choice: "
if '%choice%'=='1' goto StartService
if '%choice%'=='2' goto StopService
if '%choice%'=='3' goto Exit
REM this line is only reached, when all of the "IF ... GOTO" above failed
echo not valid input: "%choice%"
goto welcome
...
Labels and goto have a problem with spaces: goto start service will go to "start", ignoring "service". Same with Labels: :Start Service, the Label is :Start, the "Service" is ignored.

Resources