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-07-02 09:39
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Question for the experts: what does this piece of code mean? View 795 Replies 7
Original Poster Posted 2010-03-09 15:40 ·  中国 广东 广州 电信
初级用户
★★
Credits 89
Posts 76
Joined 2009-12-13 13:41
16-year member
UID 156499
Gender Male
Status Offline
@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

I really don't understand the red part of the code. I hope an expert can explain it in detail, thanks!
Floor 2 Posted 2010-03-09 15:43 ·  中国 广东 广州 电信
初级用户
★★
Credits 89
Posts 76
Joined 2009-12-13 13:41
16-year member
UID 156499
Gender Male
Status Offline
Floor 3 Posted 2010-03-09 19:42 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
:getfile and goto getfile form a loop
if "%1"=="" goto end and :end set the condition and label for breaking out of the loop
copy %1 %todir% does the copy operation
set todir=%1 assigns the parameter to the variable
The shift command in the loop copies each parameter into the previous parameter, that is, during the loop, %1 changes while %todir% stays the same, thus making it possible to copy a list of files to one fixed directory

[ Last edited by Hanyeguxing on 2010-3-9 at 19:45 ]
Floor 4 Posted 2010-03-09 23:33 ·  中国 广东 广州 电信
初级用户
★★
Credits 89
Posts 76
Joined 2009-12-13 13:41
16-year member
UID 156499
Gender Male
Status Offline
Thanks very much for the reply in post #2!
What I want to ask is: what do %1 and %todir% refer to respectively, and what is %1 used for here?
Floor 5 Posted 2010-03-09 23:42 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
Originally posted by gool123456 at 2010-3-9 23:33:
Thanks very much for the reply in post #2!
What I want to ask is: what do %1 and %todir% refer to respectively, and what is %1 used for here?

% are parameters passed in order. Whatever comes after MYCOPY.BAT becomes the parameters...
For example: MYCOPY.BAT 2.txt 2.txt 3.txt 4.txt

[ Last edited by Hanyeguxing on 2010-3-9 at 23:44 ]
Floor 6 Posted 2010-03-09 23:42 ·  中国 广东 广州 电信
初级用户
★★
Credits 89
Posts 76
Joined 2009-12-13 13:41
16-year member
UID 156499
Gender Male
Status Offline
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...
Floor 7 Posted 2010-03-09 23:46 ·  中国 吉林 延边朝鲜族自治州 延吉市 电信
银牌会员
★★★
正在学习中的菜鸟...
Credits 1,039
Posts 897
Joined 2009-03-01 15:34
17-year member
UID 140302
Gender Male
From 在地狱中仰望天堂
Status Offline
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 ]
Floor 8 Posted 2010-03-09 23:47 ·  中国 广东 广州 电信
初级用户
★★
Credits 89
Posts 76
Joined 2009-12-13 13:41
16-year member
UID 156499
Gender Male
Status Offline
Once Hanyeguxing explained it like that, I understand what shift does in this batch file now! Thanks.
Forum Jump: