|
xue2ye
初级用户
 
积分 26
发帖 11
注册 2006-11-11
状态 离线
|
『楼 主』:
请问下关于set延迟变量的
使用 LLM 解释/回答一下
不好意思 ,也许是我太笨了,我看遍了论坛里关于这方面的 还是没懂。
@echo off
echo 去掉引号:
call :replace 1.txt "
echo.
echo 替换字符:
call :replace 1.txt "谢谢您" "感谢您"
pause>nul
goto :eof
:replace
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=1* delims=" %%i in (%1) do set var=%%i & echo !var:%2=%3!
endlocal
goto :eo
象这个批处理,我怎么都弄不明白什么意思。。尤其是for /f "tokens=1* delims=" %%i in (%1) do set var=%%i & echo !var:%2=%3!
里!var:%2=%3! 这句话的意思。。其他一关于 ! 的问题我就看不懂。哪位给我指点一下呀。结合上面那个例子讲最好了。set 命令 我看 自带的帮助都那个 一元运算符不知道是什么意思。。。谢谢啊。。
I'm sorry, maybe I'm too stupid. I've read all about this in the forum but still don't understand.
@echo off
echo Remove quotes:
call :replace 1.txt "
echo.
echo Replace characters:
call :replace 1.txt "Thank you" "Thank you"
pause>nul
goto :eof
:replace
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=1* delims=" %%i in (%1) do set var=%%i & echo !var:%2=%3!
endlocal
goto :eo
For this batch script, I just can't figure out what it means. Especially the part!var:%2=%3! in for /f "tokens=1* delims=" %%i in (%1) do set var=%%i & echo!var:%2=%3!. I don't understand the part about! either. Can someone give me some pointers? It's best to explain it with the above example. I looked at the help for the set command and didn't understand what the unary operator means... Thanks ah.
|
|
2006-11-13 08:53 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
在论坛里搜索一下吧。关键字:"变量延迟"。
Search in the forum. Keyword: "variable delay".
|
|
2006-11-13 09:31 |
|
|
xue2ye
初级用户
 
积分 26
发帖 11
注册 2006-11-11
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
搜过了就是看不懂。。不好意思哈。笨了点。
I've searched, but I still don't understand. Sorry, I'm a bit slow.
|
|
2006-11-13 09:49 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
Re xue2ye:
简单的给你讲一下吧:
因为命令解释器(%comspec%)在执行for命令时,首先将for命令里的变量扩展为变量的值,所以,当我们执行时,有的变量是根据for命令得到的,这样,变量还没有被定义就被命令解释器给解释(扩展)了,显然是得不到我们想要的,如果要for命令执行之后再扩展变量的值,那么就要启用变量延迟功能来达到这个目的。要启用变量延迟功能,必须在for命令之前首先定义它,用"setlocal ENABLEDELAYEDEXPANSION"就可以开启变量延迟功能。当再次引用变量时,就要用两个"!"将变量包含起来(比如"!var!"),以扩展变量的为其值。
时间关系,讲解得不是很详细,你可以在论坛里面搜索更多的关于变量延迟的详细说明的。
Re xue2ye:
Let me briefly explain it to you:
Because the command interpreter (%comspec%) expands the variables in the for command to their values first when executing the for command. So, when we execute, some variables are obtained according to the for command. In this case, the variable is not defined yet when the command interpreter interprets (expands) it, and obviously we can't get what we want. If we want the variable value to be expanded after the for command is executed, we need to enable the variable delay function to achieve this purpose. To enable the variable delay function, we must define it first before the for command. We can use "setlocal ENABLEDELAYEDEXPANSION" to enable the variable delay function. When referring to the variable again, we need to enclose the variable with two "!" (for example, "!var!") to expand the variable to its value.
Due to time constraints, the explanation is not very detailed. You can search the forum for more detailed descriptions about variable delay.
|
|
2006-11-13 10:16 |
|
|
xue2ye
初级用户
 
积分 26
发帖 11
注册 2006-11-11
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
for /f "tokens=1* delims=" %%i in (%1) do set var=%%i & echo !var:%2=%3!
主要是这句不懂。这句能解释下吗 他这个 !var:“感谢您”=“谢谢您”! 实在想不出来
The `for /f "tokens=1* delims=" %%i in (%1) do set var=%%i & echo !var:%2=%3!` is a Windows batch command.
Here's a breakdown:
- `for /f`: This is a file - reading loop command. It reads lines from a file specified by `%1`.
- `"tokens=1* delims="`: It means it will split each line into tokens. Here, `delims=` means the delimiter is not set, so each line is treated as a single token, and `%%i` will hold the whole line.
- `do set var=%%i`: It sets the variable `var` to the current line read.
- `&`: It is a command separator.
- `echo !var:%2=%3!`: It echoes the value of `var` with the substring replacement. Here, it replaces all occurrences of the string stored in `%2` with the string stored in `%3` in the variable `var`.
The part `!var:"感谢您"="谢谢您"!` is using this substring replacement functionality. It takes the value of `var` and replaces all instances of "感谢您" with "谢谢您".
|
|
2006-11-13 11:33 |
|
|
lxmxn
版主
       
