Originally posted by gool123456 at 2010-3-9 23:42:
Thanks very much to Hanyeguxing for the enthusiastic reply!
So, what do %1 and %todir% refer to respectively, and what is %1 used for here?
Because I'm not very familiar with %mun parameters...
Using batch parameters:
hh.exe C:\WINDOWS\Help\ntcmds.chm::/percent.htm
Using Shift
hh.exe C:\WINDOWS\Help\ntcmds.chm::/shift.htm
You can use batch parameters anywhere within a batch file to extract information about environment settings.
Cmd.exe provides extended batch parameter variables (%0 to %9). When batch parameters are used in a batch file, %0 is replaced by the batch file name, while %1 to %9 are replaced by the corresponding arguments typed on the command line. To access arguments greater than %9, you must use the shift command. For detailed information about the Shift command, see Shift. The %* batch parameter is a wildcard that refers to all arguments (excluding %0) passed to the batch file.
For example, to copy the contents of Folder1 to Folder2, where %1 and %2 are replaced respectively by the values Folder1 and Folder2, type the following in the batch file Mybatch.bat:
xcopy %1\*.* %2
To run the file, type:
mybatch.bat C:\folder1 D:\folder2
This is equivalent to typing the following in the batch file:
xcopy C:\folder1 \*.* D:\folder2
You can also use modifiers in batch parameters. Modifiers expand batch parameters into partial or complete file or directory names using current drive and directory information. To use a modifier, type a percent sign (%) character, followed by a tilde (~) character, and then the appropriate modifier (that is, %~modifier).
The following table lists the modifiers that can be used in expansions.
Modifier Description
%~1 Expands %1 and removes any quotation marks ("").
%~f1 Expands %1 to a fully qualified path name.
%~d1 Expands %1 to a drive letter.
%~p1 Expands %1 to a path.
%~n1 Expands %1 to a file name.
%~x1 Expands %1 to a file extension.
%~s1 The expanded path contains short names only.
%~a1 Expands %1 to file attributes.
%~t1 Expands %1 to file date/time.
%~z1 Expands %1 to file size.
%~$PATH:1 Searches the directories listed in the PATH environment variable and expands %1 to the fully qualified name of the first one found. If the environment variable name is not defined, or the file is not found, this modifier expands to an empty string.
The following table lists possible combinations of modifiers and qualifiers that can be used to get more complex results:
Modifier Description
%~dp1 Expands %1 to the drive letter and path.
%~nx1 Expands %1 to the file name and extension.
%~dp$PATH:1 Searches the directories listed in the PATH environment variable for %1 and expands it to the drive letter and path of the first one found.
%~ftza1 Expands %1 to an output line similar to dir.
Note
In the above examples, you can use other batch parameters in place of %1 and PATH.
The %* modifier is the only modifier that can represent all arguments passed to the batch file. This modifier cannot be used together with the %~ modifier. The %~ syntax must be terminated by a valid argument value.
Batch parameters cannot be used in the same way as environment variables. You cannot search or replace values, or check substrings. However, you can assign a parameter to an environment variable, and then use that environment variable.
Shift changes the position of batch parameters in a batch file.
Syntax
shift
Parameters
None
Remarks
Use the shift command-line option with command extensions
When command extensions are enabled (the default setting), the shift command supports the /n command-line option, which tells the command to begin shifting at the nth argument, where n can be any value from 0 to 8. For example,
SHIFT /2
can change %3 to %2, %4 to %3, and so on, while leaving %0 and %1 unchanged.
How the shift command works
The shift command changes the values of batch parameters %0 to %9 by copying each parameter into the previous parameter. That is, the value of %1 is copied to %0, the value of %2 is copied to %1, and so on. This command is very useful when writing batch files that perform the same operation on any number of arguments.
Using more than 10 batch parameters
You can also use the shift command to create batch files that can accept more than 10 batch parameters. If more than 10 arguments are specified on the command line, the arguments after the tenth (%9) parameter are shifted into %9 one at a time.
Using %* together with shift
Shift has no effect on the %* batch parameter.
Restoring arguments
There is no reverse shift command. Once a shift command has been carried out, you cannot restore the first batch parameter (%0) that existed before the change.
Example
The following batch file Mycopy.bat shows how to use the shift command with any number of batch parameters. This batch file copies a list of files to a specific directory. The batch parameters are represented by the directory and file name arguments.
@echo off
rem MYCOPY.BAT copies any number of files
rem to a directory.
rem The command uses the following syntax:
rem mycopy dir file1 file2 ...
set todir=%1
:getfile
shift
if "%1"=="" goto end
copy %1 %todir%
goto getfile
:end
set todir=
echo All done
[
Last edited by Hanyeguxing on 2010-3-9 at 23:48 ]