1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Batch File to restart the service after reading value

Discussion in 'Business Applications' started by farhanmazhari, Feb 19, 2013.

Thread Status:
Not open for further replies.
Advertisement
  1. farhanmazhari

    farhanmazhari Thread Starter

    Joined:
    Feb 19, 2013
    Messages:
    5
    Hi All,

    I dont have a programming knowledge, I need support to complete my task. Here is the scenario.

    I am using Windows Server 2003
    text file name "abc.txt" in this file I have a value xyz = 2, I want a script who check this value and if it goes above 10 then it will restart "asd" service.

    I think i describe what i want to achieve.


    Help me......
     
  2. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    Please describe how you would manually restart the service.

    Rollin
     
  3. farhanmazhari

    farhanmazhari Thread Starter

    Joined:
    Feb 19, 2013
    Messages:
    5
    we can start or stop the service through command by typing

    net start [servicename]
    net stop [servicename]
     
  4. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    This should get you close. Untested.
    Code:
    FOR /F "tokens=2 delims==" %%G in ('find "xyz =" <abc.txt') do set xyz=%%G
    IF %xyz% gtr 10 (
         net stop service
         net start service
    )
     
  5. Rollin_Again

    Rollin_Again

    Joined:
    Sep 4, 2003
    Messages:
    4,912
    Does the text file contain anything else other than the line you are looking for? In other words will the text file have to be read line by line for the value in question or will it be the only thing that is contained in the text file?

    Rollin
     
  6. farhanmazhari

    farhanmazhari Thread Starter

    Joined:
    Feb 19, 2013
    Messages:
    5
    Well actually It contains other lines as well and the line which has this value is third line.

    Moreover, the script that you share is like french to me as i am 0 in development side....can i ask spoon feeding here, like how to save it how to run it.
     
  7. farhanmazhari

    farhanmazhari Thread Starter

    Joined:
    Feb 19, 2013
    Messages:
    5
    I am doing this....

    FOR /f "tokens=2" %%G IN ('find "xyz =" abc.txt') DO (set xyz=%%G)
    IF %xyz% gtr '10'
    (net start Helpsvc)


    And getting this error.

    C:\>abc.bat
    C:\>FOR /F "tokens=2" %G IN ('find "xyz =" abc.txt') DO (set xyz=%G )
    C:\>(set xyz=ABC.TXT )
    C:\>(set xyz== ) '10' was unexpected at this time.
    C:\>IF = gtr '10'



    HELP ME.............dont know why its storing xyz=abc.txt and why its not reading the value from line 2.

    Please help.
     
  8. Squashman

    Squashman Trusted Advisor

    Joined:
    Apr 4, 2003
    Messages:
    19,786
    With your code the TOKENS option needs to be set to 3 because by default the FOR /F command will delimit on spaces.
    So the first token is XYZ
    The 2nd token is the =
    The 3rd token would be the value you want.

    Your code is initially setting XYZ to ABC.txt because that is how the FIND command outputs when you are not redirecting input to it like I was doing in my code. The FOR command makes 2 iterations. The 2nd time through it was setting XYZ to the EQUALS symbol.

    Code:
    @echo off
    FOR /f "tokens=3" %%G IN ('find "xyz =" abc.txt') DO set xyz=%%G
    
    echo %xyz%
    
    IF %xyz% gtr 10 echo It is larger
    But it should be quicker running it this way.
    Code:
    @echo off
    FOR /f "tokens=3" %%G IN ('find "xyz =" ^<abc.txt') DO set xyz=%%G
    
    echo %xyz%
    
    IF %xyz% gtr 10 echo it is larger
     
  9. Keebellah

    Keebellah Trusted Advisor

    Joined:
    Mar 27, 2008
    Messages:
    6,597
    First Name:
    Hans
    I don't know if this works with net start and net stop but I used this batch file to kill processes and I imagine it could be used as well for starting

    Code:
    @echo off
    tasklist > %TEMP%\tsk.txt
    if '%1%'=='' goto seetsklst
    findstr /c:"%1%" /P %TEMP%\tsk.txt 
    if errorlevel 1 goto seetsklst2
    taskkill /F /IM %1%
    pause
    goto end
    :seetsklst
    :seetsklst2
    type %TEMP%\tsk.txt
    echo.
    if '%1%'=='' Echo Please indicate which Process needs to be stopped 
    echo.
    Echo Correct syntax is prockill processname
    echo.
    pause
    :end
    
    The batch file's name is prockill.bat or prockill.cmd works both
     
  10. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1090166

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice