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-06-29 19:01
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » [Closed] How to replace a part of characters in a string View 1,863 Replies 10
Original Poster Posted 2006-07-01 16:44 ·  中国 广东 珠海 电信
初级用户
Credits 112
Posts 40
Joined 2006-06-23 19:52
20-year member
UID 57473
Gender Male
Status Offline
How to replace d:\dos with another path f:\new in the following text?

========================
d:\dos\35选7.bat
d:\dos\CMD 命令速查手册.cmd
d:\dos\cmd_help.htm
d:\dos\game\game.bat
d:\dos\Lan\hostname.txt
========================

Hope the experts can give a good way, and it should be more scalable. For example, if d:\dos is replaced with d:\1\1\1\dos in this question, it can also be realized.

[ Last edited by zhaxi on 2006-7-1 at 23:09 ]
Floor 2 Posted 2006-07-01 17:49 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Suppose the above text is saved in test.txt, just put this code in the same directory as test.txt.

@echo off
setlocal enabledelayedexpansion
for /f "skip=1 eol== tokens=1,2 delims=." %%i in (test.txt) do (
echo %%i|find "d:\dos">nul 2>nul && set new=%%i
echo f:\new\!new:~7!.%%j>>1.txt
)
del /q test.txt
ren 1.txt test.txt
start test.txt
Floor 3 Posted 2006-07-01 23:09 ·  中国 广东 珠海 电信
初级用户
Credits 112
Posts 40
Joined 2006-06-23 19:52
20-year member
UID 57473
Gender Male
Status Offline
Thank you for providing me with the useful usage like !new:~7!.

But I can't understand that you want to separate into %%i and %%j with "." as the delimiter. In this way, if there is a path like d:\dos\ver3.2\3.txt which has 2 ".", the statement will fail. I changed it to "/", which also ensures that each line of the path is passed to %%i in full, and is not afraid of the situation with spaces.

@echo off
setlocal enabledelayedexpansion
for /f "eol== tokens=1 delims=/" %%i in (test.txt) do (
echo %%i|find "d:\dos">nul 2>nul && set new=%%i
echo f:\new\!new:~7!>>1.txt
)
del /q test.txt
ren 1.txt test.txt
start test.txt
Floor 4 Posted 2006-07-01 23:52 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Re zhaxi:

  Observing the text file you provided, there are no cases where two periods appear in the same line, so I will make use of all available conditions :) Actually, there is indeed the situation of execution failure when using periods as separators, but because there are spaces in some lines, it is impossible to separate by spaces. At this time, using periods as separators seems to be the only option.

  But executing your code works really well, which really puzzles me: there are no slash symbols in the text, how can we use / as a separator? Who can explain it?
Floor 5 Posted 2006-07-02 15:43 ·  中国 广东 珠海 电信
初级用户
Credits 112
Posts 40
Joined 2006-06-23 19:52
20-year member
UID 57473
Gender Male
Status Offline
Because from my observation, the path separators are all \, and there can't be /. Haha, so I can use it to split and assign each line of characters to %%i.
Floor 6 Posted 2006-07-02 16:20 ·  中国 广东 珠海 电信
初级用户
Credits 112
Posts 40
Joined 2006-06-23 19:52
20-year member
UID 57473
Gender Male
Status Offline
Also, I'm thinking: If I want to extend this statement to all situations, how to implement it.

Here, "d:\dos\" contains 7 characters. If it's changed to another directory like "d:\dos\dos\" which contains 11 characters.

At this time, through what statement can I get the length of a line of characters?

Hope to get some guidance.
Floor 7 Posted 2006-07-02 16:39 ·  中国 广东 珠海 电信
初级用户
Credits 112
Posts 40
Joined 2006-06-23 19:52
20-year member
UID 57473
Gender Male
Status Offline
Haha. I have a better way to implement this extension. Using %PATH:STR1=STR2%, there's no need to care about how long the characters in d:\dos are.

Now the final code is released as follows:


@echo on

:: Compare the files that exist in directory 1 but do not exist in the corresponding position in directory 2.
:: %1 is directory 1
:: %2 is directory 2

set a=%time%
if exist d:\f1.txt del d:\f1.txt /q
if exist d:\f2.txt del d:\f2.txt /q

dir %1\ /s /b > d:\f1.txt
type d:\f1.txt

cls
setlocal enabledelayedexpansion
for /f "eol== tokens=1 delims=/" %%i in (d:\f1.txt) do (
echo %%i|find "%1">nul 2>nul && (set new=%%i
set result=!new:%1=%2!
if not exist !result! (echo !result:%2=%1!>>d:\f2.txt)
)
)

@echo off
cls
echo.
set b=%time%
echo Your comparison result is placed in d:\f3.txt.
echo Start time: %a%
echo End time: %b%
pause>nul



The above code is saved as compare.bat, and it can be called using compare.bat %1 %2.

[ Last edited by zhaxi on 2006-7-2 at 22:46 ]
Floor 8 Posted 2006-07-02 16:45 ·  中国 广东 珠海 电信
初级用户
Credits 112
Posts 40
Joined 2006-06-23 19:52
20-year member
UID 57473
Gender Male
Status Offline
But still want to ask, "What statement can be used to get the length of a line of characters?"? Because I don't know what to use as the delimiter, haha
Floor 9 Posted 2006-07-02 17:20 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Can characters not present in the text be used as delimiters in for? If that's also possible, oh my god... I still can't get it through my head.

The code for counting the number of characters in a string is as follows (temporarily unable to count strings containing quotes):

@echo off
set str=d:\dos\dos\
:: Quotes cannot appear in the string
set num=0
:loop
set str=%str:~0,-1%
set /a num+=1
if not "%str%"=="" goto loop
echo The number of characters is %num%
pause
Floor 10 Posted 2006-07-02 22:48 ·  中国 广东 珠海 电信
初级用户
Credits 112
Posts 40
Joined 2006-06-23 19:52
20-year member
UID 57473
Gender Male
Status Offline
You're really good. Thanks for providing the code.
I thought I had to use for, so I didn't know which delimiter to take.
Floor 11 Posted 2006-07-02 23:14 ·  中国 四川 成都 鹏博士宽带
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Re zhaxi:

  There are several places in the code on the 7th floor that need to be modified:

  The line "@echo on" should be changed to "@echo off", and the following "@echo off" can be deleted;
  The line "if exist d:\f1.txt del d:\f1.txt /q" can be omitted because the subsequent line "dir ……>d:\f1.txt" uses a single redirection symbol ">", and the output content of this line will overwrite the previously existing f1.txt;
  In the line "dir %1\ /s /b > d:\f1.txt", the parameter "/a" should be added to prevent missing hidden files.
Forum Jump: