Jump to content

Using variables within Run command arguments


Recommended Posts


I am creating a program which will allow the user to check if a file on the PC is 32bit or 64bit.  It asks for the file location, then displays a box showing the result.  I'm using the FILE utility from GNU Coreutils for Windows (http://gnuwin32.sourceforge.net/packages/file.htm).  Here is my code:


#include <Constants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <WindowsConstants.au3>

FileInstall("C:\program files (x86)\gnuwin32\bin\file.exe", @ScriptDir & "\file.exe")
FileInstall("C:\program files (x86)\gnuwin32\bin\zlib1.dll", @ScriptDir & "\zlib1.dll")
FileInstall("C:\program files (x86)\gnuwin32\bin\regex2.dll", @ScriptDir & "\regex2.dll")
FileInstall("C:\program files (x86)\gnuwin32\bin\magic1.dll", @ScriptDir & "\magic1.dll")

$selFile = FileOpenDialog("Browse to the file", "C:\", "(*.*)")

$selFile = chr(34) & $selFile & chr(34)

$hGUI = GUICreate("Test", 700, 100)
$idEdit = GUICtrlCreateEdit("", 0, 0, 650, 100,  BitOR($ES_AUTOVSCROLL, $WS_HSCROLL, $WS_VSCROLL))
GUICtrlSendMsg($idEdit, $EM_LIMITTEXT, -1, 0)

;$_pid = Run(@ComSpec & " /c FILE.EXE " & chr(34) & "C:\Program Files (x86)\EIZO\ScreenSlicer\ESCSlicer.exe" & chr(34), @SystemDir,@SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
$_pid = Run(@comspec & " /c FILE.EXE " & $selFile,@ScriptDir,@SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

$answer = ""

While 1
   $line = StdoutRead($_pid)
   if @error Then ExitLoop
   _GUICtrlEdit_AppendText($idEdit, $line)
   $answer &= $line

   Until GUIGetMsg() = $GUI_EVENT_CLOSE


The line commented out and the line above is used for testing.  I have only been scripting in AutoIt for 4 weeks, so please excuse any best practices that I have missed.

The issue is that if I use the full filename path the script works great, if I use the variable the results box is blank.

I have a feeling that I am doing something wrong with the quotes, but I dont know why the full path works ok.

Does anyone have any ideas?


(I just want to say thank you to UEZ for the script posted on '?do=embed' frameborder='0' data-embedContent>>.  This really helped me with what I'm trying to achieve.)


Link to comment
Share on other sites

Delete this line:

$selFile = chr(34) & $selFile & chr(34)

And change:

$_pid = Run(@comspec & " /c FILE.EXE " & $selFile,@ScriptDir,@SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)


$_pid = Run(@comspec & ' /c FILE.EXE "' & $selFile & '"',@ScriptDir,@SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
Link to comment
Share on other sites

The working directory is being changed with use of FileOpenDialog. When you use run cmd then file.exe it is probably not being found in the current directory if it is not @ScriptDir. You can use FileChangeDir to reset the current working directory to @ScriptDir after use of FileOpenDialog.

To remove the dependency of file.exe, this code may be useful. It has not done much testing but it gives good results so far. It reads from the bits in the header that determine whether it is X86, X64, I64 or native.

$sFileOSType = _FileGetOSType(@WindowsDir & "\hh.exe")
MsgBox(0x40000, @ScriptName, $sFileOSType)

Func _FileGetOSType($sFilePath)
    Local $bChars, $bHeaderAddress, $hRead
    ; read the file in binary mode
    $hRead = FileOpen($sFilePath, 0x10)
    If $hRead = -1 Then Return SetError(1, 0, '')
    ; check if the binary is an executable at initial 2 chars
    If BinaryToString(FileRead($hRead, 2)) <> 'MZ' Then
        Return SetError(2, 0, '')
    ; set position at char 60
    FileSetPos($hRead, 60, 0)
    ; read 4 chars to get header address
    $bChars = FileRead($hRead, 4)
    $bHeaderAddress = BitAND($bChars, 0x0000FFFF)
    ; set position at start of header + 4 chars
    FileSetPos($hRead, $bHeaderAddress + 4, 0)
    ; read 2 chars at OS type setting
    $bChars = FileRead($hRead, 2)
    ; Compare the result to known settings
    ; Native = 0, X86 = 0x014c, Itanium = 0x0200, X64 = 0x8664
    Switch '0x' & Hex(BitAND($bChars, 0xFFFF), 4)
        Case '0x0000'
            Return 'Native'
        Case '0x014C'
            Return 'X86'
        Case '0x0200'
            Return 'I64'
        Case '0x8664'
            Return 'X64'
        Case Else
            Return SetError(3, 0, '')

Edit: You set working directory with run so perhaps the working directory is not the main issue with your code.

Edit: Localized some variables on the rather fresh code.

Edited by MHz
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...