Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.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
Variables 如何从列表或变量中使用CMD重新打开程序_Variables_Batch File_Cmd - Fatal编程技术网

Variables 如何从列表或变量中使用CMD重新打开程序

Variables 如何从列表或变量中使用CMD重新打开程序,variables,batch-file,cmd,Variables,Batch File,Cmd,我有一段代码 :check for /f "tokens=1* delims==" %%A in ('"wmic process get description, commandline /format:list"') do ( if "%%A"=="CommandLine" ( set "cmd=%%B" ) else if "%%A"=="Description" ( set "desc=%%B" setlocal enableDelayedExpansion

我有一段代码

:check
for /f "tokens=1* delims==" %%A in ('"wmic process get description, commandline /format:list"') do (
  if "%%A"=="CommandLine" (
    set "cmd=%%B"
  ) else if "%%A"=="Description" (
    set "desc=%%B"
    setlocal enableDelayedExpansion
    set "desc=!desc:~0,-1!"
    set "cmd=!cmd:~0,-1!"
    if /i !desc! == %1         (
      echo !cmd! >>C:\test.txt
    )
    endlocal
  )
)
goto:eof
这非常有效(这实际上是一个使用批处理文件调用的函数,例如

call:check processname1.exe
call:check processname2.exe
call:check processname3.exe
etc...
我想做的是(如果可能的话),除了回显到一个文件,我想能够创建两个变量

processname1.exe processname3.exe   <-- (for each process 'checked' if it IS running, append its name to this variable)
commandlinepath1 commandlinepath2   <-- (for each process 'checked' if it IS running, append its path to this variable)
但是我需要做的是,获取这个文件的每一行(或者变量,如果可能的话),并运行它们(如果我将每一行复制到windows的run命令中,它会工作,但是通过CMD执行它不会工作)

我曾尝试使用for循环打开文件,但脚本会在继续之前等待进程完成(这些进程不会结束,因为它们是应用程序)。如果我尝试使用START..则会加载一个新的CMD窗口

我需要做的(如果有更好的选择)是

  • 对于预先确定的一组进程,请检查它们是否正在运行
  • 杀死那些是的人(如果不是,那就忽略它)
  • 删除一些文件(我可以这样做,在进程中停止的原因是它们保持文件打开,防止删除)
  • 重新打开最初运行的所有程序

谢谢。

这不是一个直接的答案,但既然您已经使用了wmic,也许使用其内置功能(查询、启动和停止)会使您的目标更容易实现

我提出以下建议:

@echo off
setlocal
for %%C in (notepad.exe) do (
    for /f "skip=1 tokens=2 delims==" %%F in ('wmic process where description^="%%C" get commandline /format:list') do (
    REM required to normalize unicode output from WMIC 
    set commandline=%%F
    REM '\=\\' required as wmic treats \ as escape char in query
    call wmic process where commandline='%%commandline:\=\\%%' terminate

    REM do your work here

    call wmic process call create '%%commandline%%'

    )
)
它的作用是: 首先,
for
提供进程名称。在我的示例中,它只是
notepad.exe
,但您可以使用列表调用:
for(process1 process2 process3)
,或者将其替换为
for/f
以提供文件中的值。如果您想使用带引号的名称,则必须从下一行(
说明)中删除引号^=“%%C”
)。
第二个
for
做的是真正的工作:它得到一个与描述匹配的所有进程的列表,然后依次停止和启动每个进程

要尝试它,只需将其放入
batfile.bat
,打开记事本并执行即可。
注意:如果用文件打开记事本,请指定一个绝对路径,或通过资源管理器(双击)执行。这里的问题是当前目录-如果您的任何进程引用了相对路径,您也可能会遇到此问题(不太可能,但并非不可能)


最后但并非最不重要的一点-在powershell中执行此操作将是最简单、最短和最可靠的。

如果EXE文件是控制台程序(基于文本),
START
将打开一个新窗口。如果EXE文件是GUI程序,
START
将不会打开新窗口。在不等待控制台程序完成的情况下强制使用
/B
参数(无
/WAIT
)将导致输入冲突,因为CMD和程序都在同一窗口中运行,并且任何输入都将同时提供给两者。
@echo off
setlocal
for %%C in (notepad.exe) do (
    for /f "skip=1 tokens=2 delims==" %%F in ('wmic process where description^="%%C" get commandline /format:list') do (
    REM required to normalize unicode output from WMIC 
    set commandline=%%F
    REM '\=\\' required as wmic treats \ as escape char in query
    call wmic process where commandline='%%commandline:\=\\%%' terminate

    REM do your work here

    call wmic process call create '%%commandline%%'

    )
)