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 ]
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 ]
