|
pkuang
新手上路

积分 18
发帖 5
注册 2005-12-15
状态 离线
|
『楼 主』:
已结:批处理中增强的FOR 变量的实际运用
使用 LLM 解释/回答一下
在批处理中,很多都会用到FOR 变量.看到MS的官方帮助文件后,想用for的增强变量来定义路径,但没能成功.请大家共同研究一下.
如何给一个完整的路径定义变量?以下是官方文件的解释:
FOR 变量参照的替换已被增强。您现在可以使用下列
选项语法:
~I - 删除任何引号("),扩充 %I
%~fI - 将 %I 扩充到一个完全合格的路径名
%~dI - 仅将 %I 扩充到一个驱动器号
%~pI - 仅将 %I 扩充到一个路径
%~nI - 仅将 %I 扩充到一个文件名
%~xI - 仅将 %I 扩充到一个文件扩展名
%~sI - 扩充的路径只含有短名
%~aI - 将 %I 扩充到文件的文件属性
%~tI - 将 %I 扩充到文件的日期/时间
%~zI - 将 %I 扩充到文件的大小
%~$PATH:I - 查找列在路径环境变量的目录,并将 %I 扩充
到找到的第一个完全合格的名称。如果环境变量
未被定义,或者没有找到文件,此组合键会扩充
空字符串
可以组合修饰符来得到多重结果:
%~dpI - 仅将 %I 扩充到一个驱动器号和路径
%~nxI - 仅将 %I 扩充到一个文件名和扩展名
%~fsI - 仅将 %I 扩充到一个带有短名的完整路径名
%~dp$PATH:i - 查找列在路径环境变量的目录,并将 %I 扩充
到找到的第一个驱动器号和路径。
%~ftzaI - 将 %I 扩充到类似输出线路的 DIR
在以上例子中,%I 和 PATH 可用其他有效数值代替。%~ 语法
用一个有效的 FOR 变量名终止。选取类似 %I 的大写变量名
比较易读,而且避免与不分大小写的组合键混淆。
===============================================
我在实验中的例子:
条件:假设我们不知道以下要替换的文件在哪里
目的:通过批处理替换原文件
思路:
用dir命令找到文件的路径是 x:\文件\音乐\征服.wmv 并输出文本 1.txt
然后写两个.bat程序
程序1 (1.bat)
@echo off
for /f "tokens=1 delims=xxx" %~pF in (1.txt) do start call 2.bat %~pF
程序2 (2.bat)
@echo off
copy 征服.mp3 %~pF
解释:
程序1是通过 1.txt来获取文件的路径,定义变量后传到程序2的变量中,然后执行程序2后拷贝另一文件到原来那个路径下替换原文件
问题:
1.这样定义路径变量对吗?(反正我没成功)
2.应该如何定义路径变量?
3.delims=xxx的含义是- 指分隔符集。这个替换了空格和跳格键的
默认分隔符集。如果输出文本内容是一行一个,那么回车键(换行键)的分隔符是什么?
以上是我的问题,还请大家帮忙看看,不胜感激!
Last edited by willsort on 2005-12-29 at 14:48 ]
In batch processing, many use FOR variables. After seeing the official MS help file, I wanted to define a path using the enhanced variables of for but couldn't succeed. Let's study together.
How to define a variable for a complete path? The following is the explanation from the official file:
The substitution of FOR variable references has been enhanced. You can now use the following option syntax:
~I - Remove any quotation marks ("), expand %I
%~fI - Expand %I to a fully qualified pathname
%~dI - Expand %I to only a drive letter
%~pI - Expand %I to only a path
%~nI - Expand %I to only a file name
%~xI - Expand %I to only a file extension
%~sI - The expanded path only contains the short name
%~aI - Expand %I to the file attributes of the file
%~tI - Expand %I to the date/time of the file
%~zI - Expand %I to the size of the file
%~$PATH:I - Search the directories listed in the path environment variable and expand %I to the first fully qualified name found. If the environment variable is not defined or the file is not found, this combination will expand to an empty string
You can combine modifiers to get multiple results:
%~dpI - Expand %I to only a drive letter and path
%~nxI - Expand %I to only a file name and extension
%~fsI - Expand %I to a full pathname with a short name only
%~dp$PATH:i - Search the directories listed in the path environment variable and expand %I to the first drive letter and path
%~ftzaI - Expand %I to a DIR-like output line
In the above examples, %I and PATH can be replaced with other valid values. The %~ syntax ends with a valid FOR variable name. Choosing an uppercase variable name like %I is easier to read and avoids confusion with case-insensitive combination keys.
===============================================
My example in the experiment:
Condition: Suppose we don't know where the following file to be replaced is
Purpose: Replace the original file through batch processing
Idea:
Use the dir command to find the path of the file as x:\Files\Music\Conquer.wmv and output to text 1.txt
Then write two.bat programs
Program 1 (1.bat)
@echo off
for /f "tokens=1 delims=xxx" %~pF in (1.txt) do start call 2.bat %~pF
Program 2 (2.bat)
@echo off
copy Conquer.mp3 %~pF
Explanation:
Program 1 gets the path of the file through 1.txt, defines the variable and passes it to the variable in program 2, then executes program 2 to copy another file to the original path to replace the original file
Problems:
1. Is defining the path variable like this correct? (Anyway, I didn't succeed)
2. How should the path variable be defined?
3. The meaning of delims=xxx is - refers to the set of delimiters. This replaces the default set of delimiters of spaces and tabs. If the content of the output text is one line at a time, then what is the delimiter of the enter key (newline key)?
The above are my problems. Please help me, thank you very much!
Last edited by willsort on 2005-12-29 at 14:48 ]
|
|
2005-12-15 15:58 |
|
|
pkuang
新手上路

积分 18
发帖 5
注册 2005-12-15
状态 离线
|
『第 2 楼』:
批处理如何定义多重路径的方法
使用 LLM 解释/回答一下
假设在我们的磁盘上有两个相同文件 征服.WMV,但文件路径不同,一个是在D:\音乐\MP3\征服.WMV,另一个是在E:\我最心爱的音乐\MP3\征服.WMV,然后我们用DIR命令搜索以上两个文件,找到路径后,并输出为文本 1.TXT,此时文本里的内容如下:
E:\我最心爱的音乐\MP3\征服.WMV (注意:是一行一个路径)
D:\音乐\MP3\征服.WMV
目的:
用批处理功能自动把以上两个路径里的 征服.WMV 替换为 征服.MP3
问题:
我定义后只能自动替换掉第二行这个D:\音乐\MP3\征服.WMV,不能替换掉E:\我最心爱的音乐\MP3\征服.WMV,而如果我把第二行D:\音乐\MP3\征服.WMV这行删除掉倒是可以替换E路径里的那个文件,看来还是路径的定义问题...那么如何用批处理定义以上两个文件的路径呢??
我只是想研究一下多重路径在批处理里如何定义的问题,本人很菜,还望大家多多帮忙,谢谢!!
—————————————— willsort 版务记录 ——————————————
合并主题:{18258}批处理中增强的FOR 变量的实际运用
操作原因:二主题存在上下文的直接联系
—————————————— willsort 版务记录 ——————————————
Last edited by willsort on 2005-12-17 at 12:38 ]
Suppose there are two identical files on our disk, "Conquer.WMV", but the file paths are different. One is at D:\Music\MP3\Conquer.WMV, and the other is at E:\My Beloved Music\MP3\Conquer.WMV. Then we use the DIR command to search for the above two files, find the paths, and output them to text 1.TXT. At this time, the content in the text is as follows:
E:\My Beloved Music\MP3\Conquer.WMV (Note: One path per line)
D:\Music\MP3\Conquer.WMV
Purpose:
Use batch processing functions to automatically replace "Conquer.WMV" in the above two paths with "Conquer.MP3".
Problem:
After I define it, I can only automatically replace the second line D:\Music\MP3\Conquer.WMV, and cannot replace the E:\My Beloved Music\MP3\Conquer.WMV. And if I delete the second line D:\Music\MP3\Conquer.WMV, I can replace the file in the E path. It seems that it is still a problem with the definition of the path... Then how to define the paths of the above two files in batch processing??
I just want to study the problem of how to define multiple paths in batch processing. I am very inexperienced. I hope everyone can help a lot. Thank you!!
—————————————— willsort Moderation Record ——————————————
Merge Topic: {18258} Practical Application of Enhanced FOR Variables in Batch Processing
Operation Reason: The two topics have direct context connections
—————————————— willsort Moderation Record ——————————————
Last edited by willsort on 2005-12-17 at 12:38 ]
|
|
2005-12-16 13:16 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
Re pkuang:
你的问题主要出现在 2.bat 中,在一个批处理程序中是不能引用其它批处理程序中的 for 变量的,这里应该将 %~pF 改为 %1。
另外,以上的所有步骤使用一句代码即可完成,在新文件所在的路径下,命令行环境下执行以下代码,应该可以完成你的目的。未经测试,若有问题请继续回帖讨论。
for /r X:\ %p in ("征服.wmv") do copy 征服.wmv %pp > nul
Re pkuang:
Your problem mainly occurs in 2.bat. In a batch program, you cannot reference for variables from other batch programs. Here, you should change %~pF to %1.
Additionally, all the above steps can be completed with one line of code. In the command line environment under the path where the new file is located, execute the following code, which should achieve your purpose. Not tested, please continue to post and discuss if there are any issues.
for /r X:\ %p in ("Conquer.wmv") do copy Conquer.wmv %pp > nul
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-12-17 12:31 |
|
|
pkuang
新手上路

