1

I'm making a batch script to stop several services , however i get the syntax is incorrect error at second FOR which loops in serviceList.TEMP

setlocal EnableDelayedExpansion
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 %%X in (serviceList.TEMP) do ( 

     set /a "SKIP_LINES=%%G+7"

     set secondForFilter="skip=%SKIP_LINES%"


 FOR /F %secondForFilter% %%Z in (services.TEMP) do (   
    call debug.cmd  REM  debug.cmd -> echo debug pause>nul

set serv=%%Z
set "serv=!serv: =!"          REM Extract PID
set "serv=!serv::=!"          REM Extract PID                           
set procID=!serv!                   


 taskkill  /pid %procID% /f >>debug.txt 2>>debug.txt
 goto secondLoopEnd

 ) 

:secondLoopEnd

)





del /S *.TEMP >>debug.txt 2>>debug.txt
  • I suppose it is crashing on this line `FOR /F %secondForFilter% %%Z in (services.TEMP) do ( ` ? – npocmaka Mar 17 '16 at 15:15
  • 2
    **1**. Read about [`:label` or `:: label-like comment` inside a command block enclosed in `()` parentheses](http://stackoverflow.com/a/32147995/3439404). **2**. Check up closing parenthesis of `%%G` loop. – JosefZ Mar 17 '16 at 15:19

2 Answers2

3

the problem is here:

 FOR %%X in (serviceList.TEMP) do ( 

     set /a "SKIP_LINES=%%G+7"

     set secondForFilter="skip=%SKIP_LINES%"


 FOR /F %secondForFilter% %%Z in (services.TEMP) do (   
    call debug.cmd  REM  debug.cmd -> echo debug pause>nul

the usual approach when you set value in brackets context is to use delayed expansion but it wont work for parametrized for options.

Here you'll need a subroutine.

And you have GOTO in the for loop. GOTO breaks for context and the loops will be not called after goto is executed.

And rem cannot be used on the same line as the code without "&"

Consider something like this (though I cannot check the logic of the bat):

setlocal EnableDelayedExpansion
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 %%X in (serviceList.TEMP) do ( 

    call :subroutine %%G

)

del /S *.TEMP >>debug.txt 2>>debug.txt
exit /b %errorlevel%

:subroutine
setlocal enableDelayedExpansion

set /a skiplines=%~1+7
set "filter="skip=%skiplines%""

     FOR /F %filter% %%Z in (services.TEMP) do (   
        call debug.cmd  

        set serv=%%Z
        rem extract PID
        set "serv=!serv: =!"
        set "serv=!serv::=!"                        
        set procID=!serv!                   


         taskkill  /pid !procID! /f >>debug.txt 2>>debug.txt
         goto :break_for

     ) 
    :break_for
endlocal
exit /b
npocmaka
  • 55,367
  • 18
  • 148
  • 187
1

Should the > in your echo to serviceList.TEMP be a >> so that you append to the file?

echo !num!>>serviceList.TEMP

In which case, you should also ensure that the file is deleted prior to the appending operations.

Also, I assume you missed the /F from your FOR loop, as you're trying to read the lines of the serviceList.TEMP file, yes?

FOR %%X in (serviceList.TEMP) do (

Should be...

FOR /F %%X in (serviceList.TEMP) do (

?

Also, you can append to the same file with both std out and err by doing this...

someprocesshere 1> out.log 2>&1
ManoDestra
  • 6,325
  • 6
  • 26
  • 50