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
| Rater | Score | Time |
|---|---|---|
| DOSroot | -2 | 2009-01-29 23:15 |