积分 18
发帖 5
注册 2005-12-15
状态 离线
|
『第 4 楼』:
使用 LLM 解释/回答一下
Originally posted by willsort at 2005-12-17 12:31:
Re pkuang:
你的问题主要出现在 2.bat 中,在一个批处理程序中是不能引用其它批处理程序中的 for 变量的,这里应该将 %~pF 改为 %1。
另外@...
首先谢谢版主的热心,因为我的表达能力有限,所以没有把问题阐述清楚,请见谅..其实我是想从搜索文件到替换文件完全用批处理自动完成,不需要人工干预,只是把替换文件和批处理程序放在一个目录里,这样一来,有几个问题还不能解决,如下:
1,在您的解释里说用一句命令就可以完成上面的操作,那么,路径不用定义吗?
2,如果像我在第二个帖子里说的,相同文件在不同的路径里,在已经自动搜索到这两个文件的前提下(并输出文件路径的文本文件 1.txt ), 如何实现用新文件自动替换掉不同路径里的旧文件?
以上我都想用批处理自动完成,现在自动搜索可以完成,也可以替换掉文件,但不能替换不同路径里的文件,也就是说,尽管找到了两个文件的路径,但我定义的路径只有一个起作用,第二个路径定义后不起作用. 那么,应该如何定义才能使多重路径生效呢?
Originally posted by willsort at 2005-12-17 12:31:
Re pkuang:
Your problem mainly occurs in 2.bat. In a batch program, you cannot reference for variables in other batch programs. Here, %~pF should be changed to %1.
Also @...
First of all, thank you to the moderator for your enthusiasm. Because my expression ability is limited, so I didn't explain the problem clearly, please forgive me.. Actually, I want to completely use batch processing to automatically complete from searching files to replacing files, without manual intervention. Just put the replacement file and the batch program in one directory. In this way, there are several problems that cannot be solved, as follows:
1, In your explanation, you said that one command can complete the above operation. Then, does the path not need to be defined?
2, If, as I said in the second post, the same file is in different paths, and on the premise that these two files have been automatically searched (and the text file 1.txt of the file path is output), how to realize using the new file to automatically replace the old files in different paths?
The above I all want to use batch processing to automatically complete. Now the automatic search can be completed, and the file can be replaced, but the files in different paths cannot be replaced. That is to say, although the paths of the two files are found, only one of the defined paths works, and the second path definition does not work. Then, how should the definition be made to make multiple paths take effect?
|
|
2005-12-17 22:49 |
|
|
willsort
元老会员
         Batchinger
