可执行文件的参数可以直接在注释文件中指定SFX模块的参数。
这是一个演示此技术的示例批处理文件:
@echo off
cd /D "%TEMP%"
rem Create the file for the SFX module with the SFX options.
echo ;The comment below contains SFX script commands.>TestSetup.txt
echo.>>TestSetup.txt
echo Setup=Test.bat Switch "One more parameter">>TestSetup.txt
echo Overwrite=1>>TestSetup.txt
echo Title=Test Installation>>TestSetup.txt
echo Text>>TestSetup.txt
echo {>>TestSetup.txt
echo ^<font face='Arial'^>An SFX test which just shows how SFX module runs the installer.^<br^>^<br^>Just click on button Install or hit RETURN.^</font^>>>TestSetup.txt
echo }>>TestSetup.txt
rem Create the batch file executed by SFX archive.
echo @echo %%0 %%*>Test.bat
echo @pause>>Test.bat
echo @del %%0 ^>nul>>Test.bat
rem Create the SFX archive.
RAR.exe a -sfx -c -zTestSetup.txt TestSetup.exe Test.bat
rem Delete the created batch and comment file.
del Test.bat
del TestSetup.txt
rem Run the self-extracting archive. User has to press only RETURN.
start /wait TestSetup.exe
rem Delete the self-extracting archive.
:DeleteLoop
del TestSetup.exe >nul
if exist TestSetup.exe goto DeleteLoop
此批处理文件首先在临时文件目录中创建文本文件 TestSetup.txt,其内容为:
;The comment below contains SFX script commands.
Setup=Test.bat Switch "One more parameter"
Overwrite=
Title=Test Installation
Text
{
<font face='Arial'>An SFX test which just shows how SFX module runs the installer.<br><br>Just click on button Install or hit RETURN.</font>
}
对您来说重要的是以Setup= 开头的行。
-
Test.bat 是解压后要执行的文件。
-
Switch 是作为第一个参数传递给Test.bat 的选项。
-
"One more parameter" 是第二个参数,其中包含空格以传递给 Test.bat,因为有空格,所以必须用双引号括起来。
接下来批处理文件继续创建 Test.bat 内容:
@echo %0 %*
@pause
@del %0 >nul
这个小批处理文件只是在第一行输出它是如何被 SFX 存档调用的,接下来等待用户击键并最后删除自己。因此,将批处理文件提取到哪个目录并不重要。默认为当前目录,即临时文件所在的目录。
然后批处理文件创建 SFX 存档 TestSetup.exe。所用开关的详细信息请参见WinRAR的程序文件目录中的Rar.txt。
请注意,Rar.exe 行只有在 WinRAR 的程序文件目录包含在环境变量 PATH 中或 Windows 找不到 @ 时才有效987654332@ 是 WinRAR 的控制台版本。将完整路径的行修改为双引号中的 Rar.exe,以使批处理文件的这一行独立于 PATH 中包含的目录。
自解压 RAR 存档创建后,文件 Test.bat 和 TestSetup.txt 将被删除,不再需要。
现在创建的 SFX 存档 TestSetup.exe 被调用,在按 RETURN 键后,您会看到 Test.bat 是使用 Test.bat 中指定的 2 个参数调用的strong>TestSetup.txt。
批处理文件最后也删除创建的 SFX 存档。