Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
PiotrMP006
- Posts: 31
- Joined: 08 Sep 2017 06:10
#1
Post
by PiotrMP006 » 21 Dec 2023 03:30
Hi
Why for loop wmic get processid return an extra invalid value?
Code: Select all
for /f "usebackq skip=1 delims=" %%a in (`wmic process where "name='cmd.exe' and commandline like '%%%~n0%%'" get processid 2^>nul`) do echo %%a
The first returned value %%a is the real processid
The second returned value %%a is a non-existent processid
Why??????????????
Please help
-
miskox
- Posts: 630
- Joined: 28 Jun 2010 03:46
#2
Post
by miskox » 21 Dec 2023 11:17
Looks like wmic returns an extra empty line and this is what you get.
Saso
-
npocmaka_
- Posts: 516
- Joined: 24 Jun 2013 17:10
- Location: Bulgaria
-
Contact:
#3
Post
by npocmaka_ » 21 Dec 2023 11:42
I think I saw you asking the same question in SO and suggested in a comment to check this thread:->
viewtopic.php?t=4266
wmic sets an extra CR at the end which messes the output. But can be stripped with additional for loop:
Code: Select all
for /f "usebackq skip=1 delims=" %%a in (
`wmic process where "name='cmd.exe' and commandline like '%%%~n0%%'" get processid 2^>nul`
) do (
for %%# in ("%%a") echo %%#
)
-
Batcher
- Posts: 74
- Joined: 16 Apr 2009 10:36
#4
Post
by Batcher » 22 Dec 2023 08:45
Code: Select all
@echo off
for /f "tokens=2 delims==" %%a in ('wmic process where "name='cmd.exe' and commandline like '%%%~n0%%'" get processid /value 2^>nul ^| find "="') do (
call set "MyStr=%%%%a"
)
echo --%MyStr%--
pause