中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-08-13 04:02
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS学习入门 & 精彩文章 (教学室) » The % issue in batch files<----title added by cn_archer
Printable Version  1,027 / 7
Floor1 hotdog Posted 2003-08-13 00:00
中级用户 Posts 78 Credits 411
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




Floor2 cn_archer Posted 2003-08-13 00:00
元老会员 Posts 991 Credits 2,903 From 福建省
hotdog, please pay attention to the title next time you post!!
Otherwise I'll delete the post directly!
Floor3 Wengier Posted 2003-08-13 00:00
系统支持 Posts 10,521 Credits 27,736
%1 and %1% are actually the same under pure DOS..
Floor4 GOTOmsdos Posted 2003-08-14 00:00
铂金会员 Posts 1,827 Credits 5,154
I originally asked the above question, but why hasn't it been answered?
Floor5 cn_archer Posted 2003-08-14 00:00
元老会员 Posts 991 Credits 2,903 From 福建省
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.
Floor6 willsort Posted 2003-09-29 00:00
元老会员 Posts 1,512 Credits 4,432
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".
Floor7 无妄★模拟 Posted 2003-10-14 00:00
初级用户 Posts 35 Credits 280
This really taught me a lot~~`
Floor8 GOTOmsdos Posted 2003-12-06 00:00
铂金会员 Posts 1,827 Credits 5,154
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
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023