Jump to content

Resolving a fully-qualified Windows program filename


Go to solution Solved by MikeK6MKF,

Recommended Posts

Thanks to all the great support I've had on this forum, I have my first AutoIt script working well.   Now I would like to be able to free my script from its dependence on a hard-wired path to the Adobe Acrobat PDF reader.   Currently, I have the path defined in a variable I use in subsequent Run statements.

Local $AcrobatPath = '"C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe" /A page='

Is there some sort of function that will allow me to query Windows with "AcroRd32.exe" and get the full pathname to that executable?

I'd like to be able to distribute the script to my friends, but can't be sure their AcroRD32.exe will be installed in the same directory.

Any guidance, advice or suggestions will be greatly appreciated.

Thanks!

Link to comment
Share on other sites

  • Moderators

MikeK6MKF,

You could search the disk for the file using _FileListToArrayRec but that could take a while. A better solution might well be to use ShellExecute which allows Windows to use the default application for the passed file type rather than Run - that way the location of that app is moot. ;)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

$hFile = fileopendialog("select pdf" , @ScriptDir , "PDF (*.pdf)")
$sExe = "AcroRd32.exe"
$sPage = "3"

$ExePath =  FileGetShortName(regread('HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths\' & $sExe , ""))
$FilePath =  FileGetShortName($hFile)

run ($ExePath & " /A page=" & $sPage & " " & $FilePath)

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

  • Solution

OK, thanks for all the suggestions!  I bounced this off a buddy who had done something similar before, and he donated a code snippet that I got to work in my script.

; Code snippet to find AcroRD32.EXE on either a 32 bit installation of Windows
    ; or a 64 Bit installation.
    ;


    Local $ipid = Run(@ComSpec & ' /C DIR /b C:\Prog*', "C:\", @SW_HIDE, $STDOUT_CHILD);
    ProcessWaitClose($ipid)

    Local $progDirs = StdoutRead($iPid)
    Local $sPath = "C:\Program Files"
    ; _ArrayDisplay(StringSplit(StringStripCR($progDirs), @CRLF))

    If StringInStr($progDirs, "Program Files (x86)") <> 0 Then
    ; We must be on a 64-bit OS as there is a Program Files (x86)
    ; AcroRd32 will be under this path as its a 32 bit app
    $sPath = "C:\Program Files (x86)"
    EndIf

    ; Run dir on $sPath and find ACRORD32.EXE
    $ipid = Run(@ComSpec & ' /C DIR /s/b "' & $sPath & '\*ACRORD32.EXE', "C:\", @SW_HIDE, $STDOUT_CHILD)
    ProcessWaitClose($ipid)

    Local $hfta = StdoutRead($ipid)
    Local $aOutput = StringSplit(StringStripCR($hfta), @CRLF)
    ; _ArrayDisplay($aOutput)

    Local $i
    For $i=0 To UBound($aOutput) - 1
    If StringInStr($aOutput[$i], "ACRORD32.EXE") <> 0 Then
        ExitLoop
    EndIf
    Next

    If $i = UBound($aOutput) Or  $aOutput[$i] = '' Then
    MsgBox(0, "Error", $i & " Can't find ACRORD32 Installation directory under " & $sPath)
    Exit
    EndIf

    ;
    ; If you get here then $aOutput[$i] is the fully qualified file name of AcroRd32 including
    ; the path and the executable name.
    ;

    $AdobePath = $aOutput[$i]
    ; MsgBox(0,'',"The $AdobePath Path is: " & $AdobePath)

I will study this closely so I can begin to understand just how this works.   I think my script is done, so now off to the Tutorials!

Link to comment
Share on other sites

Actually, neither are ideal resolutions and should only be used in an environment where the expected outcome is something required... Say in an enterprise setting.

The reasons being:

1. Boththose answer is presuming that Acrobat is the default PDF association. What if associations are broken, or another program is the default for .PDF files?

2. Mike's answer is presuming that Acrobat is installed in a default location. What if it is installed onto another drive, or in a location that is not in Program Files?

If neither of these scenarios concern the OP, then it isn't a problem. :)

Link to comment
Share on other sites

  • Moderators

MikeK6MKF,

Tripredacus' comments are exactly why I suggested ShellExecute - let Windows do the hard work for you. ;)

M23

Edited by Melba23
Typo

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

The arguments in the run command, kind of also require that you are using AcroRd32.exe

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Much good information has been given above, and has given me much to think about.   This is a hobby script to be used by Amateur Radio ops to quickly reference HF propagation predictions, generated by an online service.

It is a good point that not all will be using ArcoRd32.exe to open .pdf files.   I have my script out for testing with a few friends and I will see what their experiences are.

Most likely I will revisit this, as I would like to have my script as usable as possible over a variety of environments.

As for being 'mad', well, I have heard that many times before!   :zorro:

Link to comment
Share on other sites

Other options:

If you are looking for a particular executable that is in your PATH, you can issue a "cmd /c where" to find it.

If you are looking to open a document with the standard binding, "cmd /c start" will open it (although, it sometimes has trouble with spaces in the filename even if you quote it).

Link to comment
Share on other sites

Thank you for this insight.   

I've decided I'd best stick with my approach of resolving the path to AcroRD32.exe.   I use Adobe opening parameters to get a specific page, and they are peculiar to AcroRD32.exe, so I'm sort of stuck with it.

Link to comment
Share on other sites

what is #4 lacking?  as described, it seems to meet the requirement with far less effort than #5.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

#4 is the faster way to find the application location, but requires that the program has already been run once.

Even if you probably have an answer, here are 2 other ways:

$aAdobeReader = _UninstallList("DisplayName", "Adobe Reader", "InstallLocation") ; _UninstallList in my signature
_ArrayDisplay($aAdobeReader

and a registry way :

Local $sAdobeReaderPath = _GetAdobeReaderPath()
If @error Then 
    MsgBox(16, "", "Adobe Reader not found")
Else
    MsgBox(0, "", "Adobe Reader full path : " & @CRLF & $sAdobeReaderPath & "\AcroRD32.exe")
EndIf


Func _GetAdobeReaderPath()
    Local $sKey = "HKEY_LOCAL_MACHINE\SOFTWARE\Adobe\Acrobat Reader"
    Local $i = 1, $sApp, $sInstallPath

    While 1
        $sApp = RegEnumKey($sKey, $i)
        If @error Then ExitLoop
        
        $sInstallPath = RegRead($sKey & "\" & $sApp & "\InstallPath", "")
        
        If NOT FileExists($sInstallPath & "\AcroRD32.exe") Then $sInstallPath = ""
        $i += 1
    Wend

    If $sInstallPath = "" Then Return SetError(1, 0, 0)
    Return $sInstallPath
EndFunc
Link to comment
Share on other sites

but requires that the program has already been run once

 

 

good find, that has been my go to for so long i never fully explored its edge cases.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

If you need path to default app for ,pdf, it's a different kettle of fish...

$ext = ".pdf"

$DefaultApp = RegRead("HKEY_CLASSES_ROOT64\" & $ext, "")

$DefaultAppPath = RegRead("HKEY_CLASSES_ROOT64\" & $DefaultApp & "\shell\open\command", "")

ConsoleWrite($DefaultAppPath & @LF)
Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

A better solution might well be to use ShellExecute which allows Windows to use the default application for the passed file type rather than Run - that way the location of that app is moot. ;)

My responses to this thread were purely based on the question as asked. Mike had not mentioned what this path was being used for, so I decided to look at his other threads. It seems the reason for acquiring the path to Acrord32 is because he needs to pass a parameter to it, /A page=... Can ShellExecute pass a parameter to the application that is launching the file?

Link to comment
Share on other sites

Can ShellExecute pass a parameter to the application that is launching the file?

 

 

You would have to be able to determine which application shellexecute is going to use to launch the file and then determine if the argument is suitable for that exe, right?  And if you have that info prior to shellexecute might as well just run it.

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

Ah ok, so in a round-about way, I see how your answer can be correct. If AcroRd32 is the default application for opening a PDF, you could use ShellExecute on the PDF and pass the parameter along with it. This way, the path to AcroRd32 is not needed by the script because you are relying on Windows to do that work for you.

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
 Share

  • Recently Browsing   0 members

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