Visual Studio 2008 - Build Event: How to use Variable (set tmp="abc" and %abc%)?
batch-file, environment-variables, post-build-event, set, visual-studio-2008
Solution
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.
Problem
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.)