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-08-11 10:46
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Please clarify the context. The provided "for语句中的delims=" , " is a partial content. Could you provide more relevant information to accurately translate and explain? View 1,516 Replies 7
Original Poster Posted 2006-12-24 06:16 ·  中国 湖南 长沙 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
The following is the translation:

Regarding the part where delims= in the content, if it is delims=\" I know that \ is used as the delimiter, but what does it mean when there is nothing behind it? I can't figure it out. Hope an expert can explain. Also, please explain the red parts for me? Please.
@echo off&setlocal EnableDelayedExpansion
set a=1
for /f "delims=%%i in ('dir /a-d/b/s *.txt') do (
if not "%%~ni"=="%~n0"
!%%~xi"
set/a a+=1
)


Explanation of red parts:
- "delims=": In the for /f command, delims is used to specify the delimiter. When delims is set to an empty value, it means using the default delimiters, which are spaces, tabs, etc.
- "if not "%%~ni"=="%~n0"": It is an if condition judgment. %%~ni is the name of the current loop variable i, and %~n0 is the name of the current batch file without the extension. It is judging whether the name of the current loop variable i is not equal to the name of the current batch file without the extension.
- "%%~ni!a!%%~xi": %%~ni is the name of the current loop variable i without the extension,!a! is the value of variable a, %%~xi is the extension of the current loop variable i. So it is renaming the file to the name without extension + value of variable a + extension.
Floor 2 Posted 2006-12-24 06:28 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline

  Re 26933062 :

  The delims in the for specifies the delimiter. Here, no delimiter is specified, which means no character is a delimiter, and then it means taking the entire content of a line, regardless of any symbols in it.

  For %%~ni, refer to the for help command, which is there.
Floor 3 Posted 2006-12-24 06:36 ·  中国 湖南 长沙 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
lxmxn
Thank you in advance for your answer,
If that's the case then I might as well not write it, right? Like the following content, is there anything wrong with it?
I tried it, the program runs normally, just don't know if it will have problems in some other environment.
Also, for the red part, why use only a single % sign?
@echo off&setlocal EnableDelayedExpansion
set a=1
for /f %%i in ('dir /a-d/b/s *.txt') do (
if not "%%~ni"=="%~n0" (
ren "%%i" "%%~ni!a!%%~xi"
set/a a+=1
)
)
Floor 4 Posted 2006-12-24 08:00 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
20-year member
UID 59080
Status Offline

  Re 26933062 :

  If you remove the delims option, when the file contains symbols such as spaces, the program may not achieve the effect you want or even cause errors.

  The %~n0 in the red part represents the file name of the batch processing itself, so this batch processing itself will not be renamed.
Floor 5 Posted 2006-12-24 11:07 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
In the for /f statement, by default, spaces or tabs (that is, the character produced by pressing the tab key which is 8 spaces) are used as delimiters. If the value of delims is not specified, it means that spaces or tabs are used as delimiters.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 6 Posted 2006-12-24 13:12 ·  中国 湖南 长沙 电信
银牌会员
★★★
Credits 2,268
Posts 879
Joined 2006-12-19 16:23
19-year member
UID 73968
Gender Male
Status Offline
Floor 7 Posted 2009-02-11 03:02 ·  中国 天津 电信
社区乞丐
Credits -46
Posts 25
Joined 2009-02-09 13:16
17-year member
UID 138388
Gender Male
From 内蒙
Status Offline
I sort of understand, but not completely. I'll learn it step by step.
Floor 8 Posted 2009-02-15 22:30 ·  中国 贵州 遵义 电信
初级用户
Credits 69
Posts 34
Joined 2009-02-08 23:47
17-year member
UID 138359
Gender Male
Status Offline
Go to see the example here
http://www.cn-dos.net/forum/viewthread.php?tid=46126&fpage=2
"delims="
The content of the text file is divided into several segments by each line by for. The symbol for segmenting is called a delimiter, and the system default is a space.
delims means define limite to define the boundary. "delims= " means that the delimiter is a space symbol to divide each line into several segments. This is the system default, and at this time, you can not have delims=space, which is also the same. If this sentence is changed to delims=k, each line will be divided into several segments according to the letter k. for by default only captures the first segment of each line into memory for operation.
For example, the content of *.txt is
11 12 13
21 22 23
31 32 33
Then what for captures is
11
21
31
Special note: This for will not capture the entire line of data. It will only capture the first segment of each line! If you want for to capture the entire line of data, you have to add a disruptor, that is, tokens=*
”%%~ni“
Here n comes from the English name, which means name. The ~ symbol represents removing the double quotes. So "%%~ni" means to expand the file name read by parameter %%i. How to expand? For example, if %%i represents the file name 123456.txt, then %%~ni represents: 123456
And %%~xi represents:.txt (x is the meaning of extended)
if not "%%~ni"=="%~n0" is: if the file name obtained from %%~ni from %i is not equal to the file name obtained from %~n0 from %0 (without extension), then what.

[ Last edited by 5yue5 on 2009-2-15 at 22:56 ]
Forum Jump: