中国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-14 19:25
47,812 topics / 349,917 posts / today 0 new / 48,268 members
DOS批处理 & 脚本技术(批处理室) » Please ask your specific question about the set delay variable so that I can provide the corresponding translation and relevant content.
Printable Version  6,966 / 25
Floor1 xue2ye Posted 2006-11-13 08:53
初级用户 Posts 11 Credits 26
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.
Floor2 lxmxn Posted 2006-11-13 09:31
版主 Posts 4,938 Credits 11,386

  Search in the forum. Keyword: "variable delay".
Floor3 xue2ye Posted 2006-11-13 09:49
初级用户 Posts 11 Credits 26
I've searched, but I still don't understand. Sorry, I'm a bit slow.
Floor4 lxmxn Posted 2006-11-13 10:16
版主 Posts 4,938 Credits 11,386

  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.
Floor5 xue2ye Posted 2006-11-13 11:33
初级用户 Posts 11 Credits 26
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 "谢谢您".
Floor6 lxmxn Posted 2006-11-13 11:43
版主 Posts 4,938 Credits 11,386

   !var:“感谢您”=“谢谢您”!——Replace all "感谢您" in variable var with "谢谢您"!
Floor7 xue2ye Posted 2006-11-15 06:46
初级用户 Posts 11 Credits 26
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?
Floor8 ccwan Posted 2006-11-15 07:06
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
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
Floor9 redtek Posted 2006-11-15 07:42
金牌会员 Posts 1,147 Credits 2,902
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 ]
Floor10 xue2ye Posted 2006-11-15 07:49
初级用户 Posts 11 Credits 26
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.
Floor11 3742668 Posted 2006-11-15 07:51
荣誉版主 Posts 718 Credits 2,013
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.
Floor12 xue2ye Posted 2006-11-15 08:33
初级用户 Posts 11 Credits 26
What does the sentence `set str=%%str:www=bbs%%` mean?
Floor13 zh159 Posted 2006-11-15 09:47
金牌会员 Posts 1,467 Credits 3,687
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"
Floor14 dshh98 Posted 2006-11-15 13:09
新手上路 Posts 2 Credits 3
Oh, I still don't understand it... 555555555
Floor15 ccwan Posted 2006-11-15 21:23
金牌会员 Posts 1,160 Credits 2,725 From 河北廊坊
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.
1 2  Next
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023