Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Firstly, please excuse my ignorance as I am completely new to scripting.. Here is what I am planning to do

  1. Stop the service

    (it would be good if i could check if service exists in the first place, otherwise, it should still continue on with the script correct?)

  2. Check if Win2k or WinXP, Uninstall the service and delete files according to OS type

  3. Install new application

  4. Start service

Here is what I have at the moment. Can someone please review and suggest ways of improvement? Many thanks in advance.

@echo off  
net stop "service name"  

Here, I want to check if the service exists, if yes, just go ahead and stop it, else, echo "Service not installed" and go ahead to uninstall_and_install routine, can i do as below?

if ERRORLEVEL 0 GOTO uninstall_and_install IF NOT echo Service not found GOTO uninstall_and_install  
:uninstall_and_install  
C:  
IF NOT EXIST c:\Windows GOTO Win2k  
cd Windows\system32\  
"insert commands to uninstall and install 

:Win2k  
cd Winnt\system32\  
"insert commands to uninstall and install 

exit 
share|improve this question

1 Answer 1

You might take some hints from the example below.

@echo off
set SERVICENAME=w32time

REM Check if the service is installed
REM ==================================================
    sc query %SERVICENAME% > nul 2>nul
    if %ERRORLEVEL% == 1060 goto serviceDoesNotExist

:serviceDoesExist
    net stop %SERVICENAME%

:serviceDoesNotExist
    REM Get Operating System Version
    for /F "usebackq delims==" %%i IN (`ver`) DO (set OSVersion=%%i)

    echo %OSVersion% | find "Microsoft Windows [Version 6.1.7600]"
    if %ERRORLEVEL% NEQ 0 GOTO :windows7

:windows7
    echo Install the Application the Windows 7 way
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.