1

I don't think this is possible, but I'd like to be able to do this, or possibly use an alternative method...

I have a batch file;

for /f "usebackq tokens=*" %%a in (`wmic process get description, commandline`) do (
*Some Code*
)

I need to be able to take the two answers from each line, and use them individually (basically, use the description to check if a process is running, then after I've killed the process and done some file clean-up work, reload the original process including any command line parameters.

One example of the output for a process I may need to end/re-open might be;

"C:\some folder\some other folder\some_application" -cmd_parameter                process_name.exe

Note that the descrption is clearly defined by multiple spaces..


So is there a way of saying

for /f "tokens=* delims=  "    <--(The delims is TWO spaces, not space OR space)

Another way that may be better could be to replcae all instances of multiple spaces with a special character (i.e. one that is never used in a proces or path), and then use that as my delimeter... Though I don't know if that is even possible..

I'm also open to any alternative methods, as long as I can get the process name (to check against a pre-defined list of processes, and the full path to the exe, plus any command line paramteres given.

Thanks all

Ricky Payne
  • 149
  • 4
  • 14

3 Answers3

2

In direct answer to you question: No, you cannot specify 2 spaces as a delimiter. You can use SET search and replace to change 2 spaces into some unique character, but determining a unique character that will never appear in your description or command line is easier said then done.

A better alternative is to change the output format of WMIC to LIST - one value per line in the form of propertyName=Value. Each propery value can be stored in a variable, and then when the last property for a process is recorded you can take action using the variable values. WMIC output uses Unicode, and that results in a CarriageReturn character being appended to the end of each variable assignment. The CarriageReturn must be stripped to get the correct results.

@echo off
setlocal
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!"
    echo(
    echo Do whatever you need to do with the description and command line.
    echo description=!desc!
    echo command line=!cmd!
    endlocal
  )
)

There are a few things you need to be careful of.

1) You could have multiple processes for the same image name. If you kill a process via the image name (description), then you will delete all of them. If you also restart it it based on the command line, then it will be killed again when the next process with the same name is killed. It is probably better to kill the process via the process ID.

2) If you know the image name (description) of the process, then you can restrict your output using the WMIC WHERE clause.

3) The command line reported by WMIC is not always reliable. The process is able to modify the value that is reported as the command line.

Here is a solution that retrieves the process ID and command line for a specific description.
EDIT - I fixed the code below

