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-01 14:44
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Closed: Practical Application of Enhanced FOR Variables in Batch Processing View 1,870 Replies 5
Original Poster Posted 2005-12-15 15:58 ·  中国 天津 河西区 联通
新手上路
Credits 18
Posts 5
Joined 2005-12-15 15:15
20-year member
UID 47196
Gender Male
Status Offline
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 ]
Floor 2 Posted 2005-12-16 13:16 ·  中国 天津 河西区 联通
新手上路
Credits 18
Posts 5
Joined 2005-12-15 15:15
20-year member
UID 47196
Gender Male
Status Offline
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 ]
Floor 3 Posted 2005-12-17 12:31 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 4 Posted 2005-12-17 22:49 ·  中国 天津 河西区 联通
新手上路
Credits 18
Posts 5
Joined 2005-12-15 15:15
20-year member
UID 47196
Gender Male
Status Offline
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?
Floor 5 Posted 2005-12-19 17:49 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
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:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 6 Posted 2005-12-19 19:40 ·  中国 天津 河西区 联通
新手上路
Credits 18
Posts 5
Joined 2005-12-15 15:15
20-year member
UID 47196
Gender Male
Status Offline
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!!
Forum Jump: