Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
For loop 如何在批处理文件中隔离或仅在for循环中存储第二个元素?_For Loop_Batch File_Indexing - Fatal编程技术网

For loop 如何在批处理文件中隔离或仅在for循环中存储第二个元素?

For loop 如何在批处理文件中隔离或仅在for循环中存储第二个元素?,for-loop,batch-file,indexing,For Loop,Batch File,Indexing,我在Windows批处理文件中使用一个循环来返回PID编号,我只想使用第二个PID编号。如何将第二个元素、第二个PID编号设置为变量,或者仅在循环中的命令中使用它而不是echo 代码 for /f "tokens=2 delims=," %%a in ('tasklist /FO CSV ^| findstr /I cmd.exe') do ( echo PID:%%a ) 我尝试像这样索引元素PID:%%a[1]。它不起作用。也许你想要这样的代码 @echo of

我在Windows批处理文件中使用一个循环来返回PID编号,我只想使用第二个PID编号。如何将第二个元素、第二个PID编号设置为变量,或者仅在循环中的命令中使用它而不是
echo

代码

for /f "tokens=2 delims=," %%a in ('tasklist /FO CSV ^| findstr /I cmd.exe') do (
    echo PID:%%a
)

我尝试像这样索引元素
PID:%%a[1]
。它不起作用。

也许你想要这样的代码

@echo off
SetLocal EnableDelayedExpansion
@for /f "tokens=2 delims=," %%a in ('tasklist /FO CSV ^| findstr /I cmd.exe') do (
    set /A count+=1
    Set "PID[!count!]=%%~a"
)

@For /L %%i in (1,1,%count%) do echo PID[%%i] = !PID[%%i]!
echo( I want this PID = !PID[2]!
pause

假设您需要
CMD.exe的第二个实例
,只需使用
FOR
命令
SKIP=
选项即可。然后用一个“GOTO命令中断
FOR
命令。
FOR/f“tokens=2 delims=,”%%a in('tasklist/FO CSV^ | findstr/I cmd.exe^ findstr/N^ | findstr/B“2:”)执行回显PID:%%a