Passing argument to subroutine fails RRS feed

  • Question

  • Hi,

    I've written a basic batch script to terminate some unthrusted (thus necessary) services . problem is in terminateByLineId subroutine %%G loop variable isn't passed and i get missing operand for set /a [SKIP_LINES=%~1+7] + when i [echo %~1]

    it shows /b instead of an integer .

    Code :

    setlocal EnableDelayedExpansion
    REM setlocal enableExtensions
    ::queryex output file
    sc queryex>services.TEMP					
    find /i /N "DISPLAY_NAME: Hotspot" services.TEMP>tmp1.TEMP
    FOR /F "skip=2" %%G in (tmp1.TEMP) do (
     set num=%%G
     set num=!num:~1,3!
     echo !num!>serviceList.TEMP
     FOR /F %%G in (serviceList.TEMP) do ( 
    	call terminateByLineId %%G 
    	 REM subroutine call
     :terminateByLineId      ::FAULTY SUBROOUTINE
     setlocal enabledelayedexpansion
     set /a SKIP_LINES=%~1+7
    	 set "secondForFilter="skip=%SKIP_LINES%""  
    	echo %secondForFilter%
     FOR /F %secondForFilter% %%Z in (services.TEMP) do (	
    set serv=%%Z
    set serv=!serv: =!
    set serv=!serv::=!
    set procID=!serv!					
     taskkill  /pid %procID% /f >>debug.txt 2>>debug.txt
     goto breakloop
    del /S *.TEMP 1>>debug.txt 2>>&1

    • Edited by AlphaMike741 Thursday, March 17, 2016 4:35 PM Title
    Thursday, March 17, 2016 4:35 PM


  • We would definitely recommend PowerShell rather than batch.

    In PowerShell, you can stop services very easily:

    PS C:\> Stop-Service Service1,Service2,Service3

    -- Bill Stewart [Bill_Stewart]

    Thursday, March 17, 2016 5:47 PM