@echo off
setlocal
for /f "tokens=1* delims==" %%A in ('"wmic process where description='MyApp.exe' get processId, commandline /format:list"') do (
  if "%%A"=="CommandLine" (
    set "cmd=%%B"
  ) else if "%%A"=="ProcessId" (
    set "id=%%B"
    setlocal enableDelayedExpansion
    set "id=!id:~0,-1!"
    set "cmd=!cmd:~0,-1!"
    echo(
    echo Do whatever you need to do with the process id and command line.
    echo process Id=!id!
    echo command line=!cmd!
    endlocal
  )
)

Note - the WMIC WHERE clause uses SQL syntax. It can be made complex using AND and OR conditions, and it supports the LIKE operator using % and _ as wildcards. I believe the entire expression needs to be enclosed in double quotes when it becomes complex.

dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Thanks, that part works... another question though... I've used your code to write the process paths I need to a text file. Then I figured I could use a loop to re-open those processes using 'start' When I try to re-open the processes, it just opens another CMD window... I know from testing that this is because of the double quotes, but some paths may have spaces in, so would need the double quotes.. Any idea's how I could take for example; "W:\some folder\someapp.exe" -some_parameter and basically open is as though typed into the RUN command?? Cheers – Ricky Payne Aug 15 '12 at 09:40
  • You said it - "another question though". Your question seems complicated enough and unrelated enough to warrant posting a new question. Post what you have tried along with your question. Don't forget to accept this answer if it has answered your original question :-) – dbenham Aug 15 '12 at 12:13
1

Foreword

I'd just like to add this for future readers, because I had this problem, solved it myself and I think it'll be useful just to show how to do this simply. Firstly, dbenham is absolutely correct in his answer that "No, you cannot specify 2 spaces as a delimiter.". Since you can't do it directly using the batch for loop, you can simply make your own that does the job. Again dbenham is correct in saying

"You can use SET search and replace to change 2 spaces into some unique character"

And thats somewhat similar to what I did (with some differences) but for completeness sake I think its good to have it on record. The thing is, simply setting all occurences of double spaces to some other character doesn't always solve the problem. Sometimes we have more than two spaces and what we really want is to delimit strings by more than one space. The problem I was trying to solve here is more like this (from the OP) Ricky Payne

"Another way that may be better could be to replcae all instances of multiple spaces with a special character (i.e. one that is never used in a proces or path), and then use that as my delimeter... Though I don't know if that is even possible.."

The answer to that is that It IS possible, and not hard at all. All you need is to be able to

A. loop over each character of the string

B. differentiate single from double (or more) spaces

C. turn a flag on when you encounter a double space

D. turn the double (or more) spaces into a special character or sequence of characters that you can delimit by.

the code

To do Exactly this, I coded this for my own use (edited for clarity):

FOR /F "tokens=* delims=*" %%G IN ('<command with one line output>') DO (SET 
"LineString=%%G")
SET /A "tempindex=0"
:LineStringFOR
    SET "currchar=!LineString:~%tempindex%,1!"
    IF "!currchar!"=="" (goto :LineStringFOREND)
    SET /A "tempindex=!tempindex!+1"
    SET /A "BeforeSpacePosition=!tempindex!"
    SET /A "AfterSpacePosition=!tempindex!+1"
    IF NOT "!LineString:~%BeforeSpacePosition%,2!"=="  " (goto :LineStringFOR)
    :LineStringSUBFOR
        IF "!LineString:~%BeforeSpacePosition%,2!"=="  " (
        SET LineString=!LineString:~0,%BeforeSpacePosition%!!LineString:~%AfterSpacePosition%!
        GOTO :LineStringSUBFOR
        ) ELSE (
        SET LineString=!LineString:~0,%BeforeSpacePosition%!;!LineString:~%AfterSpacePosition%!
        GOTO :LineStringSUBFOREND
        )
    :LineStringSUBFOREND
    GOTO :LineStringFOR
:LineStringFOREND
ECHO Final Result is "!LineString!"

So if your input (output of the command in the FOR or you can change that FOR loop to take in a string) was:

"a b c a b c"

The output should be in this format:

"a;b;c;a b c"

I have tested this on my own code. However, for my answer here I removed all of my comments and changed some variable names for clarity. If this code doesn't work after putting in your commands feel free to let me know and I'll update it but it SHOULD be working. Formatting on here might prevent a direct copy paste.

Just to show whats actually going on

The program flow is basically like this:

FOR each character
:TOP
grab the next character
set a variable to the current index
set another variable to the next index
IF this or the next character are not spaces, goto the TOP
:Check for 2 spaces again
IF this and the next character are both spaces then
    get the string up to (but not including) the current index AS A
    get the string after the current index AS B
    set the string to A+B
    goto Check for 2 spaces again
ELSE we have turned the double or more space into one space
    get the string up to (but not including) the current index AS A
    get the string after the current index AS B
    set the string to A + <char sequence of choice for delimiting> + B
goto TOP to grab the next character
After all characters are looped over
RETURN the string here (or echo it out like I did)

Extra

dbenham says in his answer on this type of method that:

"You can use SET search and replace to change 2 spaces into some unique character, but determining a unique character that will never appear in your description or command line is easier said then done."

While this may have been true in the past, my yeilded that (at least for my method correct me if I'm wrong for other cases) you can in fact use a delimiter that definitely WON'T appear in your input. this is accomplished by using multicharacter delimiters. This doesn't allow you to use the standard FOR loop, however you can quite easily do this manually. This is described much more in depth here:

"delims=#+#" - more then 1 character as delimiter

Redacted
  • 613
  • 6
  • 23
1

Great thread!

This got me thinking and I came up with a slightly sideways solution that may work well for someone as it did for me.

As the original questions was for the WMIC command, and the output can be CSV format, why not just circumvent the space handling by using the /format:csv switch and setting a comma as the delimiter, and incorporating 'usebackq'?

Of course, this might not work if the data itself from WMIC has commas but waqs perfect in my instance where I wanted only the BootOptionOnWatchDog status

would look something like this:

FOR /F "usebackq skip=1 tokens=1-31 delims=," %a IN (`%windir%\system32\wbem\wmic computersystem list /format:csv`) DO echo %f

which returns:

BootOptionOnWatchDog
Normal boot

I ended using 'skip=2' which would return "Normal Boot"

btw, dont post here often hence posting as a guest, but thought it prudent to put this here as it was this post that helped me come to the answer above.

cheers -steve (NZ)