Search code examples
findstrnetstattasklistbatch-file

Join two commands in dos and get the final out put


Please can someone kindly help me to join these two commands

netstat -ano | findstr 0.0.0.0:80

The output is as follows

enter image description here

Then need to pipe out PID results to next command that is

tasklist | findstr <PID from previous netstat command>

Any help would be greatly appreciated..


Solution

  • @echo off
    for /f "tokens=5" %%# in ('netstat -ano ^| findstr "0.0.0.0:80"') do set "pid=%%#"
    tasklist | findstr "%pid%"
    

    or from command line:

    for /f "tokens=5" %# in ('netstat -ano ^| findstr "0.0.0.0:80"') do set "pid=%#"
    

    To get process name from command line:

    for /f "skip=1 tokens=5" %a in ('qprocess %pid%') do set "image=%a"
    

    from

    for /f "skip=1 tokens=5" %%a in ('qprocess %pid%') do set "image=%%a"
    

    It uses already created variable %pid%