MoonPoint Support Logo

 

Shop Amazon Warehouse Deals - Deep Discounts on Open-box and Used ProductsAmazon Warehouse Deals



Advanced Search
July
Sun Mon Tue Wed Thu Fri Sat
       
12
2010
Months
Jul


Mon, Jul 12, 2010 11:33 am

Setting a Variable to be the Output of a Command

In a Windows batch file, I needed to set a variable to be the output of a command. You can use the for command for that purpose as shown below. In the example below, I wanted to count the number of hpboid.exe processes running on a system.

REM Count the number of hpboid.exe processes running.
for /f "delims=" %%a in ('tasklist /fi "imagename eq hpboid.exe" ^| find /c /i "hpboid.exe"') do set numprocesses=%%a
echo Number of hpboid.exe processes running: %numprocesses% >> %log%

The tasklist /fi "imagename eq hpboid.exe" command uses the tasklist command with the /fi option to filter the output of running processes to just those named hpboid.exe. I then want to "pipe" the output to the find command. I use the /i option to have find ignore the case of the letters in hpboid.exe, i.e., I want to count a process whether it is named hpboid.exe or HPBOID.exe, etc. I use the /c option to tell find to display only the count of lines containing the string hpboid.exe.

For Windows, and I presume DOS as well, you need to put a caret, i.e., a ^ in front of the pipe character, | character to "escape" how it would otherwise be interpreted.

The variable numprocesses is set to be the count of the hpboid.exe processes running, which I then send to a log file, i.e., %log%, which is a variable that was set earlier in the batch file.

References:

  1. Escaping a Character in a Windows Batch File
    Date: July 12, 2010
    MoonPoint Support
  2. Escape Characters
    Date: January 17, 2008
    Batcheero
  3. Escape character
    Wikipedia, the free encyclopedia

[/os/windows/commands/batch] permanent link

Mon, Jul 12, 2010 11:16 am

Escaping a Character in a Windows Batch File

I had the following in a Microsoft Windows batch file:

REM Count the number of hpboid.exe processes running.
for /f "delims=" %%a in ('tasklist /fi "imagename eq hpboid.exe" | find /c /i "hpboid.exe"') do set numprocesses=%%a
echo Number of hpboid.exe processes running: %numprocesses% >> %log%

When I ran the batch file, I would see | was unexpected at this time.. I then realized I needed to "escape" the meaning of the | at that point in the code. For Windows, and I presume DOS as well, you can put a caret, i.e., a ^, in front of a character to "escape" how it would otherwise be interpreted. So the following worked, instead:

for /f "delims=" %%a in ('tasklist /fi "imagename eq hpboid.exe" ^| find /c /i "hpboid.exe"') do set numprocesses=%%a

If you are using a variable, e.g. %myvariable% in a for loop in a batch file, you need to use another percent sign, i.e., a % before each of the % characters used for the variable name. I.e., you need to use %%myvariable%%. You can think of the additional % "escaping" the meaning of the other one.

References:

  1. Escape Characters
    Date: January 17, 2008
    Batcheero
  2. Escape character
    Wikipedia, the free encyclopedia

[/os/windows/commands/batch] permanent link

Valid HTML 4.01 Transitional

Privacy Policy   Contact

Blosxom logo