Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading a file Path on Extra 6.5 Macros 3

Status
Not open for further replies.

tonytogna

IS-IT--Management
Nov 11, 2005
5
IT
Hello!

My working environment is Win2000 or WinXP.

I wrote a macro which invokes via "shell" an executable file (.exe); unfortunately that executable file is not always on the same directory (depending on choices made during installation) and it is not on the %path% variable.

It seems that the shell instruction wants the executable file name including the complete path, but I can't write it as a fixed parameter because of the problem just mentioned.

Does anybody know how to look for a file and retrieve its path on an Extra Macro?

(I tried the "dir" function, but either I don't know how to use it, or it does not give what I'm looking for.

Any help is greatly appreciated!

thanks!!
:)
 
You can do what your asking with the dir function. Your logic would go something like this (but you'll want to function it out as you'll want to look at all drives)

Sub Main
Dim AllFolders(100) as string
Drive$ = "C:/"
foldernum = 1
MyFolders$ = Dir$(Drive$,16)
If Len(MyFolders$) Then
Do
If instr(MyFolders$,".") = 0 then
allFolders(FolderNum) = Drive$+MyFolders$
FolderNum = FolderNum + 1 ' count list
End if
MyFolders$ = Dir$ ' Find next match.
Loop Until Len(MyFolders$) = 0 ' Continue.
End If
For x = 1 to FolderNum
msgbox AllFolders(x)
next x
End Sub

This would give you an array (you'll want this array dynamic as who knows how many folders you'll find) of all folders found on c:/.

next function check those folders with the dir function to look for your file.exe.

Here's where it gets fun:

You would then search the folders array for more folders and check each of those folders for the specific file.exe your looking for and so on and so on and so on.

Not sure how long all this searching would take. It'd be highly preferrable to know the exact directory the user is dropping your .exe to and avoid all this mess.

This would however, be fun to write so if you need more help let me know.
 

Many thanks to mgwils, I think I'll follow his advise, and I'll work out a compromise, that is:

- dir on the directory where I expet to find the exe
- if found, shell
- if not found, I'll use mgwils tip

Thanks again
 
If you're looking for a specific file in a specific location then you don't need to loop it. You can just do:
Directory = Dir("Drive:\Location\YourFile.exe")

I've included a script that would work for both a specific file search that will search all folders if the file is not found.

Array Folders() is a list of all the folders.
Variable i tracks the size of the array
Variable j tracks the location in the array
Variable Found determines whether the file has been found or not.
Sub SearchAll searches the folder for your file. If the file is not found it will add all subfolders within the folder to an array.
Sub Main searches for the file in a specified location first. If your file is not found it will search all folders on the C:\ drive until all folders are searched of the file is found.

Code:
Dim Folders$(),i,j,Found

Sub SearchAll(Folder)
  Folder2 = Folder
  Directory = Dir(Folder+"Your.exe")
  If Directory="" then
    Directory = Dir(Folder+"*.",16)
    While Len(Directory)>0
      If InStr(Directory,".") = 0 then
        i = i + 1
        ReDim Preserve Folders$(i)
        Folders$(i) = Folder2+Directory+"\"
      End If
      Directory = Dir
    Wend
  Else
    Found = true
  End If
End Sub

Sub Main
  Found = false
  ReDim Folders$(i)
  Here = "C:\Temp\"
  Directory = Dir(Here+"Your.exe")
  If Directory<>"" then
    MsgBox "File is located in "+Here
  Else
    i = 0
    j = -1
    Folders$(i) = "C:\"
    While Found = False and j < i
        j = j + 1
        Call SearchAll(Folders$(j))
    Wend
    If Found = true then
        MsgBox "File is located in "+Folders(j)
    Else
        MsgBox "File not found"
    End If
  End If
End Sub

The only issue you might run into with this is if there are over 32,000 (I don't recall the exact number) of folders on the drive and the program attempts to search all of them. An array can only be so big. I doubt you'd run into this issue.
 
Star for Skie! I did notice however, that the script doesn't like spaces in file paths e.x. c:\program files\ect\ect
 
Sorry Skie it's not spaces that's giving me issues. Some yet unknown quirky problem. I'll let you know when I get it pinpointed.
 
Hello Skie and mgwils, I have tried to write the code, but I also have a problem (I think the same of mgwils), it seems it's on the subfolders; I'll check it and try again.

Anyway, thanks for your help
 
Thought that changing the line in Skie's code reading Directory = Dir(Folder+"*.",16)
to
Directory = Dir(Folder,16)
it would pick up all folders in the file, but now I just keep killing my compliler :) but i'll look at it again when I get some time Tony. So close yet so far away. I think I have narrowed the problem down to missing the first sub folder in sub folders. For e.x I can find a file in
C:\Program Files\Adobe\Illustrator 10
but not in
C:\Program Files\Adobe\Acrobat 5.0
and the same for each sub folder from there on
 
