views:

67

answers:

4

The short version: is there a way to to write an "and" or an "or" clause in a batch file?

The slightly longer version:

I've inherited a Visual Studio project that creates a dll and then copies that dll to another location. As a post build step, VS runs the following script.

if not '$(ConfigurationName)' == 'DebugNoSvc' goto end

xcopy /Y $(TargetDir)*.config $(ProjectDir)..\myService\bin\Debug
xcopy /Y $(TargetDir)*.config $(ProjectDir)..\myService\bin\DebugNoSvc

:end

It looks like there's a problem when the project is compiled as Debug, since it doesn't do the copy (I'm guessing that at some point the middle section got updated, but the if clause didn't.)

Is there an easy way to do complex boolean logic in "if" clauses in batch?

A: 

I'm beginning to think that something like this might be the best solution, but I'm really not sure:

if '$(ConfigurationName)' == 'DebugNoSvc' goto copyDebugNoSvc
if '$(ConfigurationName)' == 'Debug' goto copyDebug
goto end

:copyDebugNoSvc
xcopy /Y $(TargetDir)*.config $(ProjectDir)..\hsspringhost\bin\DebugNoSvc
goto end

:copyDebug
xcopy /Y $(TargetDir)*.config $(ProjectDir)..\hsspringhost\bin\Debug
goto end

:end
Beska
That is actually more like an `OR` clause, but I agree it seems like what he wants, even though he asked for `AND`.
TommyA
+2  A: 
if not '$(ConfigurationName)' == 'Debug' goto test2

xcopy /Y $(TargetDir)*.config $(ProjectDir)..\myService\bin\Debug
goto end

:test2
if not '$(ConfigurationName)' == 'DebugNoSvc' goto end

xcopy /Y $(TargetDir)*.config $(ProjectDir)..\myService\bin\DebugNoSvc

:end
Kyle Alons
That is actually more like an `OR` clause, but I agree it seems like what he wants, even though he asked for `AND`
TommyA
+1  A: 

With standard DOS batch processing, I think you need one of the multiple if statements as is already posted. Another possibility is to use something like JP Software's command processor Take Command. They have a free version. It is a vast improvement over a normal DOS shell and has a lot of very cool functionality including the ability to use .and. and .or. in an if statement.

Mark Wilkins
A: 

To answer your first question directly: Yes, there's an "and" in Windows batch files (a/k/a NT shell) which allows you to do multiple things on the same line: the ampersand &. There's also a "grouping" option which allows you to treat a set of lines as a single entity: parenthesis ( ... ).

echo Hello, world & if "%windir%"=="C:\WINDOWS" (
echo from Windoze
)

Not sure how that helps in your situation, though... I suspect that Beska's suggestion of if '$(ConfigurationName)' == 'Debug' ... might help, though.

ewall