-1
  1. So I currently creating a program that hide a files and moving it to other places, and to retrieve the file you need to put the password. But i found a problem where when i only press ENTER and not putting a password, the batch program closed. I wanted to make it so that when you press ENTER or not putting a password it says "Please enter the password!" until it gets what it wanted. (to be honest this is just something that i googled and copy paste, and change it a little)

  2. Additional question, is it fine to use set /p "something=>" instead of set /p something=

Here is the commands and codes that i used.

I know that this is complicted and might be the worst thing to write.

@ECHO OFF
title unlock the stuff
if EXIST SecretCodes goto CONFIRM
if NOT EXIST SecretCodes goto CONFIRMATION

:CONFIRM
echo are you sure you want to Lock the stuff?(Y/N)
set /p "cho=>"
if %cho%==Y goto LOCK
if %cho%==y goto LOCK
if %cho%==n goto END
if %cho%==N goto END
echo excuse me?
goto CONFIRM

:LOCK
move /-Y C:\Users\MyName\Desktop\SecretCodes C:\Users\MyName\AppData\Roaming\Hidden
attrib +H +S C:\Users\MyName\AppData\Roaming\Hidden\SecretCodes
echo Secret is locked!! o7
pause
goto END

:UNLOCK
title Unlock
echo Please enter the password!
set /p "pass=>" 
if NOT %pass%== passwordispassword goto FAIL
move /-Y C:\Users\MyName\AppData\Roaming\Hidden\SecretCodes C:\Users\MyName\Desktop
echo the secret has been revealed!
pause
goto END

:FAIL
title Unlock
set /p "pass=>"
if NOT %pass%== passwordispassword goto END 
move /-Y C:\Users\MyName\AppData\Roaming\Hidden\SecretCodes C:\Users\MyName\Desktop
echo the secret has been revealed!
goto END

:CONFIRMATION
echo are you sure you want to Unlock the secrets?(Y/N)
set /p "choo=>"
if %choo%==Y goto UNHIDE
if %choo%==y goto UNHIDE
if %choo%==n goto END
if %choo%==N goto END
echo excuse me?
goto CONFIRMATION

:UNHIDE
attrib -H -S C:\Users\MyName\AppData\Roaming\Hidden\SecretCodes
goto UNLOCK

to makes things more clearer, in the codes that i write :

:CONFIRM 
echo are you sure you want to Lock the stuff?(Y/N) 
set /p "cho=>" 
if %cho%==Y go to LOCK 
if %cho%==y goto LOCK 
if %cho%==n goto END 
if %cho%==N goto END 
echo excuse me? 
goto CONFIRM 

, will make me repeat the command if i put the wrong input for example i type E and then enter. It will say "excuse me?" and then asking again "are you sure you want to Lock the stuff?(Y/N)", the problem that i'm getting is when i put nothing (press Enter immediately...). when i put nothing into it, the cmd will close.

So going back to question number 1, is there a way to prevent cmd closing from pressing the enter button and not put any password on the input?

1 Answers1

1

This optimized batch file code should work as expected by you.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
if not exist "%USERPROFILE%\Desktop\SecretCodes" goto PromptUnlock

:PromptLock
title Lock the stuff
%SystemRoot%\System32\choice.exe /C YN /N /M "Are you sure you want to LOCK the stuff? (Y/N)"
if errorlevel 2 goto EndBatch

move /-Y "%USERPROFILE%\Desktop\SecretCodes" "%APPDATA%\Hidden"
%SystemRoot%\System32\attrib.exe +H +S "%APPDATA%\Hidden\SecretCodes"
echo/
echo Secret is locked!! o7
echo/
pause
goto EndBatch

:PromptUnlock
title Unlock the stuff
%SystemRoot%\System32\choice.exe /C YN /N /M "Are you sure you want to UNLOCK the stuff? (Y/N)"
if errorlevel 2 goto EndBatch

%SystemRoot%\System32\attrib.exe -H -S "%APPDATA%\Hidden\SecretCodes"

:PromptPassword
set "pass="
set /P "pass=Please enter the password: "
rem Has the user entered a string at all?
if not defined pass goto PromptPassword
rem Remove all double quotes from entered password string for a safe
rem string comparison below. Password cannot contain character " at all.
set "pass=%pass:"=%"
if not "%pass%" == "passwordispassword" goto PromptPassword

move /-Y "%APPDATA%\Hidden\SecretCodes" "%USERPROFILE%\Desktop"
echo/
echo The secret has been revealed!
echo/
pause

:EndBatch
endlocal

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • choice /?
  • echo /?
  • endlocal /?
  • goto /?
  • if /?
  • move /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?
  • title /?

See also: How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?

Mofi
  • 46,139
  • 17
  • 80
  • 143