Inno Setup执行批处理文件但不允许它创建其.txt文件

时间:2015-10-11 07:12:20

标签: batch-file cmd installation inno-setup pascal

我的安装程序执行一个接一个创建的两个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

1 个答案:

答案 0 :(得分:0)

您正在批处理文件中使用相对路径,并且未指定批处理文件的启动目录。这是非常不可靠的方法。

可能在某个临时文件夹中创建文件(如果已创建)。

要调试批处理文件,请删除@echo off并在批处理文件末尾添加pause。这允许您检查批处理文件输出。