424Views7Replies

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?

7 Replies

user
orksecurity (author)2011-02-05

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

Select as Best AnswerUndo Best Answer

user
g-one (author)orksecurity2011-02-05


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

Select as Best AnswerUndo Best Answer

user
orksecurity (author)g-one2011-02-05

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.

Select as Best AnswerUndo Best Answer

user
g-one (author)orksecurity2011-02-05

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?

Select as Best AnswerUndo Best Answer

user
orksecurity (author)g-one2011-02-05

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

Select as Best AnswerUndo Best Answer

user
g-one (author)orksecurity2011-02-05

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

Select as Best AnswerUndo Best Answer

user
XOIIO (author)orksecurity2011-02-05

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.

Select as Best AnswerUndo Best Answer