It would have to do with skipping folders with a "." in the name.

I tried changing this
Code:
If InStr(Directory,".") = 0 then
to this
Code:
If Directory <> "." and Directory <> ".." then
It still skips folders like Acrobat 5.0 but will grab folders like .java.

The only thing I can think of for it to get all the directories would be to replace this:
Code:
Directory = Dir(Folder+"*.",16)
While Directory<>""
  If Directory <> "." and Directory <> ".." then
    i = i + 1
    ReDim Preserve Folders$(i)
    Folders$(i) = Folder2+Directory+"\"
  End If
  Directory = Dir
Wend
With this:
Code:
a = -1
Dim FnF()
Directory = Dir(Folder,16)
While Directory<>""
  a = a + 1
  ReDim Preserve FnF(a)
  FnF(a) = Directory
  Directory = Dir
Wend
b = -1
Dim Files()
Directory = Dir(Folder)
While Directory<>""
  b = b + 1
  ReDim Preserve Files(b)
  Files(b) = Directory
  Directory = Dir
Wend
For c = 0 to a
  NewFolder = true
  For d = 0 to b
    If FnF(a) = Files(d) then NewFolder = false
  Next
  If NewFolder and FnF <> "." and FnF <> ".." then
    i = i + 1
    ReDim Preserve Folders$(i)
    Folders$(i) = Folder2+FnF+"\"
  End If
Next
This is going to increase the time it takes to search by quite a bit. But this should ensure that all folders are added to the list.
 
Oops on the code. FnF is an array so it needs to be FnF(c)
Code:
a = -1
Dim FnF()
Directory = Dir(Folder,16)
While Directory<>""
  a = a + 1
  ReDim Preserve FnF(a)
  FnF(a) = Directory
  Directory = Dir
Wend
b = -1
Dim Files()
Directory = Dir(Folder)
While Directory<>""
  b = b + 1
  ReDim Preserve Files(b)
  Files(b) = Directory
  Directory = Dir
Wend
For c = 0 to a
  NewFolder = true
  For d = 0 to b
    If FnF(a) = Files(d) then NewFolder = false
  Next
  If NewFolder and FnF(c) <> "." and FnF(c) <> ".." then
    i = i + 1
    ReDim Preserve Folders$(i)
    Folders$(i) = Folder2+FnF(c)+"\"
  End If
Next
 
Hello,

thanks again for your great help, I am still trying to make the macro work, because as a matter of fact I need it to scan also the subdirecctories.
I know that scanning so deep would take long, but I plan to make the macro do that once, since I could then write a file with the exact path and use it for the next running of the macro.

I am not an expert in writing macros, I hope I'll make it!

;)

bye
 
Hello!

forget my last post, I am dumb! what I wanted to find was the path of an .exe file (which works under windows) in order to run it.
....I have just discovered on a reply to a previous thread that I can easily use the API ShellExecute passing the program name as parameter!

All problems are now solved!

thanks
bye

see answer to:

pbulko: create a msg box that will open an executable
======================================================

falkor13 (Programmer) 27 Aug 03 11:47
scottmh:
I was looking for answers to my question, and though I could help you with yours. Have you considered the ShellExecute API? It uses the same logic that Windows uses to associate a file to an application when you double click on it. Try the example below. The only annoting thing I've found with it is that it reuses already open instances of the called application. But it might work for you. Use the path to your own XLS file rather than 'C:\test.xls' - it works with .doc, you can pass it a url, etc...


Option Explicit

Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal HWND As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long


Public Sub subNewShell(strFileToOpen As String)
Dim lngReturn As Long
lngReturn = ShellExecute(0, "open", strFileToOpen, vbNullString, vbNullString, 1)
End Sub

Public Sub Main()
Call subNewShell("C:\test.xls")
End Sub


falkor13 (Programmer) 27 Aug 03 11:49
scottmh:

Sorry, use "" instead of vbNullString for Extra Basic.

falkor13 (Programmer) 27 Aug 03 11:51
Drat, also forgot to remove the 'Public' keywords. This one will compile and run in Extra!

Option Explicit

Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
(ByVal HWND As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long


Sub subNewShell(strFileToOpen As String)
Dim lngReturn As Long
lngReturn = ShellExecute(0, "open", strFileToOpen, "", "", 1)
End Sub

Sub Main()
Call subNewShell("C:\test.xls")
End Sub


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top