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-30 04:39
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Please ask your specific question about the set delay variable so that I can provide the corresponding translation and relevant content. View 6,572 Replies 25
Original Poster Posted 2006-11-13 08:53 ·  中国 浙江 温州 瑞安市 电信
初级用户
Credits 26
Posts 11
Joined 2006-11-11 06:25
19-year member
UID 70205
Gender Male
Status Offline
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.
Floor 2 Posted 2006-11-13 09:31 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  Search in the forum. Keyword: "variable delay".
Floor 3 Posted 2006-11-13 09:49 ·  中国 浙江 温州 瑞安市 电信
初级用户
Credits 26
Posts 11
Joined 2006-11-11 06:25
19-year member
UID 70205
Gender Male
Status Offline
I've searched, but I still don't understand. Sorry, I'm a bit slow.
Floor 4 Posted 2006-11-13 10:16 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

  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.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +1 2006-11-14 23:56
Floor 5 Posted 2006-11-13 11:33 ·  中国 浙江 温州 瑞安市 电信
初级用户
Credits 26
Posts 11
Joined 2006-11-11 06:25
19-year member
UID 70205
Gender Male
Status Offline
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 "谢谢您".
Floor 6 Posted 2006-11-13 11:43 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

   !var:“感谢您”=“谢谢您”!——Replace all "感谢您" in variable var with "谢谢您"!
Floor 7 Posted 2006-11-15 06:46 ·  中国 浙江 温州 瑞安市 电信
初级用户
Credits 26
Posts 11
Joined 2006-11-11 06:25
19-year member
UID 70205
Gender Male
Status Offline
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?
Floor 8 Posted 2006-11-15 07:06 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
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
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Floor 9 Posted 2006-11-15 07:42 ·  中国 北京 朝阳区 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Here is a detailed application example of SET and some related basic content ~ : )
The examples you need are all there. Please do it all according to the code in it from start to finish ~ : )

Simple IndexAbout SET variables, delay variables, usage of various symbols...
http://www.cn-dos.net/forum/viewthread.php?tid=24549&page=1&sid=688Nn2#pid149853

[ Last edited by redtek on 2006-11-15 at 07:43 AM ]
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 10 Posted 2006-11-15 07:49 ·  中国 浙江 温州 瑞安市 电信
初级用户
Credits 26
Posts 11
Joined 2006-11-11 06:25
19-year member
UID 70205
Gender Male
Status Offline
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.
Floor 11 Posted 2006-11-15 07:51 ·  中国 湖北 荆门 电信
荣誉版主
★★★
Credits 2,013
Posts 718
Joined 2006-02-18 07:07
20-year member
UID 50550
Status Offline
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
Floor 12 Posted 2006-11-15 08:33 ·  中国 浙江 温州 瑞安市 电信
初级用户
Credits 26
Posts 11
Joined 2006-11-11 06:25
19-year member
UID 70205
Gender Male
Status Offline
What does the sentence `set str=%%str:www=bbs%%` mean?
Floor 13 Posted 2006-11-15 09:47 ·  中国 广西 玉林 博白县 电信
金牌会员
★★★★
Credits 3,687
Posts 1,467
Joined 2005-08-08 12:00
20-year member
UID 44210
Status Offline
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"
Floor 14 Posted 2006-11-15 13:09 ·  中国 湖北 鄂州 电信
新手上路
Credits 3
Posts 2
Joined 2006-11-06 01:30
19-year member
UID 69727
Gender Male
Status Offline
Oh, I still don't understand it... 555555555
Floor 15 Posted 2006-11-15 21:23 ·  中国 河北 廊坊 三河市 移动
金牌会员
★★★★
Credits 2,725
Posts 1,160
Joined 2006-09-23 12:00
19-year member
UID 63486
From 河北廊坊
Status Offline
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.
三人行,必有吾师焉。 学然后知不足,教然后知困,然后能自强也。
Forum Jump: