Testing Folder Attributes using .bat

748 views Asked by At

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

2

There are 2 answers

4
lit On

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'
    }
0
Kero4000 On

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