积分 4432
发帖 1512
注册 2002-10-18
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
Re pkuang:
抱歉,因为上网时间限制,所以迟迟无法回复你的主题。
我理解了你的意图,而你似乎尚未理解我的代码。深究你的问题,似乎不应归结到如何将定义和获取多重路径之上,而应归结为如何逐行处理生成文件中的路径,这样只需要使用 for /f 就可以完成任务了。当然,就我来看,这个生成文件不是必须的。
另外,我注意到你的要替换的源文件和目标文件的扩展名并不相同,不知你是想改名,还是拷贝新文件后删除原文件?
下面是批处理代码示例,将此保存为批处理,将之与要替换的源文件以及1.txt放在一起执行它试试看。代码未经测试,请谨慎使用!
for /f "delims=" %%f in (1.txt) do copy %%~nf.mp3 %%~dpnf.mp3>nul
Re pkuang:
Sorry, due to time limits for Internet access, I have been unable to reply to your topic for a long time.
I understand your intention, but it seems you haven't understood my code yet. Delving into your problem, it shouldn't be attributed to how to define and obtain multiple paths, but rather to how to process the paths in the generated file line by line. In this way, you can complete the task just by using for /f. Of course, from my perspective, this generated file is not necessary.
In addition, I noticed that the extension of the source file and the target file you want to replace are not the same. I wonder if you want to rename or copy the new file and then delete the original file?
The following is an example of a batch code. Save this as a batch file and execute it together with the source file to be replaced and 1.txt. The code is not tested, please use it with caution!
for /f "delims=" %%f in (1.txt) do copy %%~nf.mp3 %%~dpnf.mp3>nul
|

※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得! |
|
2005-12-19 17:49 |
|
|
pkuang
新手上路

积分 18
发帖 5
注册 2005-12-15
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
果然是高手啊,我经过实验成功了,谢谢willsort版主的帮助....
版主运用的就是for变量的增强命令吧,我一直没能搞懂这些增强命令的运用和定义方法.
我搜索了好多关于批处理的教程,但没有一个能够详细讲解这个增强命令的用法,大多都是把官方的解释搬到教程里,对于我们这些刚刚接触批处理的人群是很不容易理解的.
版主:有时间一定要写一篇关于for的增强命令的用法啊!!尤其是在实际当中的运用!!
再次感谢您的热心解答和帮助,谢谢!!
Sure enough, it's a master! I succeeded through experiments, thank you, moderator willsort for your help....
The moderator used the enhanced command of the for variable, I have been unable to figure out the use and definition methods of these enhanced commands.
I searched many tutorials on batch processing, but none of them can explain the usage of this enhanced command in detail. Most of them just copy the official explanation into the tutorial, which is not easy for us who are just getting in touch with batch processing.
Moderator: You must write an article about the usage of the enhanced command of for when you have time!! Especially the application in practice!!
Thank you again for your enthusiastic answer and help, thank you!!
|
|
2005-12-19 19:40 |
|
|