|
zhaxi
初级用户
 
积分 112
发帖 40
注册 2006-6-23
状态 离线
|
『楼 主』:
[已结]如何替换字符串中的某一部分字符
使用 LLM 解释/回答一下
我想将下列文本中的d:\dos 替换成 另外一个路径 f:\new 该怎么做啊?
========================
d:\dos\35选7.bat
d:\dos\CMD 命令速查手册.cmd
d:\dos\cmd_help.htm
d:\dos\game\game.bat
d:\dos\Lan\hostname.txt
========================
希望高手给出一个好的办法,而且要扩展性比较强。比如把此题中d:\dos换成了d:\1\1\1\dos也能够实现。
Last edited by zhaxi on 2006-7-1 at 23:09 ]
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 ]
|
|
2006-7-1 16:44 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
假设你的这段文本保存在test.txt中,把这段代码放到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
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
|
|
2006-7-1 17:49 |
|
|
zhaxi
初级用户
 
积分 112
发帖 40
注册 2006-6-23
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
谢谢你给我提供了!new:~7!这样的好用法。
不过我不能理解你要以 “.” 为分隔符分开成%%i,和%%j,这样做的话如果路径中含有d:\dos\ver3.2\3.txt 时,具有2个 . ,则会语句失效。我把它改成了/,这样还保证会把每行路径都全部传递给%%i,不怕有空格的情形。
@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
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
|
|
2006-7-1 23:09 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
Re zhaxi:
观察你提供的文本文件,并没有在同一行中出现两个点号的情况,所以我要利用一切可利用的条件:)其实,用点号作为分隔符确实存在你说的执行失败的情况,但是因为某些行中存在空格,所以不可能用空格来分隔,此时用点号作分隔符似乎是唯一的选择。
但是执行你的代码,确实工作得很好,真让我好生疑惑:文本中并没有斜杠符号呀,怎么可以用/作分隔符呢?哪位能解释一下?
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?
|
|
2006-7-1 23:52 |
|
|
zhaxi
初级用户
 
积分 112
发帖 40
注册 2006-6-23
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
因为根据我观察,所有路径的分割符不都是 \ 吗,不可能出现 / 。 哈哈所以我用它分割可以把每行字符都赋予给%%i啊。
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.
|
|
2006-7-2 15:43 |
|
|
zhaxi
初级用户
 
积分 112
发帖 40
注册 2006-6-23
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
另外我在想:如果要把这个语句扩展到所有情况,该如何实现。
此处d:\dos\里含有7个字符,如果换成别的目录如d:\dos\dos\则含有11个字符了。
此时可以通过什么语句把一行字符的长度取出来呢?
还望指点。
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.
|
|
2006-7-2 16:20 |
|
|
zhaxi
初级用户
 
积分 112
发帖 40
注册 2006-6-23
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
哈哈。我有了更好的办法实现这个扩展了。利用%PATH:STR1=STR2%,就不用管d:\dos有多长的字符了。
现在把最终的代码公布如下:
@echo on
:: 比较目录1中存在,目录2中相应位置不存在的文件。
:: %1为目录1
:: %2为目录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 您的比较结果放在d:\f3.txt中。
echo 开始时间:%a%
echo 结束时间:%b%
pause>nul
上述代码保存为compare.bat,使用compare.bat %1 %2调用就可以了。
Last edited by zhaxi on 2006-7-2 at 22:46 ]
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 ]
|
|
2006-7-2 16:39 |
|
|
zhaxi
初级用户
 
积分 112
发帖 40
注册 2006-6-23
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
不过还是想问下
“可以通过什么语句把一行字符的长度取出来呢”?
因为不知道用什么做分割符,哈哈
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
|
|
2006-7-2 16:45 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
在文本中没有出现的字符可以用来做for中的分隔符吗?要是这样也可以的话,我的天呐……我还是没转过弯来。
统计字符串的字符个数的代码如下(暂时没法统计含有引号的字符串):
@echo off
set str=d:\dos\dos\
:: 字符串中不能出现引号
set num=0
:loop
set str=%str:~0,-1%
set /a num+=1
if not "%str%"=="" goto loop
echo 字符个数为 %num% 个
pause
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
|
|
2006-7-2 17:20 |
|
|
zhaxi
初级用户
 
积分 112
发帖 40
注册 2006-6-23
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
你真是厉害。多谢提供代码。
我还以为要用for呢,所以我不知道取哪个分割符。
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.
|
|
2006-7-2 22:48 |
|
|
namejm
荣誉版主
       batch fan
积分 5226
发帖 1737
注册 2006-3-10 来自 成都
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
Re zhaxi:
你在7楼的代码还有几处有待修改,分别是:
@echo on 一句应该改成@echo off,后面的@echo off可以删掉;
if exist d:\f1.txt del d:\f1.txt /q 一句可以省去,因为后面的dir ……>d:\f1.txt一句中用到了单个的重定向符号>,此句的输出内容会把以前存在的f1.txt覆盖掉;
dir %1\ /s /b > d:\f1.txt 一句中应该再加上参数 /a,防止漏掉隐藏文件。
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.
|
|
2006-7-2 23:14 |
|