China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-01 14:45
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Help] How to strip the first few characters from several lines of output without generating an intermediate file? View 3,432 Replies 8
Original Poster Posted 2005-12-26 16:40 ·  中国 上海 虹口区 海电信科技发展有限公司电信节点
初级用户
★★
Credits 196
Posts 82
Joined 2005-09-26 11:31
20-year member
UID 42842
Status Offline
Recently I wanted to make a list for an installer.
I don't want to generate an intermediate file. I want to take the output generated by reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Uninstall" /s | find "DisplayName" /i | find "HKEY" /i /v
and then, by adding another pipe or by some other method that does not use third-party programs, process it directly into a txt document containing only the third field, that is, only the “FileZilla (remove only)” part:
DisplayName REG_SZ FileZilla (remove only)
DisplayName REG_SZ HWiNFO32 Version 1.57
DisplayName REG_SZ Brother P-touch Editor Version 4.0
DisplayName REG_SZ EasyRecovery Professional
DisplayName REG_SZ Brother P-touch Quick Editor
DisplayName REG_SZ KP Typing Tutor v7.0

I hope the result will be:
FileZilla (remove only)
HWiNFO32 Version 1.57
Brother P-touch Editor Version 4.0
EasyRecovery Professional
Brother P-touch Quick Editor
KP Typing Tutor v7.0


What I most want is not to generate an intermediate file and not to use third-party tools. Can someone point me in the right direction?
Or does anyone know of a simpler, faster, more complete way to list installed software?

[ Last edited by pillow on 2005-12-27 at 08:22 ]
Floor 2 Posted 2005-12-30 09:08 ·  中国 上海 虹口区 海电信科技发展有限公司电信节点
初级用户
★★
Credits 196
Posts 82
Joined 2005-09-26 11:31
20-year member
UID 42842
Status Offline
Could someone give me a pointer!
Floor 3 Posted 2005-12-30 10:09 ·  中国 河北 石家庄 联通
铂金会员
★★★★
网络独行侠
Credits 6,962
Posts 2,753
Joined 2003-04-16 00:00
23-year member
UID 1565
Gender Male
From 河北保定
Status Offline
Just use a for loop to output the contents of the third field into a TXT file.

for /F "tokens=1,2,3* delims= " %i in ('reg query ...') do @echo %k >> your.txt

Roughly like this.
偶只喜欢回答那些标题和描述都很清晰的帖子!
如想解决问题,请认真学习“这个帖子”和“这个帖子”并努力遵守,如果可能,请告诉更多的人!
Floor 4 Posted 2006-01-03 15:01 ·  中国 上海 虹口区 海电信科技发展有限公司电信节点
初级用户
★★
Credits 196
Posts 82
Joined 2005-09-26 11:31
20-year member
UID 42842
Status Offline
Mm, thanks. The method I came up with later was this one too.
I've already tried it, and it works. Thanks!
I'd also like to ask, in in ('…………'), what does the ' mean?
How is it used?
Otherwise I'll only know that it works, without knowing why it works.

[ Last edited by pillow on 2006-1-3 at 15:02 ]
Floor 5 Posted 2006-01-03 16:22 ·  中国 香港 环球全域电讯
新手上路
Credits 14
Posts 5
Joined 2006-01-03 16:19
20-year member
UID 48373
Status Offline
Under 98 there are no parameters like /F, so how would you do it?




Executes a specified command for each file in a set of files.



FOR %variable IN (set) DO command



%variable specifies a replaceable parameter.

(set) specifies a set of one or more files. Wildcards may be used.

command specifies the command to execute for each file.

command-parameters

specifies parameters or switches for the specified command.



If you use the FOR command in a batch program, specify %%variable instead of

%variable. Variable names are case-sensitive, so %i is different from %I.



If command extensions are enabled, the following additional forms of the FOR command

are supported:



FOR /D %variable IN (set) DO command



If set contains wildcards, then it specifies matching directory

names instead of file names.



FOR /R path] %variable IN (set) DO command



Walks the directory tree rooted at path and executes the

FOR statement in each directory of the tree. If no directory specification

is given after /R, the current directory is assumed.

