Inno Setup 执行批处理文件,但不允许它创建其 .txt 文件
Inno Setup executes batch file but it doesn't let it create its .txt file
我的安装程序依次执行我创建的两个 bat 文件,即批处理文件 运行,但它们没有创建它们应该创建的两个 .txt
文件。当我手动执行它们时,它们完美地完成了它们的工作,但是从 Inno Setup 中它们只是在屏幕上闪烁了一会儿,并没有创建它们应该创建的 txt 文件。
这是我的代码,请帮忙。我已经在互联网上搜索了解决方案,但我还没有找到。
[Code]
function InitializeSetup(): boolean;
var
ResultCode: integer;
begin
// this bat file creates 2 txt files with the hardware id, one is permanent one is temporary
Exec(ExpandConstant('{src}\gethardwareid_win.bat'), '', '', SW_SHOW,
ewWaitUntilTerminated, ResultCode) ;
// this one verify if the 2 files are the same, if they are it creates an answer.txt
Exec(ExpandConstant('{src}\verify.bat'), '', '', SW_SHOW,
ewWaitUntilTerminated, ResultCode) ;
//if the answer.txt file exists it means the two collected hardware id are the same and it continues installatin, if not it aborts it
if FileExists(ExpandConstant('{src}\answer.txt')) then
begin
// handle success if necessary; ResultCode contains the exit code
MsgBox('correct', mbCriticalError, MB_OK);
Result:=True
end
else
begin
// handle failure if necessary; ResultCode contains the error code
MsgBox('this is not the licensed computer', mbCriticalError, MB_OK);
Result:=False
end;
// Proceed Setup
end;
批处理文件 1) gethardwareid_win.bat
@echo off
@rem
@rem HelpSystems hardware ID discovery for Windows
@rem
@rem setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set outfile=out.txt
set verify= verify.txt
set internfile=1
set "indent= "
if NOT "x%1" == "x" (
set outfile=%1
set internfile=0
)
set removedStr=%outfile:~1,-1%
set computername=
for /f "delims=" %%a in ('hostname') do @set computername=%%a
@rem delete the old output file if existed
if exist %outfile% del %outfile%
echo Hardware ID(s) for computer:
echo Hardware ID(s) for computer: >> %outfile%
@rem run "getmac" command to get all the Mac addresses
SET count=0
for /f "tokens=1 delims=," %%a in ('"getmac /NH"') do (
if NOT "x%%a"=="x" (
if NOT "%%a"=="N/A " (
call :formatHardwareId %%a
set /a count+=1
)
)
)
@rem copy to clipboard
@rem echo Found IDs: %count%
if count gtr 0 (
clip < %outfile%
) else (
echo No hardware ID found.
)
echo.
@rem remove out file if not specified externally
@rem ENDLOCAL
@rem ---------------------------------------------------------
if NOT "x%1" == "x" (
set verify=%1
set internfile=0
)
set removedStr=%verify:~1,-1%
set computername=
for /f "delims=" %%a in ('hostname') do @set computername=%%a
@rem delete the old output file if existed
if exist %verify% (goto:eof)
else( echo Hardware ID(s) for computer:
echo Hardware ID(s) for computer: >> %verify%
@rem run "getmac" command to get all the Mac addresses
SET count=0
for /f "tokens=1 delims=," %%a in ('"getmac /NH"') do (
if NOT "x%%a"=="x" (
if NOT "%%a"=="N/A " (
call :formatHardwareId1 %%a
set /a count+=1
)
)
)
@rem copy to clipboard
@rem echo Found IDs: %count%
if count gtr 0 (
clip < %verify%
) else (
echo No hardware ID found.
)
echo.
@rem remove out file if not specified externally
@rem ENDLOCAL
goto:eof)
@rem ---------------------------------------------------------
@rem ---------------------------------------------------------
@rem function to format the Hardware ID
@rem ---------------------------------------------------------
:formatHardwareId
SETLOCAL ENABLEDELAYEDEXPANSION
set localmac=%1
@rem set localmac=%localmac:~1,-1%
set localmac=%localmac:-=:%
if NOT "x%1"=="x" (
@rem echo local: %localmac%
if NOT "%localmac%"=="%removedStr%" (
if NOT "%localmac%"=="N/A" (
echo %indent% W-%localmac%
echo %indent% W-%localmac% >> %outfile%
)
)
)
ENDLOCAL
exit /b
@rem
:formatHardwareId1
SETLOCAL ENABLEDELAYEDEXPANSION
set localmac=%1
@rem set localmac=%localmac:~1,-1%
set localmac=%localmac:-=:%
if NOT "x%1"=="x" (
@rem echo local: %localmac%
if NOT "%localmac%"=="%removedStr%" (
if NOT "%localmac%"=="N/A" (
echo %indent% W-%localmac%
echo %indent% W-%localmac% >> %verify%
)
)
)
ENDLOCAL
exit /b
@rem ---------------------------------------------------------
第二个批处理文件2)verify.bat
@echo off
set ans=answer.txt
if exist %ans% del %ans%
for /f "Delims=" %%a in (out.txt) do (
set TEXT=%%a
)
for /f "Delims=" %%a in (verify.txt) do (
set TEXT1=%%a
)
if %text1%==%TEXT% echo 1 > answer.txt
您在批处理文件中使用了相对路径并且没有指定批处理文件的启动目录。这是非常不可靠的方法。
文件可能在某个临时文件夹中创建(如果创建的话)。
要调试批处理文件,删除 @echo off
并在批处理文件末尾添加 pause
。这允许您检查批处理文件输出。
我的安装程序依次执行我创建的两个 bat 文件,即批处理文件 运行,但它们没有创建它们应该创建的两个 .txt
文件。当我手动执行它们时,它们完美地完成了它们的工作,但是从 Inno Setup 中它们只是在屏幕上闪烁了一会儿,并没有创建它们应该创建的 txt 文件。
这是我的代码,请帮忙。我已经在互联网上搜索了解决方案,但我还没有找到。
[Code]
function InitializeSetup(): boolean;
var
ResultCode: integer;
begin
// this bat file creates 2 txt files with the hardware id, one is permanent one is temporary
Exec(ExpandConstant('{src}\gethardwareid_win.bat'), '', '', SW_SHOW,
ewWaitUntilTerminated, ResultCode) ;
// this one verify if the 2 files are the same, if they are it creates an answer.txt
Exec(ExpandConstant('{src}\verify.bat'), '', '', SW_SHOW,
ewWaitUntilTerminated, ResultCode) ;
//if the answer.txt file exists it means the two collected hardware id are the same and it continues installatin, if not it aborts it
if FileExists(ExpandConstant('{src}\answer.txt')) then
begin
// handle success if necessary; ResultCode contains the exit code
MsgBox('correct', mbCriticalError, MB_OK);
Result:=True
end
else
begin
// handle failure if necessary; ResultCode contains the error code
MsgBox('this is not the licensed computer', mbCriticalError, MB_OK);
Result:=False
end;
// Proceed Setup
end;
批处理文件 1) gethardwareid_win.bat
@echo off
@rem
@rem HelpSystems hardware ID discovery for Windows
@rem
@rem setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
set outfile=out.txt
set verify= verify.txt
set internfile=1
set "indent= "
if NOT "x%1" == "x" (
set outfile=%1
set internfile=0
)
set removedStr=%outfile:~1,-1%
set computername=
for /f "delims=" %%a in ('hostname') do @set computername=%%a
@rem delete the old output file if existed
if exist %outfile% del %outfile%
echo Hardware ID(s) for computer:
echo Hardware ID(s) for computer: >> %outfile%
@rem run "getmac" command to get all the Mac addresses
SET count=0
for /f "tokens=1 delims=," %%a in ('"getmac /NH"') do (
if NOT "x%%a"=="x" (
if NOT "%%a"=="N/A " (
call :formatHardwareId %%a
set /a count+=1
)
)
)
@rem copy to clipboard
@rem echo Found IDs: %count%
if count gtr 0 (
clip < %outfile%
) else (
echo No hardware ID found.
)
echo.
@rem remove out file if not specified externally
@rem ENDLOCAL
@rem ---------------------------------------------------------
if NOT "x%1" == "x" (
set verify=%1
set internfile=0
)
set removedStr=%verify:~1,-1%
set computername=
for /f "delims=" %%a in ('hostname') do @set computername=%%a
@rem delete the old output file if existed
if exist %verify% (goto:eof)
else( echo Hardware ID(s) for computer:
echo Hardware ID(s) for computer: >> %verify%
@rem run "getmac" command to get all the Mac addresses
SET count=0
for /f "tokens=1 delims=," %%a in ('"getmac /NH"') do (
if NOT "x%%a"=="x" (
if NOT "%%a"=="N/A " (
call :formatHardwareId1 %%a
set /a count+=1
)
)
)
@rem copy to clipboard
@rem echo Found IDs: %count%
if count gtr 0 (
clip < %verify%
) else (
echo No hardware ID found.
)
echo.
@rem remove out file if not specified externally
@rem ENDLOCAL
goto:eof)
@rem ---------------------------------------------------------
@rem ---------------------------------------------------------
@rem function to format the Hardware ID
@rem ---------------------------------------------------------
:formatHardwareId
SETLOCAL ENABLEDELAYEDEXPANSION
set localmac=%1
@rem set localmac=%localmac:~1,-1%
set localmac=%localmac:-=:%
if NOT "x%1"=="x" (
@rem echo local: %localmac%
if NOT "%localmac%"=="%removedStr%" (
if NOT "%localmac%"=="N/A" (
echo %indent% W-%localmac%
echo %indent% W-%localmac% >> %outfile%
)
)
)
ENDLOCAL
exit /b
@rem
:formatHardwareId1
SETLOCAL ENABLEDELAYEDEXPANSION
set localmac=%1
@rem set localmac=%localmac:~1,-1%
set localmac=%localmac:-=:%
if NOT "x%1"=="x" (
@rem echo local: %localmac%
if NOT "%localmac%"=="%removedStr%" (
if NOT "%localmac%"=="N/A" (
echo %indent% W-%localmac%
echo %indent% W-%localmac% >> %verify%
)
)
)
ENDLOCAL
exit /b
@rem ---------------------------------------------------------
第二个批处理文件2)verify.bat
@echo off
set ans=answer.txt
if exist %ans% del %ans%
for /f "Delims=" %%a in (out.txt) do (
set TEXT=%%a
)
for /f "Delims=" %%a in (verify.txt) do (
set TEXT1=%%a
)
if %text1%==%TEXT% echo 1 > answer.txt
您在批处理文件中使用了相对路径并且没有指定批处理文件的启动目录。这是非常不可靠的方法。
文件可能在某个临时文件夹中创建(如果创建的话)。
要调试批处理文件,删除 @echo off
并在批处理文件末尾添加 pause
。这允许您检查批处理文件输出。