Instructables

use an attribute as a variable in batch?

How can I use a file attribute as a variable in batch, something like this?

IF 'attrib'=='+h' C:\Users\XOIIO\Desktop\foldername (echo yes) ELSE (echo no)

is there any way to do this?


orksecurity3 years ago
Not easily, no. Consider learning a real programming language... or at least a more powerful convenience language.

Well, orksecurity it's really easy. Option ~a for a FOR variable returns all attributes

@echo off &setlocal
for /f "delims=" %%i in ('dir /a /b "%userprofile%\desktop" ^|findstr /ixc:"foldername"') do (
echo %%~ai | find /i "h" >nul &&(
echo yes
)||(
echo no
)
)
pause


BTW Is there a way to post code as is (with indented lines etc.)?

Hmmm. I stand corrected. Tried a slightly simplified version of your code on my XT box (just looking at current directory), and it does seem to respond "yes" and "no" in somewhat appropriate manner.

@echo off &setlocal
for /f "delims=" %%i in ('dir /a /b') do (
  echo %%~ai | find /i "h" >nul &&(
    echo yes
  )||(
    echo no
  )
)
pause

Thanks; apparently I'm somewhat out of date on the odder corners of the command shell.
Your code iterates over the current directory and returns "yes" or "no" for each subfolder or file. I'm not sure what XOIIO would need. I thought the intension was to return "yes" or "no" only for the "foldername" subdirectory on his desktop. But I don't understand what this has to do with the topic "use an attribute as a variable in batch". There is no variable but %%i ...
@XOIIO, what next? Seems to be only one step. What do you do with the returned information?
Wasn't posting mine as an alternative, just as confirmation. Apologies for the confusion.
Your code wasn't confusing ;-)
I only tried to clarify the difference between yours and mine because I don't know exactly what XOIIO is looking for. The topic of this thread is confusing in this context ...
XOIIO (author)  orksecurity3 years ago
I am working on python, that way I can make GUI's, but there doesn't seem to be a windows 7-happy version yet. I need to check again.
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!