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-13 02:19
中国DOS联盟论坛 » DOS学习入门 & 精彩文章 (教学室) » The % issue in batch files<----title added by cn_archer View 1,025 Replies 7
Original Poster Posted 2003-08-13 00:00 ·  中国 福建 福州 仓山区 电信
中级用户
★★
Credits 411
Posts 78
Joined 2003-07-27 00:00
23-year member
UID 7622
Gender Male
Status Offline
What is the relation and difference between %1 and %%1?
What's wrong with %1 in batch files? Please advise!!
1,
@echo off
dir %1 c:\d4.txt
type %1
copy %1 e:\
dir works. But type can't find the file

2,
@echo off
set c:\d4.txt=%1
dir %1
type %1
copy %1 e:\

3,
@echo off
set 1=(or ==) c:\d4.txt
dir %1%
type %1%
copy %1% e:\

4,
@echo off
if exist %1 c:\d4.txt type %1

No response, or the syntax is incorrect

Where is the problem? How should it be written so that both the formal parameter (%1) and the actual parameter (c:\d4.txt, and it should only be written once, no repeated work) can be put into the batch file at the same time, and as long as I enter only the batch filename at the command line it can execute




Floor 2 Posted 2003-08-13 00:00 ·  中国 福建 福州 仓山区 电信
元老会员
★★★
农民
Credits 2,903
Posts 991
Joined 2003-07-23 00:00
23-year member
UID 7391
Gender Male
From 福建省
Status Offline
hotdog, please pay attention to the title next time you post!!
Otherwise I'll delete the post directly!
艰难奋长戟,万古用一夫

中国DOS联盟 http://www.cn-dos.net 欢迎大家来共同学习
我的MSN&E-Mail cn_archer@hotmail.com QQ 56049418
Floor 3 Posted 2003-08-13 00:00 ·  美国 肯塔基州 费耶特县 列克星敦 Charter_Communications
系统支持
★★★★★★
“新DOS时代”站长
Credits 27,736
Posts 10,521
Joined 2002-10-09 12:00
23-year member
UID 9
Status Offline
%1 and %1% are actually the same under pure DOS..
Wengier - 新DOS时代

欢迎大家来到我的“新DOS时代”网站,里面有各类DOS软件和资料,地址:
http://wendos.mycool.net/

E-Mail & MSN: wengierwu AT hotmail.com (最近比较忙,有事请联系DOSroot和雨露,谢谢!)

Floor 4 Posted 2003-08-14 00:00 ·  中国 广东 广州 教育网
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
23-year member
UID 7105
Gender Male
Status Offline
I originally asked the above question, but why hasn't it been answered?
Floor 5 Posted 2003-08-14 00:00 ·  中国 福建 福州 电信
元老会员
★★★
农民
Credits 2,903
Posts 991
Joined 2003-07-23 00:00
23-year member
UID 7391
Gender Male
From 福建省
Status Offline
Everything will get an answer. Unless the moderator was busy at the time, or the post was pushed down very quickly and wasn't seen.
艰难奋长戟,万古用一夫

中国DOS联盟 http://www.cn-dos.net 欢迎大家来共同学习
我的MSN&E-Mail cn_archer@hotmail.com QQ 56049418
Floor 6 Posted 2003-09-29 00:00 ·  中国 山东 烟台 联通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re hotdog:

The sample given earlier was already close to the correct answer; it only needed one more step. Below is the example after I modified it:

::Sample.bat
@echo off
set file=c:\d4.txt
if not exist %file% goto error

dir %file%
type %file%
copy %file% e:\
goto end

:error
echo File %file% not exist.
echo.

:end
set file=

In addition, the following points should be mentioned:

1. Batch files are not a high-level programming language, so there is no such concept as "formal parameters" and "actual parameters"; there are only concepts such as "command-line parameters," "loop variables," and "environment variables." Typical cases are as follows:
%0 %1 %1 ...%9 : command-line parameters; they themselves are also variables
%%a %%b %%c ...%%z: loop variables, used only in FOR command statements, case-sensitive; when used on the command line, only a single percent sign % is used;
%temp% %path%...: environment variables, equivalent to global variables in high-level languages; this is the form used when referencing their values, and when setting their values there is no need to add double %%, and they are case-insensitive;

2. In program 1:
dir %1 c:\d4.txt
there is a syntax error, because dir only parses one valid filename parameter, so when this program is run with a parameter, it will get an incorrect result. The other statements have no syntax errors, but since a valid filename is required as a parameter at runtime, it does not meet the requirement of the question.

3. In program 2
set c:\d4.txt=%1
although there is no syntax problem, it still goes against the author's original intention. Because its assignment order is reversed—it assigns a constant to a variable, not a variable to a constant. Also, I personally do not recommend including non-letter characters such as ": . \" in environment variable names, because this can easily cause misunderstanding of program statements.

4. Program 3
set 1=c:\d4.txt
wants to assign the filename to an environment variable, but you should avoid using a single digit as an environment variable name, because its namespace conflicts with command-line parameters, and when COMMAND interprets it, it will give priority to interpreting ambiguous usages like %1%, %2% as command-line parameters.
Also, the usage set 1==c:\d4.txt is incorrect, because set handles the equals sign in the command line in a rather special way; unlike other internal commands, it does not ignore it together with spaces.

5. Program 4 still does not solve the essential problem:
in the line if exist %1 c:\d4.txt type %1, the position of %1 is somewhat awkward, because %1 was introduced by the author as a filename, but then another filename is specified after it, which directly leads to the following result: if %1 is empty, then when type %1 is executed it will prompt that a required parameter is missing ("Required parameter missing"
); if %1 is not empty, then C:\d4.txt will be executed as a command, and the result is obviously "Bad command or file name".
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 7 Posted 2003-10-14 00:00 ·  中国 吉林 四平 中移铁通
初级用户
蝎蛇统领
Credits 280
Posts 35
Joined 2003-05-21 00:00
23-year member
UID 2294
Gender Male
Status Offline
This really taught me a lot~~`
我是菜鸟,我要变成老鸟!
www.10.164.63.101.com
Floor 8 Posted 2003-12-06 00:00 ·  中国 广东 广州 教育网
铂金会员
★★★★
C++启程者
Credits 5,154
Posts 1,827
Joined 2003-07-18 00:00
23-year member
UID 7105
Gender Male
Status Offline
One more addition: in for /f "eol= skip= tokens= delims="等 do command, besides using consecutive letters like %a %b %c, you can also use consecutive numbers like %1 %2 %3
Forum Jump: