Testing Folder Attributes using .bat - batch-file

iam using .bat code to check folder attribute if its ( Hidden + System ) then change it to ( Not Hidden + Not System )
"MTD" is Folder
Code:
if attrib +h +s "MTD" attrib -h -s "MTD"
Thanks

REWRITE With thanks to #Compo and a post from dbenham (https://stackoverflow.com/a/8669636/447901), this is completely rewritten. It is -very- hardcoded for specific character positions which is not a good idea.
When it appears that the correct ATTRIB commands will be run on the correct directories, remove the echo from the ATTRIB command.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "delims=" %%A IN ('DIR /B /A:H') DO (
FOR /F "delims=" %%B in ("%%~aA") DO (
SET ATTRS=%%~B
if "!ATTRS:~0,1!" == "d" if "!ATTRS:~3,2!" == "hs" (
echo ATTRIB -H -S %%~A
)
)
)
This would be far better done in PowerShell. This will require the current PowerShell 5.x or higher. In fact...
Get-ChildItem -Directory -Hidden -System |
ForEach-Object {
$_.Attributes -= 'Hidden'
$_.Attributes -= 'System'
}

Hi All i found solution
cls
#ECHO OFF
title Folder Locker
:CONFIRM
echo Are you sure u want to Lock the folder(Y/N)
set/p "cho=>"
if %cho%==Y goto LOCK
if %cho%==y goto LOCK
if %cho%==n goto UNLOCK
if %cho%==N goto UNLOCK
echo Invalid choice.
goto CONFIRM
:LOCK
attrib +h +s "MTD"
echo Folder locked
goto End
:UNLOCK
echo Enter password to Unlock folder
set/p "pass=>"
if NOT %pass%== 123 goto FAIL
attrib -h -s "MTD"
echo Folder Unlocked successfully
goto End
:FAIL
echo Invalid password
goto end
:End

Related

Access is denied creating text file in cmd & weird "if exist" problem

For a batch file project of mine, I had to create two text files if they didn't already exist. For some reason the 'if exist "example.txt"' didn't work. What I mean by that is:
if not exist "example.txt" goto :label
wouldn't go to 'label' when the file did not exist. So, i presumed the file must be existing already (from my previous tests). The thing is that
if exist "example.txt" goto :label2
always went to 'label2', even though later in the script cmd could not find the file, and the folder is empty except the batch script (the file didn't exist). I decided I'd test the label for if the file didn't exist anyway. when I used the break> command to create the text file, it came back as access is denied. It did the same thing with the following code which i used to enter information:
>> "example.txt" echo 0
and with
del/f "example.txt"
I am a full administrator on my computer, the code I use works in other batch files I have created. I have tried running cmd in administrator (same outcome). Specifying the directory works no better in this case than using current directory. I'm wondering what is the cause of this is.
Anyway, here is the full batch script.
#echo off
title Folder Safe
color e8
if exist "infocj.txt" goto start
goto MAKESOURCE
:MAKESOURCE
rem notes: access is denied. this means that createproc doesn't work (createproc relies on makesource).
break>"infocj.txt"
attrib +s +h +r "infocj.txt"
break>"setupcj.txt"
>> "setupcj.txt" echo 0
attrib +s +h +r "setupcj.txt"
goto START
:START
echo what safe do you want to unlock (number only)
set/p "loc=>"
if exist %loc% goto LOCK
if exist Locked%loc% goto UNLOCK
echo there is no safe hashing to %loc%. do you wish to create a new safe? (y/n)
set/p "cho=>"
if %cho%==y goto createpass
exit
:LOCK
cls
For /f "tokens=%loc% delims=;" %%i in (infocj.txt ) do set pass=%%i
echo enter password to lock Safe %loc%
set/p "qaz=>"
if not %qaz%==%pass% goto END
ren %loc% Locked%loc%
attrib/d +h +s +r Locked%loc%
exit
:UNLOCK
cls
For /f "tokens=%loc% delims=;" %%i in (infocj.txt ) do set pass=%%i
echo enter password to unlock Safe %loc%:
set/p "qaz=>"
if not %qaz%==%pass% goto END
ren Locked%loc% %loc%
attrib/d -s -h -r %loc%
exit
:createpass
echo enter password:
set/p "pass=>"
cls
echo confirm password:
set/p "npass=>"
cls
if not %pass%==%npass% (
echo the passwords you entered are not matching
timeout /t 2 /nobreak > NUL
goto createpass
)
goto createproc
:createproc
rem this stuff is screwed because the stuff under makesource is not working properly, but even if I do all that manually, a lot of this still returns access is denied
for /f "tokens=* usebackq" %%a in ("infocj.txt") do set inf=%%a
set inf=%inf%%pass%;
attrib -s -h -r "infocj.txt"
del/f "infocj.txt"
break>"infocj.txt"
>> "infocj.txt" echo %inf%
attrib +s +h +r "infocj.txt"
for /f "delims=" %%b in ("setupcj.txt") do set stp=%%b+1
md %stp%
attrib -s -h -r "setupcj.txt"
del/f "setupcj.txt"
break>"setupcj.txt"
>> "setupcj.txt" echo %stp%
attrib +s +h +r "setupcj.txt"
echo your locker hashes to the number %stp%
pause
exit
Thanks in advance for your answers. This one's got me truly stumped.

.bat folder lock: how to change password inside Cmd

cls
#ECHO OFF
title Folder posnetki
if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto UNLOCK
if NOT EXIST posnetki goto MDLOCKER
:CONFIRM
echo Are you sure you want to lock the folder(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 Invalid choice.
goto CONFIRM
:LOCK
ren posnetki "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
echo Folder locked
goto End
:UNLOCK
echo Enter password to unlock folder
set/p "pass=>"
if NOT %pass%== pass123 goto FAIL
attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" posnetki
echo Folder Unlocked successfully
goto End
:FAIL
echo Invalid password
goto end
:MDLOCKER
md posnetki
echo posnetki created successfully
goto End
:End
How to make password change from cmd not from code? Here I really need your help because I dont want everytime when I want to change password I need to open it by txt.
I am not going to focus on your code at all, I am simply going to demonstrate what you asked as the main question, that being "how to change password via script".
Writing password to a file method:
#echo off
cls
:start
if not exist "%temp%\tmppwd.lck" (
echo password file does not yet exist Please create a Password.
goto chpwd
)
Choice /C TC /M "Select U to unlock T to test password"
if %errorlevel%==2 goto chpwd
if %errorlevel%==1 goto checkpass
:chpwd
set /p "passwd=Enter your new password and press Enter: "
set /p "passwdc=Confirm new password: "
if "%passwd%"=="%passwdc%" (
echo %passwd% > %temp%\tmppwd.lck
goto start
) else (
cls
echo Sorry, Passwords did not match, please retry
goto chpwd
)
:checkpass
for /f %%i in ('type "%temp%\tmppwd.lck"') do set "test=%%i"
set /p "attempt=Enter password to see if this works: "
if "%attempt%"=="%test%" (
echo Passwords Match & pause
) else (
echo Sorry, you entered the incorrect password
)
So as you can see, we create a new password, verify it and then write it to a password file. If the file does not exist, we simply do the exact same.
The :checkpass label demonstrates how you can use the password from the file. So where you currently use if NOT %pass%== pass123 goto FAIL we use the loop to read from file.

how to mask password in bat

I have this code snippet from a Stack Overflow question about masking input in a batch script.
set "psCommand=powershell -Command "$pword = read-host 'Enter Password' -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%p
echo %password%
And here is my script.
cls
#ECHO OFF
title Folder Locker
if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto UNLOCK
if NOT EXIST Locker goto MDLOCKER
:CONFIRM
echo Are you sure u want to Lock the folder(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 Invalid choice.
goto CONFIRM
:LOCK
ren Locker "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
echo Folder locked
goto End
:UNLOCK
echo Enter password to Unlock folder
set/p "pass=>"
if NOT %pass%==Your-Password-Here goto FAIL
attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" Locker
echo Folder Unlocked successfully
goto End
:FAIL
echo Invalid password
goto end
:MDLOCKER
md Locker
echo Locker created successfully
goto End
:End
Now, I want to implement the code snippet I showed earlier into my own script, where should I put the snippet into the script? Should I remove some code or not?

instead of not printing anything batch file prints echo is off

I have a very little problem, which gets on my nerves. I wrote a program for locking folders and I gave it the option to change the password as well as the tipp, however, if no tipp is typed, I want it to just print nothing. Instead it says "echo is off"
I guess it's the syntax, but I do not know where. Can someone double check it for me. thank you!
#Echo Off
setlocal EnableDelayedExpansion
mode con cols=80 lines=25
set /a "tries=3"
color 5F
title Folder Locker by KBKOZLEV
:SETPASS
set "tipp="
set "password="
if exist "password.txt" (
set /p password=<password.txt
attrib +h +s "password.txt"
)
if exist "tipp.txt" (
set /p tipp=<tipp.txt
attrib +h +s "tipp.txt"
)
:START
if exist "Locked" goto :OPEN
if exist "Unlocked" goto :LOCK
if not exist "Unlocked" goto :MDLOCKER
:LOCK
ren "Unlocked" "Locked"
attrib +h +s "Locked"
echo.
echo Folder locked.
CHOICE /C X /T 1 /D X > nul
goto :END
exit
:MDLOCKER
md "Unlocked"
echo>password.txt 1234
echo>tipp.txt 1234
attrib +h +s "password.txt"
attrib +h +s "tipp.txt"
cls
echo.
echo Private folder created successfully.
CHOICE /C X /T 1 /D X > nul
goto :END
:OPEN
color 2F
cls
echo ********************************************************************************
echo Folder Locker by KBKOZLEV v.01
echo.
echo ********************************************************************************
echo ---- Enter password to unlock folder, or enter "new" to set a new password. ----
echo You have %tries% attempts left.
echo --------------------------------------------------------------------------------
echo.
echo Password tipp: %tipp%
echo.
set "pass="
Set /P "=Password:" < Nul
set "psCommand=powershell -Command "$pword = read-host -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set pass=%%p
if /i "%pass%"=="new" goto :NEWPASS
if "%pass%"=="%password%" (
attrib -h -s "Locked"
ren "Locked" "Unlocked"
echo.
echo Folder unlocked successfully.
goto :END
)
set /a tries=%tries -1
if %tries%==0 (
goto :FAIL2
)
goto :FAIL
:FAIL
color 4F
cls
echo.
echo Invalid password, please try again.
CHOICE /C X /T 1 /D X > nul
cls
goto :OPEN
:FAIL2
color 4F
cls
echo.
echo Invalid password, program will now close.
CHOICE /C X /T 2 /D X > nul
cls
goto :END
:NEWPASS
color 8F
cls
echo.
set "oldpass="
Set /P "=Old Password:" < Nul
set "psCommand=powershell -Command "$pword = read-host -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set oldpass=%%p
if not "%oldpass%"=="%password%" goto :FAIL
:ENTERNEW
color 8F
cls
echo.
set "newpass=""
Set /P "=New Password:" < Nul
set "psCommand=powershell -Command "$pword = read-host -AsSecureString ; ^
$BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set newpass=%%p
set newpass=%newpass:"=%
if "%newpass%"=="" (
echo.
echo Invalid new password, please enter new password again.
CHOICE /C X /T 1 /D X > nul
goto :ENTERNEW
)
if exist "password.txt" attrib -h -s "password.txt"
echo>password.txt %newpass%
echo.
set "passtipp=""
set /p "passtipp=New tipp:"
set passtipp=%passtipp:"=%
if exist "tipp.txt" attrib -h -s "tipp.txt"
if not "%passtipp%"=="" (
echo>tipp.txt %passtipp%
) else (
del "tipp.txt"
)
goto :SETPASS
:END
color
EndLocal
exit
The only approach:
if "%passtipp%"=="" (echo(>tipp.txt) else (echo>tipp.txt %passtipp%)
Explanation: see all my comments to other answers (for the present), please...
To suppress the "ECHO is off" text when echoing a blank line, change echo to echo. with a trailing dot. Your script already does this for blank lines to the console. The same thing works for redirecting to files.
For example, this line will put "ECHO is off" into tipp.txt if the %passtip% variable is empty:
echo>tipp.txt %passtipp%
This line will cause tipp.txt to be empty:
>tipp.txt echo.%passtipp%
It's usually safe to append all instances of echo with trailing punctuation. The form echo. can be problem if there is a file named "echo" in the path; In that case, a safer alternative is echo(.
The root cause of your issue is that your syntax for redirecting data to a file is wrong. When echo is run by itself, it outputs whether echo is ON or OFF. So when you have lines like echo>password.txt 1234, you are saying "Take the output of echo and redirect it to password.txt 1234".
What you should be doing is echo 1234>password.txt, which will redirect the string 1234 to password.txt.
And then do the same thing for tipp.txt.
Also, I feel the need to point out that the word you're looking for is "tip," not "tipp."

fix variable = variable in a batch file for a password

in this batch file i am trying to created a program where the user can enter his or her desired password and use it when ever they open or close the Batch file.
the problem is that i can not get the user password and (at :MDLOCKER and :UNLOCK) and the unlock part of the script to work. when finally get it to work it accepts any password
if you could help that would be great thanks.
enter code here#ECHO OFF
prompt Filelocker`enter code here`
:START
echo what do you want to do? (insert number)
echo 1 Lock current folder
echo 2 Unlock current folder
echo 3 Make new locked folder
set/p "cho=>"
if %cho%==1 goto CONFIRM
if %cho%==2 goto UNLOCK
if %cho%==3 goto NEW
echo not valid
goto start
: NEW
title Folder Locked files
if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" goto ALREADY
if NOT EXIST Locker goto MDLOCKER
:ALREADY
echo folder already exist!
echo try unlocking if folder can not be found
pause
goto START
:CONFIRM
echo Are you sure u want to Lock the folder(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 Invalid choice.
goto CONFIRM
:LOCK
ren Locker "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
echo Folder locked
goto end
:MDLOCKER
md Locker
echo Locked folder created....
echo folder is now created
echo enter password for your file.
set/p 1%=
echo password accepted
goto start
:UNLOCK
echo Enter password to Unlock folder
set/p "pass=>"
if NOT %pass%==%1% goto FAIL
attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" Locker
echo Folder Unlocked successfully
goto End
:FAIL
echo Invalid password
echo 2 MORE TRYS LEFT
pause
goto
:End
I don't think you can change the positional parameters %1 .. %9. Try saving your password into a variable instead.
the problem is here:
set/p 1%=
Why are you doing that?
You can't use a special var like a var-name.
And you can't use numbers at the start of a var-name.
and you can use the command Choice instead of all the IF statements that you are using:
#ECHO OFF
prompt Filelocker`enter code here`
:START
echo what do you want to do? (insert number)
echo 1 Lock current folder
echo 2 Unlock current folder
echo 3 Make new locked folder
choice /C 123 /M "choose an option: "
IF %ERRORLEVEL% EQU 1 (goto :CONFIRM)
IF %ERRORLEVEL% EQU 2 (goto :UNLOCK)
IF %ERRORLEVEL% EQU 3 (goto :NEW)
:NEW
title Folder Locked files
if EXIST "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" (
echo folder already exist!
echo try unlocking if folder can not be found
pause
goto :START
)
if NOT EXIST "Locker" (goto :MDLOCKER)
:CONFIRM
choice /C YN /M "Are you sure u want to Lock the folder? "
IF %ERRORLEVEL% EQU 1 (goto :LOCK)
IF %ERRORLEVEL% EQU 2 (goto :END)
:LOCK
ren Locker "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
attrib +h +s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
echo Folder locked
goto :end
:MDLOCKER
md "Locker"
echo Locked folder created....
echo folder is now created
echo enter password for your file.
set/p "VAR=>> "
echo password accepted
goto :start
:UNLOCK
echo Enter password to Unlock folder
set/p "pass=>> "
if NOT "%pass%"=="%VAR%" goto FAIL
attrib -h -s "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}"
ren "Control Panel.{21EC2020-3AEA-1069-A2DD-08002B30309D}" Locker
echo Folder Unlocked successfully
goto End
:FAIL
echo Invalid password
echo 2 MORE TRYS LEFT
pause
goto :UNLOCK
:End

Resources