-5

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

lit
  • 14,456
  • 10
  • 65
  • 119
Kero4000
  • 3
  • 5
  • 1
    `ocker folder.bat`? I'm having trouble understanding what you're saying and just what you are trying to accomplish. A change doesn't seem much like a test to begin with but, what have you tried and how did it fail? Please update your question to include the code you've tried, an explanation of what it does and how that differs from what you expected. – notjustme Jan 28 '20 at 11:11
  • i update the question thanks for your time – Kero4000 Jan 28 '20 at 11:31
  • As far as I'm aware, you don't need to check the current attribute state, simply adding those you need should be sufficient. – Compo Jan 28 '20 at 13:03
  • Dear Compo , i cant understand your solution – Kero4000 Jan 28 '20 at 13:21
  • Just using `Attrib -H -S "MTD"` is necessary, you do not need to determine what the existing attributes are, before setting your required attributes. If the target is not already hidden and/or system, then nothing will happen, if it is then the change will be performed. Please note however, that you may need to be running the command with appropriate privileges too. – Compo Jan 28 '20 at 13:59
  • Thanks Mr. Compo for your answer , the main core for determine the existing attribute that this is just part of a full batch so i need to check if the attributes = ... then do ..... else do ..... thanks – Kero4000 Jan 28 '20 at 14:53
  • @Kero4000 - Are you still working on this issue? – lit Feb 05 '20 at 02:28

2 Answers2

1

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'
    }
lit
  • 14,456
  • 10
  • 65
  • 119
  • lit, `/A:H /A:S` is exactly the same as `/A:S`, the `Dir` command uses only the last `/A` option if there are multiple. Additionally, I thought it may be worth mentioning that `DIR /B /A:HS` will output all files and directories matching both attributes, but I'm not sure from the question if those with both attributes are to be determined, or those with either or both. The OP appears to be looking for directory attributes too, so if they're looking for only directories, with both hidden and system attributes, I'd suggest `('DIR /A:DHS /B 2^>NUL')`. – Compo Jan 29 '20 at 00:51
  • Technically you wouldn't do it as a single command, you'd create it, then set its attributes. – Compo Jan 29 '20 at 15:46
  • i can make dir code and then change attributes but what if its exist ? – Kero4000 Jan 29 '20 at 16:09
  • Where are you creating it, @lit? `MD "%UserProfile%\Desktop\MTD" 2>NUL` then `Attrib +S +H "%UserProfile%\Desktop\MTD"` should be all that you require. To verify you should be able to use, `Attrib "%UserProfile%\Desktop\MTD"`, which hopefully will output `SH` among the attribute sequence. Then I suppose, that would answer the question too! `Attrib "MTD" 2>NUL|Find "SH">NUL&&Attrib -S -H "MTD"`. Depending upon the filename, you may need to adjust the actual match string, e.g. `"SH--"`, or choose `findstr` instead. – Compo Jan 29 '20 at 20:19
0

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
Kero4000
  • 3
  • 5