中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-23 18:27
47,812 topics / 349,917 posts / today 0 new / 48,274 members
DOS批处理 & 脚本技术(批处理室) » [Closed] How to replace a part of characters in a string
Printable Version  2,030 / 10
Floor1 zhaxi Posted 2006-07-01 16:44
初级用户 Posts 40 Credits 112
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 ]
Floor2 namejm Posted 2006-07-01 17:49
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Suppose the above text is saved in test.txt, just put this code in the same directory as test.txt.
Floor3 zhaxi Posted 2006-07-01 23:09
初级用户 Posts 40 Credits 112
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
Floor4 namejm Posted 2006-07-01 23:52
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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?
Floor5 zhaxi Posted 2006-07-02 15:43
初级用户 Posts 40 Credits 112
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.
Floor6 zhaxi Posted 2006-07-02 16:20
初级用户 Posts 40 Credits 112
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.
Floor7 zhaxi Posted 2006-07-02 16:39
初级用户 Posts 40 Credits 112
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:




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 ]
Floor8 zhaxi Posted 2006-07-02 16:45
初级用户 Posts 40 Credits 112
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
Floor9 namejm Posted 2006-07-02 17:20
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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):
Floor10 zhaxi Posted 2006-07-02 22:48
初级用户 Posts 40 Credits 112
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.
Floor11 namejm Posted 2006-07-02 23:14
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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.
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023