WMIC批量拿下盛大parent
WMIC getting the grand parent in batch
已经能够将以下内容放在一起,但在末尾多了一行。
For /f "tokens=1,2 skip=1 delims=\" %%i IN (
'WMIC service WHERE "name LIKE 'tomcat%%'" GET PathName'
) DO (
echo %%i\%%j
)
Pause
当运行时,我们看到:
- D:\tomcat
- D:\tomcat2
- \
和\不正确。
如果您想批量执行,可以改用此语法。这只会为您提供 parent 和 Grandparent 文件夹。如果您需要这些的完整路径,请将 dp
修饰符添加到 FOR 变量。
@echo off
For /f "skip=1 delims=" %%G IN ('WMIC service WHERE "name LIKE 'wdpostman'" GET PathName ^|findstr /r /V "^$"') DO (
for %%H in ("%%~dpG\.") do set "parent=%%~nxH"
for %%H in ("%%~dpG\..") do set "gparent=%%~nxH"
)
echo parent=%parent%
echo gparent=%gparent%
只是想添加这组额外的代码来向您展示如何将文件夹路径分解为变量。
@echo off
setlocal enabledelayedexpansion
For /f "skip=1 delims=" %%G IN ('WMIC service WHERE "name LIKE 'wdpostman'" GET PathName ^|findstr /r /V "^$"') DO (
set "_fpath=%%~dpG"
)
set "_fpath=%_fpath:~0,-1%"
set i=1
set "_spath!i!=%_fpath:\=" & set /A i+=1 & set "_spath!i!=%"
echo There %i% Folder(s^)
set _spath
Pause
我工作中的一位同事向我指出:
How can I remove empty lines from wmic output? - and more specifically:
For /f "tokens=1,2 skip=1 delims=\" %%i IN (
'WMIC service WHERE "name LIKE 'tomcat%%'" GET PathName ^| findstr /r /v "^$"'
) DO (
echo %%i\%%j
)
Pause
我知道它不像@Squashman 的解决方案那么优雅,但这是我正在寻找的或至少与之相似的。
根据我的经验,PathName 实际上 returns 额外的行。当我说我的意思是在 WMIC 命令的通常 'odd' 输出之上。为了确保您的输出不受影响,我会使用 Call
.
来解决它
@Echo Off
For /F "Skip=1 Delims=" %%A In (
'"WMIC Service Where (Name Like 'tomcat%%') Get PathName"'
) Do Call :Sub %%A
Timeout -1
GoTo :EOF
:Sub
If Not "%~1"=="" Echo(%*
环绕另一个 for /F
循环以摆脱 for /F
:
for /F "skip=1 delims=" %%a in ('
wmic Service where "Name LIKE 'tomcat%%'" get PathName
') do (
for /F "tokens=1,2 delims=\" %%i in ("%%a") do (
echo %%i\%%j
)
)
外层循环读取除了你想跳过的那一行之外的所有行;内部的解析文本。
已经能够将以下内容放在一起,但在末尾多了一行。
For /f "tokens=1,2 skip=1 delims=\" %%i IN (
'WMIC service WHERE "name LIKE 'tomcat%%'" GET PathName'
) DO (
echo %%i\%%j
)
Pause
当运行时,我们看到:
- D:\tomcat
- D:\tomcat2
- \
和\不正确。
如果您想批量执行,可以改用此语法。这只会为您提供 parent 和 Grandparent 文件夹。如果您需要这些的完整路径,请将 dp
修饰符添加到 FOR 变量。
@echo off
For /f "skip=1 delims=" %%G IN ('WMIC service WHERE "name LIKE 'wdpostman'" GET PathName ^|findstr /r /V "^$"') DO (
for %%H in ("%%~dpG\.") do set "parent=%%~nxH"
for %%H in ("%%~dpG\..") do set "gparent=%%~nxH"
)
echo parent=%parent%
echo gparent=%gparent%
只是想添加这组额外的代码来向您展示如何将文件夹路径分解为变量。
@echo off
setlocal enabledelayedexpansion
For /f "skip=1 delims=" %%G IN ('WMIC service WHERE "name LIKE 'wdpostman'" GET PathName ^|findstr /r /V "^$"') DO (
set "_fpath=%%~dpG"
)
set "_fpath=%_fpath:~0,-1%"
set i=1
set "_spath!i!=%_fpath:\=" & set /A i+=1 & set "_spath!i!=%"
echo There %i% Folder(s^)
set _spath
Pause
我工作中的一位同事向我指出:
How can I remove empty lines from wmic output? - and more specifically:
For /f "tokens=1,2 skip=1 delims=\" %%i IN (
'WMIC service WHERE "name LIKE 'tomcat%%'" GET PathName ^| findstr /r /v "^$"'
) DO (
echo %%i\%%j
)
Pause
我知道它不像@Squashman 的解决方案那么优雅,但这是我正在寻找的或至少与之相似的。
根据我的经验,PathName 实际上 returns 额外的行。当我说我的意思是在 WMIC 命令的通常 'odd' 输出之上。为了确保您的输出不受影响,我会使用 Call
.
@Echo Off
For /F "Skip=1 Delims=" %%A In (
'"WMIC Service Where (Name Like 'tomcat%%') Get PathName"'
) Do Call :Sub %%A
Timeout -1
GoTo :EOF
:Sub
If Not "%~1"=="" Echo(%*
环绕另一个 for /F
循环以摆脱 for /F
:
for /F "skip=1 delims=" %%a in ('
wmic Service where "Name LIKE 'tomcat%%'" get PathName
') do (
for /F "tokens=1,2 delims=\" %%i in ("%%a") do (
echo %%i\%%j
)
)
外层循环读取除了你想跳过的那一行之外的所有行;内部的解析文本。