4

When creating a Post-Build event in VS2008, I want to check if the target for an xcopy operation is not readonly. I found some code ( http://forums.techguy.org/dos-other/842311-solved-checking-if-file-folder.html ) that makes use of a temporary variable and later checks that for certain attribs. It does not work. When I manually print the variable, it seems to be empty.

if $(PlatformName)==x86 (
  echo x86
  For /F "Delims=" %%I In ('Attrib $(ProjectDir)..\..\somedir\$(ConfigurationName)    \somemoredir\$(TargetName).dll') Do Set _Attribs=%%I
  If NOT "%_Attribs:~5,1%"=="R" (
    set test="monkey"
    echo %test%
    echo $(test)
    echo nono
    echo %_Attribs%
    echo $(ProjectDir)..\..\somedir\$(ConfigurationName)\somemoredir\$(TargetName).dll
    attrib $(ProjectDir)..\..\somedir\$(ConfigurationName)\somemoredir\$(TargetName).dll
    xcopy /Y $(ProjectDir)..\..\..\Runtime\bin\$(TargetName).*     $(ProjectDir)..\..\somedir\$(ConfigurationName)\somemoredir\
  )

)

output is:

x86 ECHO is on. ECHO is on. nono ECHO is on.

then the attrib message and so on.

echo %test%, echo $(test) and so on seem to show that the test variable does not contain anything.

How can I use environment variables?

(BTW, _Attribs, which I am really interested in, also does not contain anything.)

Andreas Reiff
  • 7,961
  • 10
  • 50
  • 104

1 Answers1

12

You have a classic problem for batch newbies - You can't look at a value you just set within a loop (parentheses) because the entire loop is parsed at once. So you are seeing the value of your variable before the loop was executed - obviously not going to work. The solution is to enable delayed expansion with SETLOCAL EnableDelayedExpansion and then expand your variable using delayed expansion as !var!.

Your other issue of how to get the file attributes - you can use the ~a FOR variable expansion modifier to get the list of attributes. In typical Microsoft fashion, the attributes are not listed in the same order or case as the ATTRIBUTES command. The read only flag is r in the 2nd character (position 1).

Both delayed expansion and modifiers are explained in the documentation: Simply type HELP FOR or FOR /? from a Windows command line. Simply run CMD from the Start menu to get a command line.

setlocal enableDelayedExpansion
if $(PlatformName)==x86 (
  echo x86
  for %%F In ("Attrib $(ProjectDir)..\..\somedir\$(ConfigurationName)\somemoredir\$(TargetName).dll") do set _Attribs=%%~aF
  if not "!_Attribs:~1,1!"=="r" (
    set test="monkey"
    echo !test!
    echo $(test)
    echo nono
    echo !_Attribs!
    echo $(ProjectDir)..\..\somedir\$(ConfigurationName)\somemoredir\$(TargetName).dll
    attrib $(ProjectDir)..\..\somedir\$(ConfigurationName)\somemoredir\$(TargetName).dll
    xcopy /Y $(ProjectDir)..\..\..\Runtime\bin\$(TargetName).*  $(ProjectDir)..\..\somedir\$(ConfigurationName)\somemoredir\
  )
)

You could use the ATTRIB command as in your original command - just make sure to use !_Attribs:~5,1! instead of %_Attribs:~5,1%. But the ~a modifier solution I provided is more efficient since it uses a simple FOR loop and doesn't have to execute a command using FOR /F.

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Thx a lot from all newbies and from me! I found some minor problems - maybe on my side?? - with the brackets and also with a ~ missing. Below version works as hoped for. I will add another reply since I cannot format this here for the life of me. – Andreas Reiff Jun 26 '12 at 13:47
  • @Andreas - Indeed you did spot a missing `~`. I've updated the answer, thanks. I'm not sure what you mean by missing bracket though. – dbenham Jun 26 '12 at 13:57
  • Ok, so I deleted my post if your one is working 100%ly. :) For me, I introduced a ( before the set _Attribs=%%~aF) in order to make this all part of the for-loop. Else, only the 1 thing is being executed? – Andreas Reiff Jun 26 '12 at 14:28
  • @Andreas - The extra parens are not needed, though they don't hurt. It is running a single SET command within the loop. If you want to run multiple commands within the loop then parens are useful. – dbenham Jun 26 '12 at 15:19