Page 1 of 1

Folder "lock" not working

Posted: 04 Dec 2015 13:08
by batchcc
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

Re: Folder "lock" not working

Posted: 04 Dec 2015 20:47
by ShadowThief
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.

Re: Folder "lock" not working

Posted: 05 Dec 2015 10:03
by batchcc
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

Re: Folder "lock" not working

Posted: 05 Dec 2015 12:28
by Squashman
I bet if you turned echo on and ran the batch file from the cmd prompt you would see the problem.

Re: Folder "lock" not working

Posted: 06 Dec 2015 09:16
by batchcc

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>

Re: Folder "lock" not working

Posted: 06 Dec 2015 10:25
by einstein1969
the function PasswordInput is wrong!

look this

Einstein1969

Re: Folder "lock" not working

Posted: 06 Dec 2015 11:12
by batchcc
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

Re: Folder "lock" not working

Posted: 06 Dec 2015 14:21
by Aacini
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