Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
batchcc
- Posts: 139
- Joined: 17 Aug 2015 06:05
-
Contact:
#1
Post
by batchcc » 04 Dec 2015 13:08
What am i doing wrong with this "folder lock" it doesn't work it just closes after I type to enter a password.
Code: Select all
goto folder
:unlock
@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set /P "=Enter password to unlock folder:" < Nul
Call :PasswordInput
set !Line! =pass
if NOT %pass%==password goto FAIL
attrib -h -s "HTG Locker"
ren "HTG Locker" Private
echo Folder Unlocked successfully
goto End
Goto :Eof
:PasswordInput
::Author: Carlos Montiers Aguilera
::Last updated: 20150401. Created: 20150401.
::Set in variable Line a input password
For /F skip^=1^ delims^=^ eol^= %%# in (
'"Echo(|Replace.exe "%~f0" . /U /W"') Do Set "CR=%%#"
For /F %%# In (
'"Prompt $H &For %%_ In (_) Do Rem"') Do Set "BS=%%#"
Set "Line="
:_PasswordInput_Kbd
Set "CHR=" & For /F skip^=1^ delims^=^ eol^= %%# in (
'Replace.exe "%~f0" . /U /W') Do Set "CHR=%%#"
If !CHR!==!CR! Echo(&Goto :Eof
If !CHR!==!BS! (If Defined Line (Set /P "=!BS! !BS!" Set "Line=!Line:~0,-1!"
)
) Else (Set /P "=*" If !CHR!==! (Set "Line=!Line!^!"
) Else Set "Line=!Line!!CHR!"
)
Goto :_PasswordInput_Kbd
:folder
@echo off
cls
title Folder Private
if EXIST "HTG Locker" goto UNLOCK
if NOT EXIST Private goto MDLOCKER
:CONFIRM
echo Are you sure you want to lock the folder(Y/N)
set/p "cho=>"
if /i %cho%==Y goto LOCK
if /i %cho%==n goto END
echo Invalid choice.
goto CONFIRM
:LOCK
ren Private "HTG Locker"
attrib +h +s "HTG Locker"
echo Folder locked
goto End
:FAIL
echo Invalid password
goto end
:MDLOCKER
md Private
echo Private created successfully
goto End
:End
-
ShadowThief
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
#2
Post
by ShadowThief » 04 Dec 2015 20:47
I don't know where you got that PasswordInput code, but it looks like the formatting got messed up when you copied it, because there's absolutely no way that it's correct as written.
-
batchcc
- Posts: 139
- Joined: 17 Aug 2015 06:05
-
Contact:
#3
Post
by batchcc » 05 Dec 2015 10:03
Here is the code I have
Code: Select all
@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set /P "=Enter a Password:" < Nul
Call :PasswordInput
Echo(Your input was:!Line!
Goto :Eof
:PasswordInput
::Author: Carlos Montiers Aguilera
::Last updated: 20150401. Created: 20150401.
::Set in variable Line a input password
For /F skip^=1^ delims^=^ eol^= %%# in (
'"Echo(|Replace.exe "%~f0" . /U /W"') Do Set "CR=%%#"
For /F %%# In (
'"Prompt $H &For %%_ In (_) Do Rem"') Do Set "BS=%%#"
Set "Line="
:_PasswordInput_Kbd
Set "CHR=" & For /F skip^=1^ delims^=^ eol^= %%# in (
'Replace.exe "%~f0" . /U /W') Do Set "CHR=%%#"
If !CHR!==!CR! Echo(&Goto :Eof
If !CHR!==!BS! (If Defined Line (Set /P "=!BS! !BS!" Set "Line=!Line:~0,-1!"
)
) Else (Set /P "=*" If !CHR!==! (Set "Line=!Line!^!"
) Else Set "Line=!Line!!CHR!"
)
Goto :_PasswordInput_Kbd
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#4
Post
by Squashman » 05 Dec 2015 12:28
I bet if you turned echo on and ran the batch file from the cmd prompt you would see the problem.
-
batchcc
- Posts: 139
- Joined: 17 Aug 2015 06:05
-
Contact:
#5
Post
by batchcc » 06 Dec 2015 09:16
Code: Select all
C:\Users\User>title Folder Private
C:\Users\User>if EXIST "HTG Locker" goto UNLOCK
C:\Users\User>SetLocal EnableExtensions EnableDelayedExpansion
C:\Users\User>Set /P "=Enter password to unlock folder:" 0<Nul
Enter password to unlock folder:
C:\Users\User>Call :PasswordInput
C:\Users\User>For /F skip=1 delims= eol= %# in ('"Echo(|Replace.exe "C:\Users\User\fl.bat" . /U /W"') Do Set "CR=%#"
" \Users\User>Set "CR=
C:\Users\User>For /F %# In ('"Prompt $H &For %_ In (_) Do Rem"') Do Set "BS=%#"
C:\Users\User>Set "BS"
C:\Users\User>Set "Line="
C:\Users\User>Set "CHR=" & For /F skip=1 delims= eol= %# in ('Replace.exe "C:\Users\User\fl.bat" . /U /W') Do Set "CHR=%#"
C:\Users\User>Set "CHR=1"
C:\Users\User>If !CHR! == !CR! Echo( & Goto :Eof
Else was unexpected at this time.
C:\Users\User>) Else Set "Line=!Line!!CHR!"
C:\Users\User>
-
batchcc
- Posts: 139
- Joined: 17 Aug 2015 06:05
-
Contact:
#7
Post
by batchcc » 06 Dec 2015 11:12
Is this better I haven't tried it yet
Code: Select all
goto folder
:unlock
@Echo Off
SetLocal EnableExtensions EnableDelayedExpansion
Set /P "=Enter a Password:" < Nul
Call :PasswordInput
set !pwd! =pass
if NOT %pass%==password goto FAIL
attrib -h -s "HTG Locker"
ren "HTG Locker" Private
echo Folder Unlocked successfully
goto End
Goto :Eof
:PasswordInput
:: consolesoft.com/batch/?#password_input
::Author: Carlos Montiers Aguilera
::Last updated: 20150405. Created: 20150401.
::Set variable pwd with input password
::Needs delayed expansion enabled
Set "pwd="
Set "INTRO=" &For /F "skip=1" %%# in (
'"Echo(|Replace.exe ? . /U /W"'
) Do If Not Defined INTRO Set "INTRO=%%#"
For /F %%# In (
'"Prompt $H &For %%_ In (_) Do Rem"') Do Set "BKSPACE=%%#"
:_PasswordInput_Kbd
Set "Excl="
Set "CHR=" &For /F skip^=1^ delims^=^ eol^= %%# in (
'Replace.exe ? . /U /W') Do If Not Defined CHR (
Set "CHR=%%#" &If "%%#"=="!" Set "Excl=yes")
If "!INTRO!"=="!CHR!" Echo(&Goto :Eof
If "!BKSPACE!"=="!CHR!" (If Defined pwd (
Set "pwd=!pwd:~0,-1!"
Set /P "=!BKSPACE! !BKSPACE!" <Nul)
Goto :_PasswordInput_Kbd
) Else If Not Defined Excl (
Set "pwd=!pwd!!CHR:~0,1!"
) Else Set "pwd=!pwd!^!"
Set /P "=*" <Nul
Goto :_PasswordInput_Kbd
:folder
@echo off
cls
title Folder Private
if EXIST "HTG Locker" goto UNLOCK
if NOT EXIST Private goto MDLOCKER
:CONFIRM
echo Are you sure you want to lock the folder(Y/N)
set/p "cho=>"
if /i %cho%==Y goto LOCK
if /i %cho%==n goto END
echo Invalid choice.
goto CONFIRM
:LOCK
ren Private "HTG Locker"
attrib +h +s "HTG Locker"
echo Folder locked
goto End
:FAIL
echo Invalid password
goto end
:MDLOCKER
md Private
echo Private created successfully
goto End
:end
-
Aacini
- Expert
- Posts: 1913
- Joined: 06 Dec 2011 22:15
- Location: México City, México
-
Contact:
#8
Post
by Aacini » 06 Dec 2015 14:21
You may get
this ReadLine subroutine and modify it in order to read a password. For example:
Code: Select all
@echo off
setlocal
call :ReadPassword pass="Enter password to unlock folder: "
echo Password read: "%pass%"
goto :EOF
:ReadPassword var="prompt"
rem Read a password
rem Antonio Perez Ayala
rem Initialize variables
setlocal EnableDelayedExpansion
echo > _
for /F %%a in ('copy /Z _ NUL') do set "CR=%%a"
for /F %%a in ('echo prompt $H ^| cmd') do set "BS=%%a"
rem Show the prompt and start reading
set /P "=%~2" < NUL
set "input="
set i=0
:nextKey
set "key="
for /F "delims=" %%a in ('xcopy /W _ _ 2^>NUL') do if not defined key set "key=%%a"
rem If key is CR: terminate input
if "!key:~-1!" equ "!CR!" goto endRead
rem If key is BS: delete last char, if any
set "key=!key:~-1!"
if "!key!" equ "!BS!" (
if %i% gtr 0 (
set /P "=!BS! !BS!" < NUL
set "input=%input:~0,-1%"
set /A i-=1
)
goto nextKey
)
rem Else: show and accept the key
set /P "=*" < NUL
set "input=%input%%key%"
set /A i+=1
goto nextKey
:endRead
echo/
del _
endlocal & set "%~1=%input%"
exit /B
Antonio