If set is just a single period (.) character, it will just enumerate the tree.



FOR /L %variable IN (start,step,end) DO command



set is a sequence of numbers from start to end, by step amount.

So (1,1,5) would generate the sequence (1 2 3 4 5) and

(5,-1,1) would generate the sequence (5 4 3 2 1)



FOR /F %variable IN (file-set) DO command

FOR /F %variable IN ("string") DO command

FOR /F %variable IN ('command') DO command



or, if usebackq is used:



FOR /F %variable IN (file-set) DO command

FOR /F %variable IN ('string') DO command

FOR /F %variable IN (`command`) DO command



filenameset may be one or more file names. Each file is opened,

read, and processed before moving on to the next file in the file name set.

File processing consists of reading the file, breaking it into separate text lines,

then parsing each line into zero or more tokens.

The body of the For loop is then called with the variable value(s) set

to the found token string(s). By default, /F passes the first blank-separated

token from each line of each file. Blank lines are skipped.

You can override the default parsing behavior by specifying the "options"

parameter. This is a quoted string containing one or more keywords

to specify different parsing options. The keywords are:



eol=c - specifies an end-of-line comment character

(just one)

skip=n - specifies the number of lines to skip

at the beginning of the file.

delims=xxx - specifies a delimiter set. This replaces

the default delimiter set of space and tab.

tokens=x,y,m-n - specifies which tokens from each line are to be

passed to the for body for each iteration.

This causes additional variable names to be allocated.

The m-n form is a range,

specifying the mth through the nth tokens.

If the last character in the tokens=

string is an asterisk, then an additional variable is allocated

to receive the remaining text on the line

after the last token parsed.

usebackq - specifies that the new semantics are enabled.

Where a backquoted string is executed as a command,

and a single-quoted string is a literal string.

It also allows the use of double quotes to

quote file names in

filenameset.



Here is an example:



FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k



This parses each line in myfile.txt, ignoring lines that begin with a semicolon,

and passes the 2nd and 3rd tokens from each line to the for body,

where the tokens are delimited by commas and/or spaces. Note that the

for body statements refer to %i to get the second token, %j to get

the third token, and %k to get all remaining tokens after the third.

If file names contain spaces, you need to enclose the file name in double quotes.

To use double quotes in that way, you must use the usebackq option.

Otherwise, double quotes are interpreted as defining a literal string.



%i is explicitly declared in the for statement, and %j is implicitly declared

through the tokens= option. You can specify up to 26 tokens via the tokens= line,

provided that it does not attempt to declare a variable higher than the letter

'z' or 'Z'. Keep in mind that FOR variable names are global, and at any one time

you cannot have more than 52 FOR variables in use.



You can also use the FOR /F command to parse a literal string immediately,

by making the filenameset between the parentheses a quoted string.

It will be treated as a single line of input from a file and parsed.



Finally, you can use the FOR /F command to parse the output of a command.

You do this by making the filenameset between the parentheses a single-quoted string.

It will be treated as a command line and passed to a child CMD.EXE, and its

output will be captured into memory and parsed as if it were a file.

So the following example:



FOR /F "delims==" %i IN ('set') DO @echo %i



would enumerate the names of the environment variables in the current environment.

In addition, substitution of FOR variable references has been enhanced.

You can now use the following syntax:



