443Views7Replies

Author Options:

use an attribute as a variable in batch? Answered

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?

Discussions

0
None
orksecurity

7 years ago

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

0
None
g-oneorksecurity

Answer 7 years ago


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.)?

0
None
orksecurityg-one

Answer 7 years ago

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.

0
None
g-oneorksecurity

Answer 7 years ago

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?

0
None
orksecurityg-one

Answer 7 years ago

Wasn't posting mine as an alternative, just as confirmation. Apologies for the confusion.

0
None
g-oneorksecurity

Answer 7 years ago

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 ...

0
None
XOIIOorksecurity

Answer 7 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.