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:请访问
批处理编程的异类 ,欢迎交流与共享批处理编程心得!