:deleteaccount
cls
echo what account do you want to delete?
set /p dan=
if not exist %~dp0\database\%dan%\ (
echo this account doesn't exist & pause >nul & goto stage
)
else %~dp0\database\%dan%\ (
cls & echo password:
set /p dap=
call %~dp0\database\%dan%\%dan%.bat
if %dap% == %rpassword1% (
echo are you sure you want to delete this account? yes/no
set /p daq=
if %daq% == yes (
#RD /S /Q %~dp0\database\%daq%\
echo account succesfully deleted
pause >nul & goto stage)
if %daq% == no (goto stage)
)
)
After I type the correct password for the account I wanted to delete, it says < is unexpected for some reason.
For the purposes of saving screen real estate, here's an example of your script, modified to use the correct syntax and to include a logical structure.
#Echo Off
:DeleteAccount
ClS
Set "dan=:"
Set /P "dan=Which account do you want to remove? "
If Not Exist "%~dp0database\%dan%\" (
Echo The account does not exist.
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
GoTo Stage
)
ClS
If Not Exist "%~dp0database\%dan%\%dan%.bat" (
Echo The required batch file is missing for the account.
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
GoTo Stage
)
Set "dap="
Set /P "dap=Please enter your password: "
Call "%~dp0database\%dan%\%dan%.bat" 2> NUL
If ErrorLevel 1 (
Echo A error occurred running the batch file.
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
GoTo Stage
)
If "%dap%." == "%rpassword1%." (
"%__AppDir__%choice.exe" /M "Are you sure you want to remove this account"
If Not ErrorLevel 2 (
RD /S /Q "%~dp0database\%daq%" 2> NUL
If ErrorLevel 1 (
Echo An error occurred deleting the account.
) Else Echo Account successfully removed.
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
)
) Else (
Echo Incorrect password.
"%__AppDir__%timeout.exe" /T 3 /NoBreak 1> NUL
)
:Stage
Rem Rest of code goes here.
Please read and review it, taking particular notice of the included double-quotes which are used to enclose and protect special characters in strings. The first two and last two lines were included to keep this example independent, (as they were omitted from your posted snippet).
Related
Coding is just something I like to play with so I have limited knowledge and research hasn't helped. If I run the script I get both success and error messages instead of one or the other.
#ECHO OFF
SET /p HOSTFILE=Host File Name?&CLS
SET /p HIDEFILE=File To Hide?&CLS
SET /p OUTPUT=Output File Name?&CLS
SET /p EXTENSION=Output File Extension (.Zip, .Jpg, .M4A)?&CLS
COPY /b "%HOSTFILE%" + "%HIDEFILE%" %OUTPUT%%EXTENSION% >null
if %ERRORLEVEL% EQU 0 GOTO continue
if %ERRORLEVEL% EQU 2 GOTO error
:continue
ECHO %OUTPUT%%EXTENSION% Created.
:error
ECHO File Not Created.
TIMEOUT /T 2 >null
As Compo said in the comment you should probably check if your files exist.
when both hostfile and hidefile exist, a success message is indicated otherwise error message follows.
#ECHO OFF
SET /p HOSTFILE=Host File Name?&CLS
SET /p HIDEFILE=File To Hide?&CLS
SET /p OUTPUT=Output File Name?&CLS
SET /p EXTENSION=Output File Extension (.Zip, .Jpg, .M4A)?&CLS
IF EXIST "%HOSTFILE%" IF EXIST "%HIDEFILE%" (
echo exist hidefile
COPY /b "%HOSTFILE%" + "%HIDEFILE%" %OUTPUT%%EXTENSION% > nul
GOTO continue
)
) ELSE (
echo does not exist
GOTO error
)
:continue
ECHO %OUTPUT%%EXTENSION% Created.
GOTO timer
exit /b 0
:error
ECHO File Not Created.
GOTO timer
exit /b 0
:timer
TIMEOUT /T 2 > nul
exit /b 0
I am hoping you can help me. So, I have been trying to run this command. But everytime I do so..it exits out. Then, I found this error.."batch file do( was unexpected at this time" I am not sure where I made a mistake. Thanks in advance for all the replies.
Here is my code:
#echo off
for /L %%n in (1,1,3) do (
set uname=mama
set pword=mu
:xy
set /p user ="Enter Username"
set /p pass="Enter Pass"
if %uname%==%user% ( echo
username is valid ) ELSE (
echo username not found
goto xy
)
IF %pword%==%pass% ( echo sucess
) ELSE (
echo invalid password
goto xy
)
pause
if %%n EQU 3 (echo Run Again)
)
This is elaborated based in #Stephan comments:
Replace the for loop with a _cnt counter to limit attempts by if.
#echo off && setlocal enabledelayedexpansion
set "uname=mama" && set "pword=mu"
:xy
set /a "_cnt+=1+0" && set /p "user=Enter Username: "
if not !_cnt! EQU 3 (
if "!uname!" == "!user!" (echo/ Username is valid^!!
) else ( echo/ Username not found^!! & goto :xy )
set /p "pass=Enter Pass: "
if "!pword!" == "!pass!" (echo/ Sucess^!!
) else ( echo/ Invalid password^!! & goto :xy )
) else ( echo/ Run Again^!! && %__APPDIR__%timeout -1 & endlocal & goto :EOF )
rem./ do more tasks here... after use endlocal ... && endlocal & goto :EOFF
You had a few errors in your code.
labels are not allowed in code blocks and may break them. // Labels removed.
goto unconditionally breaks the code block // left one goto to intentionally leave the loop and removed the rest
you need delayed expansion to use a variable changed within the same code block (technically not needed for uname and pword, as I defined them outside the loop) // enabled and used delayed expansion
You had a space in set uname=mama<space> // fixed by recommended syntax set "var=value"
(security issue) for security reasons, you shouldn't tell if it was the username or the password (or both), which was wrong. // fixed (as a bonus, it simplifies the code)
With the above errors fixed and reduced code complexity, it could look like:
#echo off
setlocal enabledelayedexpansion
set "uname=mama"
set "pword=mu"
for /L %%n in (1,1,3) do (
set /p "user=Enter Username: "
set /p "pass=Enter Password: "
if "!uname!-!pword!" == "!user!-!pass!" goto :payload
echo Invalid username or password.
pause
)
echo sorry, run again.
goto :eof
:payload
echo successfully logged in.
REM rest of your code
(speaking of security: having the credentials in clear text in the (readable) code isn't secure by any standard, but it's hard to overcome with pure batch)
Looking at your code it appeared to me that using Call would solve both your nested label and delayed expansion issues, and probably make your code easier for you to manage.
#Echo Off
Set "uname=mama"
Set "pword=mu"
Set "user="
Set "pass="
:Inputs
For /L %%G In (1,1,3) Do (
Call :xy
If Not ErrorLevel 1 GoTo Main
)
"%__AppDir__%choice.exe" /M "Would you like to try again"
If Not ErrorLevel 2 GoTo Inputs
Exit /B 1
:Main
Rem Your code below here for users with successful inputs.
Echo Welcome %user%.
Timeout /T 3 /NoBreak > NUL
Rem Your code ends above here.
Exit /B 0
:xy
ClS
Set /P "user=Enter Username> %user%"
If /I Not "%uname%" == "%user%" (
Echo Username not found.
Set "user="
Timeout /T 2 /NoBreak > NUL
Exit /B 1
)
Echo Username is valid.
Set /P "pass=Enter Pass> "
IF "%pword%" == "%pass%" Exit /B 0
Echo Invalid password.
Set "pass="
Timeout /T 2 /NoBreak > NUL
Exit /B 1
You may notice that I've used the recommended syntax for defining variables with the Set command. It ensures that the content is protected and does not have things such as hidden trailing spaces. If you take a look at the code in your question, you will note that the value assigned to the uname variable is actually mama<space>. This would obviously cause issues when mama is locked out of the rest of the script!
You also notice had an unwanted newline instead of a space between echo and username is valid.
Edit
The code above was re-worked to allow for up to three attempts at entering both values correctly, although the try again message seems a little redundant then.
My goal is to first prompt, echo the line, issue the next prompt, then display press any key to exit....
I'm not sure why my batch-file isn't issuing the second prompt.
#echo off
::deploying to test
set /p tdeploy="Deploy to test: [y/n]"
IF /I "%tdeploy%"=="y"(
call :deploy_test
if /I "%ERRORLEVEL%" NEQ "0"(
echo Deploy test failed
)
)
::deploying to argos
set /p adeploy="Deploy to argos: [y/n]"
IF /I "%adeploy%"=="y"(
call :deploy_argos
if /I "%ERRORLEVEL%" NEQ "0"(
echo Deploy argos failed
)
)
set /p DUMMY=Press any key to exit...
:deploy_test
ECHO deploying test!
goto :eof
:deploy_argos
ECHO deploying argos!
goto :eof
:eof
set /p DUMMY=Press any key to exit.222..
I usually prefer to structure it as a nested IF ELSE to avoid a bunch of GOTO commands and having to figure out error levels:
#echo off
REM deploying to test
set /p tdeploy="Deploy to test: [y/n]"
if /i "%tdeploy%" == "y" (
echo deploying test!
) else (
if /i "%tdeploy%" == "n" (
echo deploy test cancelled
) else (
echo seriously, there were only two options...
)
)
)
REM deploying to argos
set /p adeploy="Deploy to test: [y/n]"
if /i "%adeploy%" == "y" (
echo deploying test!
) else (
if /i "%adeploy%" == "n" (
echo deploy test cancelled
) else (
echo seriously, there were only two options...
)
)
)
pause
The pause will be your "Press any key to continue..." - the rest of the spacing is just to make it more visible.
I would suggest you use choice.exe for your Y,N questions:
#Echo Off
Rem Deploying to test
Choice /M "Deploy to test"
If "%ERRORLEVEL%"=="1" (Call :deploy_test
If ErrorLevel 1 Echo Deploy test failed)
Rem Deploying to argos
Choice /M "Deploy to argos"
If "%ERRORLEVEL%"=="1" (Call :deploy_argos
If ErrorLevel 1 Echo Deploy test failed)
Echo Press any key to exit...
Timeout /T -1 >NUL
GoTo :EOF
:deploy_test
Echo Deploying test!
Timeout /T 3 /NoBreak >NUL
Exit /B 0
:deploy_argos
Echo Deploying argos!
Timeout /T 3 /NoBreak >NUL
Exit /B 1
In the example above, I have used two different exit codes, to simulate the returned error level for each of the two deployment options.
In a batch script I'm running sqlcmd in a loop that goes through a folder of SQL scripts. I would like to be able to pass the script/output file currently being worked on to a label exterior to the loop in the case of an error.
Here's sample code:
#echo off
#setlocal enabledelayedexpansion
SET _INSTANCE=someinstance
SET _DATABASE=somedatabase
SET "_SCRIPTFOLDER=D:\Scripts for Testing"
SET "_OUTPUTFOLDER=D:\Output for Testing"
FOR %%S IN (
"%_SCRIPTFOLDER%\*.sql"
) DO (
SET /P _MSGa=Generating CSV: %%~nS.csv ... <NUL
sqlcmd -b -S %_INSTANCE% -d %_DATABASE% -i "%%~fS" -s "|" -o "%_OUTPUTFOLDER%\%%~nS.csv" -W
IF ERRORLEVEL >= 1 GOTO sqlcomderrorhandling
SET /P _MSGb=file created. Removing header dashes ... <NUL
REM REM Remove the line with dashes below the header
#FINDSTR /r /b /v /c:"-*|" "%_OUTPUTFOLDER%\%%~nS.csv" > "%_OUTPUTFOLDER%"\tmp.txt
IF ERRORLEVEL >= 1 GOTO findstrerrorhandling
XCOPY /Y "%_OUTPUTFOLDER%"\tmp.txt "%_OUTPUTFOLDER%\%%~nS.csv" >NUL
IF ERRORLEVEL >= 1 GOTO copyerrorhandling
ECHO done.
)
DEL /Q /F "%_OUTPUTFOLDER%"\tmp.txt
GOTO done
:sqlcomderrorhandling
ECHO An error occurred while processing the file %%~nS.csv
:done
#pause
The last ECHO just outputs %~nS.csv, not the actual name of the CSV file. Do I need to utilize functions in some way to do what I want to do?
Based on part of rojo's first comment and my own, would this sort of structure not make more sense?
#Echo Off
Set "_INSTANCE=someinstance"
Set "_DATABASE=somedatabase"
Set "_SCRIPTFOLDER=D:\Scripts for Testing"
Set "_OUTPUTFOLDER=D:\Output for Testing"
If Not Exist "%_OUTPUTFOLDER%\" (Echo Output folder doesn't exist
Timeout 3 /NoBreak >Nul
GoTo :EOF)
If Not Exist "%_SCRIPTFOLDER%\*.sql" (Echo Source folder doesn't contain any SQL files
Timeout 3 /NoBreak >Nul
GoTo :EOF)
CD /D "%_SCRIPTFOLDER%" 2>Nul || (Echo Source folder, %_SCRIPTFOLDER%, is not available.
Timeout 3 /NoBreak >Nul
GoTo :EOF)
For %%A In (*.sql) Do (
Echo Generating CSV: %%~nA.csv ...
SQLCmd -b -S %_INSTANCE% -d %_DATABASE% -i "%%A" -s "|" -o "%_OUTPUTFOLDER%\%%~nA.csv" -W 2>Nul && (
Echo File created. Removing header dashes ...
Findstr "[^-|]" "%_OUTPUTFOLDER%\%%~nA.csv">"%_OUTPUTFOLDER%"\%%~nA.tmp" && (
Move /Y "%_OUTPUTFOLDER%"\%%~nA.tmp" "%_OUTPUTFOLDER%\%%~nA.csv">Nul 2>&1 || (
Echo A Move error occurred while moving %%~nA.tmp
Timeout 2 /NoBreak >Nul)) || (Echo A FindStr error occurred while removing header dashes
Timeout 2 /NoBreak >Nul)) || (Echo A SQLCmd error occurred while processing the file %%~nS.csv
Timeout 2 /NoBreak >Nul))
Pause
what i want is if user already exist just put the name of this user in errorlog.txt
i have this:
set var_b=123
FOR /F %%v in (%arxiu%) do (
net user %%v >nul 2>nul
if errorlevel 1 ( net user %%v %var_b% /add >nul 2>nul
)
if errorlevel 0 ( echo %%v >> C:\Users\%username%\Desktop\errorlog.txt
)
)
but with this code i get the name of users that already exist and the one news that creates i dont know why any error ?
See Microsoft support article Testing for a Specific Error Level in Batch Files.
if errorlevel X means if exit code of previous command is greater or equal X.
Use following batch code:
set "var_b=123"
for /F %%v in (%arxiu%) do (
net user %%v >nul 2>nul
if errorlevel 1 (
net user %%v %var_b% /add >nul 2>nul
) else (
echo %%v>>"%USERPROFILE%\Desktop\errorlog.txt"
)
)