积分 11386
发帖 4938
注册 2006-7-23
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
!var:“感谢您”=“谢谢您”!——讲变量var里面所有的"感谢您"换成"谢谢您"!
!var:“感谢您”=“谢谢您”!——Replace all "感谢您" in variable var with "谢谢您"!
|
|
2006-11-13 11:43 |
|
|
xue2ye
初级用户
 
积分 26
发帖 11
注册 2006-11-11
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
不好意思,还是没理解。。为什么要用感谢您和谢谢您呢。我换了别的好象就不行了。我看了论坛里大部分关于set的 好象都是只讲了原理,没讲具体用法。能讲讲吗。
I'm sorry, but I still don't understand. Why use "Thank you" and "Thanks a lot"? If I use others, it doesn't seem to work. I've looked at most of the posts about set in the forum, and they mostly only talk about the principle, not the specific usage. Can you explain it?
|
|
2006-11-15 06:46 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
有些东西确实很抽象,不举个例子大多数人是不明白的。请各位版主、高手在解答问题时尽量加入一些例子,便于理解。谢谢
Some things are indeed very abstract. Most people won't understand if you don't give an example. Please, all moderators and experts, when answering questions, try to add some examples to facilitate understanding. Thanks
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2006-11-15 07:06 |
|
|
redtek
金牌会员
     
积分 2902
发帖 1147
注册 2006-9-21
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
|

Redtek,一个永远在网上流浪的人……
_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._ |
|
2006-11-15 07:42 |
|
|
xue2ye
初级用户
 
积分 26
发帖 11
注册 2006-11-11
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
我就是看不懂啊。跟着做会自己做就不会了。。你给我举个简单的例子哇。。。谢谢啊
I just can't understand. If I follow to do, I can do it myself, but I can't when I want to do it by myself. You give me a simple example, please... Thank you.
|
|
2006-11-15 07:49 |
|
|
3742668
荣誉版主
      
积分 2013
发帖 718
注册 2006-2-18
状态 离线
|
『第 11 楼』:
使用 LLM 解释/回答一下
从代码格式上看似乎是我写的,不过现在看来代码实在是很不成熟的。
至于"为什么要用感谢您和谢谢您",那就要结合 1.txt 的内容来看了,因为参数%1是1.txt。
建议初学者不要把参数与变量混在一起研究,先一句句set地弄明白了再结合其它的。
@echo off
set str=www.cn-dos.net
echo 替换前变量str的内容为: %str%
echo 执行set str=%%str:www=bbs%%
echo 替换后变量str的内容为: %str:www=bbs%
pause
From the code format, it seems like I wrote it, but now it seems the code is really immature.
As for "why use 'Thank you' and 'Thank you'", that has to be combined with the content of 1.txt to see, because the parameter %1 is 1.txt.
It is suggested that beginners do not study parameters and variables together, first understand each set one by one and then combine with others.
@echo off
set str=www.cn-dos.net
echo The content of variable str before replacement is: %str%
echo Execute set str=%%str:www=bbs%%
echo The content of variable str after replacement is: %str:www=bbs%
pause
|
|
2006-11-15 07:51 |
|
|
xue2ye
初级用户
 
积分 26
发帖 11
注册 2006-11-11
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
set str=%%str:www=bbs%%这句什么意思??
What does the sentence `set str=%%str:www=bbs%%` mean?
|
|
2006-11-15 08:33 |
|
|
zh159
金牌会员
     
积分 3687
发帖 1467
注册 2005-8-8
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
Originally posted by xue2ye at 2006-11-14 20:33:
set str=%%str:www=bbs%%这句什么意思??
这句是显示给你看的,要显示一个%,得%%才行
执行“echo set str=%%str:www=bbs%%”后显示为:set str=%str:www=bbs%
将“ www.cn-dos.net”指派给变量“str”,再用%str:www=bbs%将“ www.cn-dos.net”里的“www”替换为“bbs”
Originally posted by xue2ye at 2006-11-14 20:33:
What does the line "set str=%%str:www=bbs%%" mean?
This line is to show you that to display a %, you need "%%".
After executing "echo set str=%%str:www=bbs%%", it will display: set str=%str:www=bbs%
Assign " www.cn-dos.net" to the variable "str", then use %str:www=bbs% to replace "www" with "bbs" in " www.cn-dos.net"
|
|
2006-11-15 09:47 |
|
|
dshh98
新手上路

积分 3
发帖 2
注册 2006-11-6
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
偶也还没看懂``` 555555555
Oh, I still don't understand it... 555555555
|
|
2006-11-15 13:09 |
|
|
ccwan
金牌会员
     
积分 2725
发帖 1160
注册 2006-9-23 来自 河北廊坊
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
学习不要求新奇,要从基本的东西开始。建议dshh98先把dos基本命令仔细看看,多多实践,再探讨高深的东西。
Learning doesn't require novelty; it should start from basic things. It is suggested that dshh98 first carefully read the basic DOS commands, practice more, and then explore advanced things.
|

三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。 |
|
2006-11-15 21:23 |
|