Page 1 of 1

function call inside function is not working

Posted: 25 May 2014 09:04
by sambasiva
Hi All,

I have the following batch script which calls the function inside another function.


-------------------
@ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
goto :upServers

:getFullDomainPath
echo "Get full domain path"
goto :eof

:getProcesses
FOR /f "tokens=* delims=" %%I in ('wmic process get ProcessId^,CommandLine /format^:csv ^|findstr java^|findstr "domain.home"^|findstr /V /I /C:"findstr"') do (
FOR %%A in (%%I) do SET pid=%%A
echo PROCESS-PS: "process output printed here"
set COMMAND_LINE=%%I
echo Start of call
call :getFullDomainPath "!COMMAND_LINE!"
echo end of call
)
goto :eof

:upServers
CALL :getProcesses
---------------------------

At runtime the call to the :getFullDomainPath function is not happening for each of the process got from the output of the wmic command in for loop.
It somehow skipping the call :getFullDomainPath "!COMMAND_LINE!" for most of the iteration of the for loop.

Output
-------
PROCESS-PS: "process output printed here"
Start of call ----------------------------------------->call not executed
end of call
PROCESS-PS: "process output printed here"
Start of call ----------------------------------------->call executed
"Get full domain path"
end of call
PROCESS-PS: "process output printed here"
Start of call ----------------------------------------->call not executed
end of call
PROCESS-PS: "process output printed here"
Start of call ----------------------------------------->call not executed
end of call
PROCESS-PS: "process output printed here"
Start of call ----------------------------------------->call not executed
end of call
PROCESS-PS: "process output printed here"
Start of call ----------------------------------------->call not executed
end of call
PROCESS-PS: "process output printed here"
Start of call ----------------------------------------->call not executed
end of call
PROCESS-PS: "process output printed here"
Start of call ----------------------------------------->call executed
"Get full domain path"
end of call


Please help me with this...I need to execute this call for each iteration.

Thanks
SS

Re: function call inside function is not working

Posted: 25 May 2014 09:59
by foxidrive
Your symptoms don't appear here.

I added lines 6 and 7, a pause at the end, and replaced your command string in the for loop to test it.

Code: Select all

@ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
goto :upServers

:getFullDomainPath
echo "Get full domain path"
echo "%~1"
pause
goto :eof

:getProcesses
FOR /f "tokens=* delims=" %%I in ('echo abc') do (
FOR %%A in (%%I) do SET pid=%%A
echo PROCESS-PS: "process output printed here"
set COMMAND_LINE=%%I
echo Start of call
call :getFullDomainPath "!COMMAND_LINE!"
echo end of call
)
goto :eof

:upServers
CALL :getProcesses
pause


The issue could be the trailing CR that is a common problem with WMIC, assuming your WMIC command is correct.
Run the WMIC command at the cmd prompt and redirect the output into a file, and view the file with a hex viewer/editor so you can see what is being parsed.

Also run this to see what is being returned.

Code: Select all

@ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /f "tokens=* delims=" %%I in ('wmic process get ProcessId^,CommandLine /format^:csv ^|findstr java^|findstr "domain.home"^|findstr /V /I /C:"findstr"') do (
echo "%%I"
pause
)

Re: function call inside function is not working

Posted: 25 May 2014 10:37
by sambasiva
Hi foxidrive,

WMIC command is correct and returns the command line of the java processes running.

Output of the snippet you posted is: (replaced the complete command with ******)
----------------------------------
"ADC06BZD ******weblogic.Server,16608
"
Press any key to continue . . .


However the end " is printed in a new line !!!
is that could be a reason ?

Thanks
SS

Re: function call inside function is not working

Posted: 25 May 2014 11:00
by sambasiva
Added the below line to the original script to trim the CR at the end. Still I see the same issue
for /f "tokens=* delims=" %%A in ("%%I") do set COMMAND_LINE=%%A


@ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
goto :upServers
:getFullDomainPath
echo "Get full domain path"
goto :eof
:getProcesses
FOR /f "tokens=* delims=" %%I in ('wmic process get ProcessId^,CommandLine /format^:csv ^|findstr java^|findstr "domain.home"^|findstr /V /I /C:"findstr"') do (
FOR %%A in (%%I) do SET pid=%%A
echo PROCESS-PS: "process output printed here"
for /f "tokens=* delims=" %%A in ("%%I") do set COMMAND_LINE=%%A -------------->Added this line
echo commaneline="!COMMAND_LINE!"
echo Start of call
call :getFullDomainPath "!COMMAND_LINE!"
echo end of call
)
goto :eof
:upServers
CALL :getProcesses

Re: function call inside function is not working

Posted: 26 May 2014 02:44
by julesverne
I wonder two things for you to test. the escape character ^ before the colon : I can't remember if it's needed or not.

sambasiva wrote:FOR /f "tokens=* delims=" %%I in ('wmic process get ProcessId^,CommandLine /format^:csv ^|findstr java^|findstr "domain.home"^|findstr /V /I /C:"findstr"')


The 2nd thing would be would it be possible to set your tokens instead of *? Perhaps those dashes you are seeing I would think are part of the wmic output. If you can figure out what token it is then you could your for loop to skip it ex if the dashes were on token 3. for /f "tokens=1,2,4,5 delims="

Re: function call inside function is not working

Posted: 26 May 2014 03:04
by Compo
@sambasiva
It would help greatly if you tried to explain what particular java string you were trying to capture from the WMIc output!

Re: function call inside function is not working

Posted: 26 May 2014 06:35
by Dragokas
Hi, sambasiva !
You can remove last CR after direct a value to the subroutine:

Code: Select all

@echo off
FOR /f "delims=" %%I in ('2^>NUL wmic process WHERE "Name='java.exe'" GET ProcessId^,CommandLine /format:csv^|findstr "domain.home"') do call :DelCr %%I
pause & exit /B

:DelCr
set COMMAND_LINE=%~1
echo "%COMMAND_LINE%"
exit /B


or using a wmic table format:

Code: Select all

@echo off
SetLocal EnableExtensions EnableDelayedExpansion
for /f "tokens=1* delims==" %%A in (
  'WMIC path win32_process WHERE "Name='java.exe' and commandline like '%domain.home%'" GET "CommandLine"^,"Handle" /value^|findstr /B /C:"CommandLine" /C:"Handle"') do (
  if "%%A"=="CommandLine" Set "COMMAND_LINE=%%B"
  if "%%A"=="Handle" Set "PID=%%B"
  if defined PID (call :execute "!COMMAND_LINE!" !PID!& set PID=)
)
pause & exit /B

:execute
echo Current PID: %~2. Command line is: %~1
exit /B


Best wishes, Alex.