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-11 07:56
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Help with two BAT problems, please help solve them, thanks View 799 Replies 9
Original Poster Posted 2009-01-29 15:21 ·  中国 湖南 长沙 电信
初级用户
Credits 47
Posts 30
Joined 2007-12-18 18:03
18-year member
UID 106058
Gender Male
Status Offline

Moved from DOS Troubleshooting & Problem Discussion (Answer Room)
Obviously posted in the wrong section. Although you're a forum newbie, you've been registered for over a year, so you should be somewhat familiar with the forum by now. A suitable point deduction is given as a reminder.
——Administrator





I'm quite interested in BAT, so I often tinker with it. Now I've run into two problems. I've thought about them for a while but still couldn't solve them,
so now I'd like to ask friends here who are proficient in BAT to help me out.
The first problem is: I want to make a BAT to extract a path.
This path is in the registry. As everyone knows, in registry files exported from the registry, paths are marked with
double slashes ("\\") rather than single slashes ("\") .
Let's use QQ's installation path as an example.
Below is the first part of what I made to extract the path; I can't do the rest.

@Echo off
reg export HKEY_LOCAL_MACHINE\SOFTWARE\TENCENT\QQ\ 1.txt
for /f "tokens=1* delims==" %%i in ('find /I "=" 1.txt') do echo %%j >2.txt

If your QQ is installed under C:\program files\tencent\qq\
then what you get in 1.txt is this:

Windows Registry Editor Version 5.00


"Install"="C:\\Program Files\\Tencent\\QQ\\"

And what you get in 2.txt is:

"C:\\Program Files\\Tencent\\QQ\\"

But what I want now is to get it displayed in the form C:\Program Files\Tencent\QQ\
How should I do that?
Of course, you don't have to start from these first two lines I made; you can use your own method entirely.
As long as the result can be obtained in the form C:\Program Files\Tencent\QQ\ that's fine.

I've tried the following method:

@Echo off
reg export HKEY_LOCAL_MACHINE\SOFTWARE\TENCENT\QQ\ 1.txt
for /f "tokens=1* delims==" %%i in ('find /I "=" 1.txt') do echo %%j >2.txt
set /a n=2
:loop
set /a m=%n%+1
find /i "\\" %n%.txt >NUL
if %errorlevel%==0 set /a n=%n%+1&& for /f "tokens=1* delims=\\" %%i in ('type %n%.txt') do echo %%i\%%j >%m%.txt&&goto loop

But the result can only be "C:\Program Files\\Tencent\\QQ\\", and it goes into an endless loop.

I hope friends here who are proficient in BAT can also point out what's wrong with the batch above.
Many thanks.



The first method was provided by Slore. Based on my original one, it uses the set function, a bit like renaming.
It achieves the requirement with minimal changes. Solid.
Thanks to Slore. Specifically as follows:

@Echo off
echo y|reg export HKEY_LOCAL_MACHINE\SOFTWARE\TENCENT\ 1.txt
for /f "tokens=1* delims==" %%i in ('find /I "QQ.exe" 1.txt') do set a=%%j
set %a%=%a:\\=\%
echo %a% >2.txt

The second method was provided by tireless. It improves my approach and does it in one step. Concise.
Thanks to tireless

@Echo off
echo y|reg export HKEY_LOCAL_MACHINE\SOFTWARE\TENCENT\ 1.txt
for /f "tokens=1* delims==" %%i in ('find /I "QQ.exe" 1.txt') do echo %%~fj >2.txt






The second problem is simpler to describe: I want to add serial numbers to every file in a folder. How should I do that?
For example: suppose I have a folder MP3 under drive D, with many songs in it. Now I want to keep the song names unchanged,
and only add serial numbers like 01, 02, 03... in front of the song names. How should I do that?




For the second problem, friend Slore told me to search the forum, but I couldn't think of the right keywords.
Then it turned out a friend had posted asking about merging two text files line by line.
The address is here: http://www.cn-dos.net/forum/viewthread.php?tid=45985&fpage=4
A friend there came up with an answer, so I learned from it and solved my problem.
Specifically as follows:

@echo off
dir /b >%temp%\1.txt
Setlocal Enabledelayedexpansion
set /a m=0
set /a n=0
set /a a=0
for /f %%k in (%temp%\1.txt) do set /a a+=1
for /l %%i in (1 1 !a!) do (
set /a m+=1
for /f %%j in (%temp%\1.txt) do (
set /a n+=1
if !m!==!n! set b=%%i%%j&&if %%i lss 10 (rename "%%j" "0!b!") else rename "%%j" "!b!"
)
set /a n=0
)
endlocal



[ Last edited by raythunder on 2009-2-9 at 15:21 ]
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
DOSroot -2 2009-01-29 23:15
Floor 2 Posted 2009-01-31 15:34 ·  中国 湖南 长沙 电信
初级用户
Credits 47
Posts 30
Joined 2007-12-18 18:03
18-year member
UID 106058
Gender Male
Status Offline
It's been three days since I posted this. The view count is 41.
How come the experts still haven't finished celebrating the New Year and come online?
Maybe this really wasn't a good time to post.

I saw there was a problem discussion section and came in, but didn't expect I'd come to the wrong place.
Thanks to Manager for setting it right!!!
Still waiting for results.

These days I'm at home, and getting online isn't convenient, but I'll come check the results every few days.
If friends passing by could give this a bump,
so the thread doesn't sink,
I'd appreciate it.
Still waiting for results...
Floor 3 Posted 2009-01-31 15:42 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore
Floor 4 Posted 2009-01-31 15:48 ·  中国 湖南 长沙 电信
初级用户
Credits 47
Posts 30
Joined 2007-12-18 18:03
18-year member
UID 106058
Gender Male
Status Offline
Thanks to Slore for replying.
The result is out now.
I'd like to ask,
how should this line be understood?
set a=%a:\\=\%
Floor 5 Posted 2009-01-31 15:50 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Replace \\ with \

Take a good look at set /? .

As for the renaming one, there are also many posts about it on the forum. Make good use of search.
S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore
Floor 6 Posted 2009-01-31 15:53 ·  中国 湖南 长沙 电信
初级用户
Credits 47
Posts 30
Joined 2007-12-18 18:03
18-year member
UID 106058
Gender Male
Status Offline
Many thanks to Slore for clearing that up.
Can the second problem be solved together as well?
Floor 7 Posted 2009-01-31 19:28 ·  中国 陕西 西安 电信
铂金会员
★★★★
Credits 5,212
Posts 2,478
Joined 2007-02-08 23:39
19-year member
UID 79003
Gender Male
Status Offline
Search the forum. There are lots of examples.
S smile 微笑,L love 爱,O optimism 乐观,R relax 放松,E enthusiasm 热情...Slore
Floor 8 Posted 2009-01-31 23:08 ·  中国 江西 赣州 电信
银牌会员
★★★★
Credits 2,025
Posts 1,122
Joined 2007-09-05 20:15
18-year member
UID 96653
Gender Male
Status Offline
@Echo off
reg export HKEY_LOCAL_MACHINE\SOFTWARE\TENCENT\QQ\ 1.txt
for /f "tokens=1* delims==" %%i in ('find /I "=" 1.txt') do echo %%~fj >2.txt

This can change double slashes into single slashes.
Floor 9 Posted 2009-02-01 15:09 ·  中国 湖南 长沙 电信
初级用户
Credits 47
Posts 30
Joined 2007-12-18 18:03
18-year member
UID 106058
Gender Male
Status Offline
%~fI - expands %I to a fully qualified path name
Thanks to tireless for providing yet another new method.
Floor 10 Posted 2009-02-09 15:22 ·  中国 湖南 长沙 电信
初级用户
Credits 47
Posts 30
Joined 2007-12-18 18:03
18-year member
UID 106058
Gender Male
Status Offline
Thanks to all the friends here for the help, and thanks to the related forum posts for their help too.
TKS
Forum Jump: