←August→
| Sun |
Mon |
Tue |
Wed |
Thu |
Fri |
Sat |
| 1 |
2 |
3 |
4 |
5 |
6 |
7 |
| 8 |
9 |
10 |
11 |
12 |
13 |
14 |
| 15 |
16 |
17 |
18 |
19 |
20 |
21 |
| 22 |
23 |
24 |
25 |
26 |
27 |
28 |
| 29 |
30 |
31 |
|
|
|
|
|
|
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
hpoid.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:
-
Escaping a Character in a Windows Batch File
Date: July 12, 2010
MoonPoint Support
-
Escape Characters
Date: January 17, 2008
Batcheero
-
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:
-
Escape Characters
Date: January 17, 2008
Batcheero
-
Escape character
Wikipedia, the free encyclopedia
[/os/windows/commands/batch]
permanent link
Sun, Jul 04, 2010 4:56 pm
Windows NT 5.2
If you run the
winver command on a system and see the
operating system listed as Windows NT 5.2, there are actually several
versions of Windows as well as
ReactOS that identify themselves as
Windows NT 5.2
-
Windows
Server 2003
-
Windows
Small Business Server 2003
-
Windows XP 64-bit Edition
-
Windows XP Professional x64 Edition
-
Windows Home Server
-
ReactOS
References:
-
Windows NT 5.2
Wikipedia, the free encyclopedia
[/os/windows/commands]
permanent link
Mon, Jun 21, 2010 6:10 pm
Finding Large Files on a Windows System from the Command Line
You can find all files over 99 MB in size and store the file names in a text
file with the command below:
dir C:\ /a-d/s | findstr "[0-9][0-9][0-9],[0-9].*,[0-9]" | findstr /v
"[er](s)" > bigfiles.txt
You can use dir /a-d to select only files and not directories.
The /a option means that you want to display files with the
specified attributes. Directories are specified by d; specifying
a -d means select everything that is not a directory.
The /s option for the dir command indicates that you
want to display files in the specified directory and all of its subdirectories.
You can use the findstr command to filter the output of the
dir command. If I only wanted to see files that were at least
10 MB or more, I could use findstr "[0-9][0-9],[0-9].*,". To send
the output of the dir command to the findstr command,
you use the |, which is the pipe symbol, e.g.,
dir /a-d/s | findstr "[0-9][0-9],[0-9].*,". The information between
the double quotes is a "regular expression" that tells the findstr
command what to look for in the input it receives. I want it to look for
two numbers, followed by a comma, then another number, then zero or more
characters and then another comma. The [0-9] tells
finstr to look for any character in the numeric set, i.e., 0, 1,
2, 3, 4, 5, 6, 7, 8, or 9. Since I include [0-9] twice,
findstr looks for two numbers, one after the other. The dot,
., is used to represent any character and the *
following it states the character can occur zero or more times. Then another
comma should appear. That command
dir /a-d/s | findstr "[0-9][0-9],[0-9].*,", might produce the
following output.
C:\Documents and Settings\administrator>dir /a-d/s | findstr "[0-9][0-9],[0-9].*
,"
07/30/2002 04:27p 35,417,061 savceclt.exe
5 File(s) 36,390,066 bytes
217 File(s) 38,486,808 bytes
0 Dir(s) 469,125,120 bytes free
I don't want the "File(s) and "Dir(s)" lines. I can eliminate them by sending
the outpout of the findstr command to another findstr
command, i.e., dir /a-d/s | findstr "[0-9][0-9],[0-9].*," |
findstr /v "[er](s)". The /v option to findstr
tells findstr to display only lines that do not contain a match.
In this case, I tell it to look for either an "e" or "r" followed by "(s)"
and to discard any lines that match.
:\Documents and Settings\administrator>dir /a-d/s | findstr "[0-9][0-9],[0-9].*
," | findstr /v "[er](s)"
07/30/2002 04:27p 35,417,061 savceclt.exe
In the case above, I've found all files greater than 10 MB in the
administrator's "My Documents" directory or beneath it. If I want to
search the entire system for files 100 MB or greater, I could use the
following commands:
C:\Documents and Settings\administrator>dir C:\ /a-d/s | findstr "[0-9][0-9][0-9
],[0-9].*,[0-9]" | findstr /v "[er](s)" > bigfiles.txt
Those commands start at the root directory, C:\ and search
for all files within that directory or beneath it that are over 99 MB. The
>bigfiles.txt redirects the output of the last command to
a file named bigfiles.txt.
Examing the bigfiles.txt file, I might see something like
the following:
C:\Documents and Settings\administrator>type bigfiles.txt
06/21/2010 05:21p 267,948,032 hiberfil.sys
06/21/2010 05:21p 314,572,800 pagefile.sys
06/09/2010 05:49p 277,297,548 Deleted Items.dbx
06/09/2010 05:49p 151,866,224 Inbox.dbx
06/09/2010 05:49p 144,463,812 Sent Items.dbx
10/27/2009 08:51p 949,861,964 Inbox
06/20/2010 07:48p 1,014,070,855 Sent
03/13/2010 04:42p 2,148,821,701 Inbox
06/21/2010 03:47p 388,459,482 Sent
06/21/2010 05:00p 201,147,392 personal.pst
Directory of C:\Program Files\PhoneTools\vocfiles\WAVMS,11025,8,1
07/27/2007 09:03a 119,977,472 500dc2a.msp
07/27/2007 09:03a 119,977,472 5a34890.msp
09/14/2007 01:16p 113,491,064 MAINSP3.CAB
In the case above, there was one line that
was included that I didn't actually want just because it did fit
the pattern I was looking for, i.e., the WAVMS,11025,8,1 line.
But I would expect entries like that to be rare, so I'm not concerned
about it in this case.
The directory where the file was found won't be listed, but you can look
through bigfiles.txt and then locate particular files listed
in it and then make the current directory the root directory with
cd \ and then search all subdirectories for the file name as
in the example below:
C:\Documents and Settings\administrator>cd \
C:\>dir /s 500dc2a.msp
Volume in drive C has no label.
Volume Serial Number is F4D7-D263
Directory of C:\WINDOWS\Installer
07/27/2007 09:03a 119,977,472 500dc2a.msp
1 File(s) 119,977,472 bytes
Total Files Listed:
1 File(s) 119,977,472 bytes
0 Dir(s) 471,283,200 bytes free
[/os/windows/commands]
permanent link
Mon, Jan 18, 2010 5:56 pm
Finding Time of Last Reboot
On Windows systems, such as XP, Vista, Small Business Server 2003, or Windows 7,
you can determine the time a system was last booted from a command prompt
using the
systeminfo and
find commands.
C:\>systeminfo | find "System Boot"
System Boot Time: 8/31/2009, 10:36:15 AM
For older versions of Windows, such as Windows NT or Windows 2000, you can
use the
uptime utility available in the Resource Kit for that
version of Windows.
C:\Program Files\Reskit>uptime
\\JILL has been up for: 6 day(s), 9 hour(s), 34 minute(s), 7 second(s)
For Windows NT, you can download the uptime utility from
Uptime.exe Tool Allows You
to Estimate Server Availability with Windows NT 4.0 SP4 or Higher.
For Windows 2000, the tool can be downloaded from
Application Center 2000 - Uptime Tool.
[/os/windows/commands]
permanent link
Wed, Dec 30, 2009 10:46 pm
Determining the Uninstall Location for a Program
I needed to be able to determine the uninstall program for a program that
was installed on a system by querying the registry. When you go to the
Control Panel and choose "Add or Remove Programs" or "Uninstall a
Program", you see a list of installed programs, such as is shown below:
The information listed there is assoicated with
Uninstall
registry keys that are found at
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
in the
Windows registry.
I created a batch file to query the registry to show all of the
programs that have such an Uninstall key and that will
accept a program name and return the uninstall command found under that
key for the program.
The first example below shows the output when no arguments are passed to
the batch file on the command line. For the second example, the script finds the
uninstall value in the registry for the program vim. The third example shows
the output when the value is not found. The fourth example shows how
to query when the name of the program has spaces in it.
C:\Users\JDoe\Downloads>uninstallstring
AddressBook
Adobe Flash Player ActiveX
All ATI Software
ATI Display Driver
CDisplay_is1
Connection Manager
DirectDrawEx
DXM_Runtime
Fontcore
IE40
IE4Data
IE5BAKEX
IEData
MobileOptionPack
MPlayer2
PuTTY_is1
RealPopup_is1
SchedulingAgent
SecondLife
Total Uninstall 5_is1
Vim
WIC
{9D07059A-EC99-4F03-9BF2-BE40FB007822}
{FB08F381-6533-4108-B7DD-039E11FBC27E}
C:\Users\JDoe\Downloads>uninstallstring vim
C:\Program Files\vim\vim72\uninstall.exe
C:\Users\JDoe\Downloads>uninstallstring gvim
ERROR: The system was unable to find the specified registry key or value.
C:\Users\JDoe\Downloads>uninstallstring Adobe Flash Player ActiveX
C:\Windows\system32\Macromed\Flash\uninstall_activeX.exe
The batch file is as follows:
@echo off
REM uninstallstring.bat
REM
REM Written By: Jim Cameron
REM Created: 2009-12-29
REM Last Modified: 2009-12-30
REM Version: 1.0
REM
REM Usage:
REM
REM uninstallstring
REM uninstallstring program
REM
REM Purpose: If no arguments are given to the batch file on the command line,
REM it will display a list of all the programs with UninstallString values,
REM i.e., all the registry keys under
REM HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall. If
REM an argument appears on the command line, it should be one of the values
REM reported when no arguments are given to the script, i.e. a program name,
REM such as "vim" or "Adobe Flash Player ActiveX" (don't actually put the quotes
REM around the names, even if there are spaces in the name. E.g. for the latter
REM case you would use the following:
REM
REM uninstallstring Adobe Flash Player Activex
REM
REM When a program name is included on the command line, uninstallstring.bat
REM will determine the "UninstallString" value in the registry for that
REM particular program, i.e. the location for the uninstall program for a
REM particular piece of software. It will return just that value. E.g., for
REM the "uninstallstring vim", it would return the following:
REM
REM C:\Program Files\vim\vim72\uninstall.exe
REM
REM If it can not find an uninstall registry value for the program listed it
REM will return the following:
REM
REM ERROR: The system was unable to find the specified registry key or value.
REM The following example shows a "reg query" command that could be issued from
REM the command line to determine the value of "UninstallString" for the Vim
REM editor software. The last line of output contains the "value name",
REM "value type", and "value data", which is the part of the output of interest.
REM C:\&glt;reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Vim /v UninstallString
REM
REM HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Vim
REM UninstallString REG_SZ C:\Program Files\vim\vim72\uninstall.exe
REM First, display the UninstallString values present in the registry.
REM Values returned by the reg query command will be in the following format:
REM
REM HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Vim
IF "%1"=="" (
GOTO Show_All
) ELSE (
GOTO Find_String )
:Show_ALL
set _All=reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
FOR /f "tokens=6* delims=\" %%A IN ('%_All%') Do echo %%B
GOTO End
:Find_String
REM Set _Program to be the parameter entered on the command line.
REM Use %* rather than %1 to cover cases where the program has spaces in the
REM name.
set _Program=%*
REM Set the variable _UninstallString_Query to the reg query command to be issued.
set _UninstallString_Query=reg query "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\%_Program%" /v UninstallString
REM There are two parts at the beginning of the line, the "value name" and the
REM "value type" that aren't relevant, By specifying "2*" the "value name" is
REM ignored, the "value type" goes into %%A and %%B holds everything else on
REM the line.
FOR /f "skip=2 tokens=2*" %%A IN ('%_UninstallString_Query%') Do echo %%B
:End
Download uninstallstring.bat
References:
-
Creating DOS Batch Files
Useful Information: tons of info
stuffed into a small site
-
For /f - Looop through text
SS64.com Command line reference
-
For - Looop through command output
SS64.com Command line reference
-
Batch files - How To ... Verify if Variables are Defined
Rob van der Woude's Scripting Pages
-
Microsoft Windows XP - If
Microsoft Corporation
-
Microsoft DOS if command
Computer Hope's free computer help
-
Information on batch files
Computer Hope's free computer help
-
Microsoft Registry Tools
Softpanorama (slightly skeptical)
Open Source Software Educational Society
-
Quotes, Escape Chars, Delimiters
SS64.com Command line reference
-
Tips for using the Windows command prompt in Windows XP
The Command Line in Windows
-
Batch File Command Reference
At The Data Center - by Don WIlwol
-
Batch file count tokens in var
Computing.net Computer Tech Support Forum
[/os/windows/commands]
permanent link
Sun, Feb 15, 2009 3:44 pm
Substring Extraction with the FOR /F Command
I want to obtain the file name listed as the value for
Wallpaper
in the registry key
HKCU\Software\Microsoft\Windows\CurrentVersion\Themes\LastTheme.
I can do so using the
reg query command.
C:\>reg query HKCU\Software\Microsoft\Windows\CurrentVersion\Themes\LastTheme /v
Wallpaper
! REG.EXE VERSION 3.0
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\LastTheme
Wallpaper REG_EXPAND_SZ %SystemRoot%\Web\Wallpaper\Ripple.jpg
From a batch file, though, I only want the filename, i.e.
%SystemRoot%\Web\Wallpaper\Ripple.jpg.
I can select just that part of the output usng the
FOR /F command in a batch
file. The syntax of the FOR /F command is as follows:
FOR /F ["options"] %%parameter IN ('command_to_process') DO command
Key
options:
delims=xxx The delimiter character(s)
(default = a space)
skip=n A number of lines to skip at the beginning.
(default = 0)
eol=; Character at the start of each line to indicate a comment
The default is a semicolon ; Use "eol=" to process all lines
tokens=n Specifies which numbered items to
read from each line
(default = 1)
usebackq Specify `back quotes`
the command_to_process is placed in `BACK quotes`
instead of 'straight' quotes
command_to_process : The output of the 'command_to_process' is
passed into the FOR parameter.
command : The command to carry out, including any
command-line parameters.
%%parameter : A replaceable parameter:
in a batch file use %%G (on the command line %G)
FOR /F processing of a command consists of reading the output from
the command one line at a time and then breaking the line up into individual
items of data or 'tokens'. The DO command is then executed with the
parameter(s) set to the token(s) found.
By default, /F breaks up the command output at each blank space, and any
blank lines are skipped. You can override this default parsing behavior
by specifying the "options" parameter. The options must be contained
within double quotes.
In this case, the last line of output is the following:
Wallpaper REG_EXPAND_SZ %SystemRoot%\Web\Wallpaper\Ripple.jpg
The delimiter between the fields in the output is the tab character
(I checked for whether the delimiter was spaces or tabs with
hod
by redirecting the output of the reg query command to a file with
reg query
HKCU\Software\Microsoft\Windows\CurrentVersion\Themes\LastTheme /v Wallpaper
> temp.txt).
For the FOR /F command, the default delimiter is the space and
tab characters so I don't have to specify delims=. But, if
I don't, the output will be incorrect in cases where the path name or
file name contain spaces, since the second space will be treated as
a demarcation point, so that the space-separated parts of the path name or file
name are treated as separate tokens.
"Tokens" are the parts of the line separated by the delimiter. In this case,
I'm only interested in the third token, i.e.
%SystemRoot%\Web\Wallpaper\Ripple.jpg, so
I can use the following lines in a batch file:
FOR /F "tokens=3" %%R in ('reg query HKCU\Software\Microsoft\Windows\CurrentVersion\Themes\LastTheme /v Wallpaper') DO SET wallpaper=%%R
echo %wallpaper%
I used the tokens=3 option to select just the third
token on the output line. The %%R for the parameter name
is arbitrary. I could have called it %%S, %%T, etc.,
instead, if I wished. The command I wished to process is enclosed
between '( and )'. Note: for the reg
query command, if the registry key contains spaces in the key name,
then you must enclose the registry key name within double quotes as in the
following batch file example:
@echo off
FOR /F "tokens=3" %%R in ('reg query "HKCU\Control Panel\Desktop" /v ConvertedWallpaper') DO SET wallpaper_file=%%R
echo %wallpaper_file%
The output produced by the batch file is shown below:
C:\WINDOWS\Web\Wallpaper\Ripple.jpg
But, if I checked the value for OriginalWallper in the
registry key
HKCU\Control Panel\Desktop with reg query as below,
I would see a path name containing spaces.
C:\>reg query "HKCU\Control Panel\Desktop" /v OriginalWallpaper
! REG.EXE VERSION 3.0
HKEY_CURRENT_USER\Control Panel\Desktop
OriginalWallpaper REG_SZ C:\Documents and Settings\James\Local Settings\A
pplication Data\Microsoft\Wallpaper1.bmp
If I don't specify delims= in the batch file, e.g., if I use
the code below, I would get incorrect output.
@echo off
FOR /F "tokens=3" %%R in ('reg query "HKCU\Control Panel\Desktop" /v OriginalWallpaper') DO SET wallpaper_file=%%R
echo %wallpaper_file%
The output would be as shown below:
C:\Documents
That is because C:\Documents will be treated as one token
and then the space between Documents and and Settings
is treated as the demarcation point between two tokens. But, if I instead
include delims= and immediately hit the tab key, that
problem won't occur, since the the horizontal tab character between the fields
will be used to break up the line into tokens.
@echo off
FOR /F "delims= tokens=3" %%R in ('reg query "HKCU\Control Panel\Desktop" /v OriginalWallpaper') DO SET
wallpaper_file=%%R
echo %wallpaper_file%
You can't see any character after the delims=, but in order
for the FOR /F command to work in this case, I had to
type delims= and then hit the tab key while editing the file
in Windows Notepad. If you then save the file, it will produce the correct
output, e.g.:
C:\Documents and Settings\James\Local Settings\Application Data\Microsoft\Wallpaper1.bmp
Note: if you are using Vim as your editor
on a Windows system, as I usually do, then you need to insert the tab character
after delims= by hitting the Ctrl-I keys simultaneously.
You can then hit the spacebar and type tokens=. If you just
hit the tab key, your output won't be as you expect.
Wallpaper-info.bat is an example batch file to query the values in
the Windows registry pertaining to the file used for the Windows wallpaper
using the FOR /F command.
References:
-
For - Loop through command output
SS64.com
-
NT's FOR /F command: tokens and delims
Rob van der Woude's Scripting Pages