1

I'm doing scripting using appcmd. What I'd like to do is check to see if a given site exists and if does perform an action. In this case, delete it.

appcmd list app | find "mySite"

So, if the find "mySite" returns any text, I'd like to execute a delete statement. Now, I understand I could attempt to delete the site and ignore the error but that gives a (potentially) confusing output.

I've tried something like the following, where I assume I'd just use the variable appExists in an if statement, but it just ends up blank. Alayways.

set appExists=appcmd list app | find "mySite"
echo %~n0: %appExists%

How can I achieve this?

Frank V
  • 449
  • 4
  • 15

1 Answers1

0

You could do:

for /f "delims=" %%f in ('appcmd list app ^| find "mySite"') do (
    set appExists=%%f
)
echo %~n0: %appExists%

But then you could skip setting an environment variable and adding an if test, and build the removal into the for loop:

for /f "delims=" %%f in ('appcmd list app ^| find "mySite"') do (
    rem appcmd whatever whatever remove %%f here
)

(And check the for options (for /?) to use the delims= and tokens= to get whichever part of the line you need directly).

TessellatingHeckler
  • 5,676
  • 3
  • 25
  • 44
  • That is quite complex for what I perceive to be a simple statement. That said, I'll give this a try to see if it achieves what I need. Thank you. – Frank V Sep 22 '15 at 14:28
  • Yes, batch files aren't very flexible. There's plenty of questions and pages on it which lead back to two answers - write the output to a file and read it in, or use a `for` loop. http://stackoverflow.com/questions/6359820/batch-files-how-to-set-commands-output-as-a-variable And linked others. Would be a more readable command in PowerShell, `Get-Website |? Name -match "mysite" | Remove-Website ` or similar. – TessellatingHeckler Sep 22 '15 at 17:02