%~I - expands %I and removes any surrounding double quotes (")

%~fI - expands %I to a fully qualified path name

%~dI - expands %I to a drive letter only

%~pI - expands %I to a path only

%~nI - expands %I to a file name only

%~xI - expands %I to a file extension only

%~sI - expanded path contains short names only

%~aI - expands %I to the file attributes of the file

%~tI - expands %I to the date/time of the file

%~zI - expands %I to the size of the file

%~$PATH:I - searches the directories listed in the PATH environment variable

and expands %I to

the fully qualified name of the first one found.

If the environment variable name

is not defined,

or the search does not find the file,

then this modifier expands to

the empty string.



The modifiers can be combined to get compound results:



%~dpI - expands %I to drive letter and path only

%~nxI - expands %I to file name and extension only

%~fsI - expands %I to a full path name with short names only

%~dp$PATH:i - searches the directories listed in the PATH environment variable

for %I and expands the first one found to the drive letter and

path.

%~ftzaI - expands %I to an output line like DIR



In the above examples %I and PATH can be replaced by other valid values. The %~ syntax

is terminated by a valid FOR variable name. Using uppercase variable names like %I

may improve readability and avoid confusion with the modifiers, since they are not

case sensitive.

[ Last edited by panner on 2006-1-3 at 17:45 ]
Floor 6 Posted 2006-01-03 19:35 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re panner:

Mm, I feel this Traditional Chinese for document is semantically smoother than the Simplified Chinese one, and it also doesn't have hard errors like the quoted content below; it's just that it's full of terminology, which will probably leave beginners even more lost and confused. Maybe from Microsoft's point of view, the for command is only meant for system administrators, so rigor and brevity are more important.


FOR /F %variable IN (file-set) DO command
FOR /F %variable IN ("string") DO command
FOR /F %variable IN ('command') DO command



Note the red text: on the second line, the double quote " should be a single quote ' , and on the third line, the single quote ' should be a backquote ` . This is the character-escaping behavior when the usebackq option is used. The original double quotes are then used to enclose file names containing spaces, so the first line's file-set really ought to be enclosed in a pair of double quotes. But because double quotes can be omitted when the file name contains no spaces, and using double quotes when there are spaces is part of the default convention, it doesn't need separate explanation, so it doesn't count as a hard error.

[ Last edited by willsort on 2006-1-3 at 19:37 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 7 Posted 2006-01-04 01:17 ·  中国 广东 深圳 龙岗区 电信
新手上路
Credits 14
Posts 5
Joined 2006-01-03 16:19
20-year member
UID 48373
Status Offline
Dear moderator,
under win98 the for command doesn't have parameters like /F, so how would you do it?
Thanks for the explanation.
Floor 8 Posted 2006-01-04 08:47 ·  中国 上海 虹口区 海电信科技发展有限公司电信节点
初级用户
★★
Credits 196
Posts 82
Joined 2005-09-26 11:31
20-year member
UID 42842
Status Offline
Thanks to panner for the support,
and even more thanks to willsort for the answer, because your reply really solved my question about ' . Thank you!
Floor 9 Posted 2006-01-04 17:01 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re pillow:

Hehe, very few people ask about batch files under win9x/MSDOS now, so you should note the system environment you are using in the first post of the thread, so as to avoid everyone doing useless work as much as possible.

Doing string manipulation under win9x / msdos, while also requiring no third-party tools, is going to be quite complex; of course, for your specific problem, it is relatively much simpler. Below is my solution.

Save the code below as a batch file named DisplayName.bat . The program assumes you have already obtained the exported registry data, with the file name src.reg (as for how to export the registry under win9x, there are many methods), and the target file generated is dst.txt . As you can see from the code, the software's DisplayName is first stored in a variable and then output to the file, because variable control is more flexible. But this will also cause multiple consecutive spaces appearing in DisplayName to be collapsed into a single space. The way to solve this is to use debug instead, but the algorithm will be somewhat more complex.


:: DisplayName.bat - Get DisplayName of software from reg file
:: Will Sort - 16:45 2006-1-4 - MSDOS7.10
@echo off
if "%1"=="REG_SZ" goto ParseName

:Init
if exist dst.txt del dst.txt
if not exist src.reg goto end
find "DisplayName" /i < src.reg | find "HKEY" /i /v > _DspName.bat

:MainLoop
set _name=
if not exist _DspName.bat goto Quit
call _DspName.bat
if "%_name%"=="" goto Quit
echo %_name%
echo %_name%>> dst.txt
find "%_name%" /v < _DspName.bat > _DspName.tmp
copy _DspName.tmp _DspName.bat > nul
goto MainLoop

arseName
set _name=%2
arseLoop
if "%3"=="" goto end
shift
set _name=%_name% %2
goto ParseLoop

:Quit
if exist _DspName.* del _DspName.*
goto end

:end


[ Last edited by willsort on 2006-1-5 at 19:20 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Forum Jump: