AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

FileExist in path environment
Goto page 1, 2  Next
 
Reply to topic    AutoHotkey Community Forum Index -> Wish List
View previous topic :: View next topic  
Author Message
Andreone



Joined: 20 Jul 2007
Posts: 257
Location: Paris, France

PostPosted: Wed Aug 29, 2007 12:20 pm    Post subject: FileExist in path environment Reply with quote

It would be nice to have IfExist / FileExist having an option to support path environment variable, like Run does.
Back to top
View user's profile Send private message
tic



Joined: 22 Apr 2007
Posts: 1786

PostPosted: Wed Aug 29, 2007 1:15 pm    Post subject: Reply with quote

what does that mean?
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Wed Aug 29, 2007 2:34 pm    Post subject: Reply with quote

tic wrote:
what does that mean?


The following code will do nothing unless run from the same folder where notepad.exe resides ...
Code:
IfExist, Notepad.exe
  Run, Notepad.exe

Whereas, if ifexist could use the path environment, it would satisfy the condition.

@Andreone: You can write an UDF for this purpose.

Smile
Back to top
View user's profile Send private message Send e-mail
Andreone



Joined: 20 Jul 2007
Posts: 257
Location: Paris, France

PostPosted: Wed Aug 29, 2007 3:23 pm    Post subject: Reply with quote

The example with Notepad is exactly what I meant.

Quote:
@Andreone: You can write an UDF for this purpose

What's an UDF?
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Wed Aug 29, 2007 3:30 pm    Post subject: Reply with quote

Andreone wrote:
What's an UDF?


User Defined Function

* Redundant code edited out *

Smile


Last edited by SKAN on Wed Aug 29, 2007 11:13 pm; edited 2 times in total
Back to top
View user's profile Send private message Send e-mail
Andreone



Joined: 20 Jul 2007
Posts: 257
Location: Paris, France

PostPosted: Wed Aug 29, 2007 3:50 pm    Post subject: Reply with quote

Ok, I will write a function.
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Wed Aug 29, 2007 3:52 pm    Post subject: Reply with quote

Did you try my version ? Smile
Back to top
View user's profile Send private message Send e-mail
Andreone



Joined: 20 Jul 2007
Posts: 257
Location: Paris, France

PostPosted: Wed Aug 29, 2007 6:17 pm    Post subject: Reply with quote

not yet, but sure I will. Thanks Smile

Bitwise, my question still opened. Will path environment variable be taken into account some day for the FileExist command ?
Back to top
View user's profile Send private message
Chris
Site Admin


Joined: 02 Mar 2004
Posts: 10716

PostPosted: Wed Aug 29, 2007 8:49 pm    Post subject: Reply with quote

It could happen. However, it's more likely to become part of the standard library.
Back to top
View user's profile Send private message Send e-mail
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Wed Aug 29, 2007 8:50 pm    Post subject: Reply with quote

Andreone wrote:
Bitwise, my question still opened. Will path environment variable be taken into account some day for the FileExist command ?


It will break existing scripts. I also like to have this functionality built-in but not with IfExist or FileExist()

Smile

Edit: I am late by a minute Sad
Back to top
View user's profile Send private message Send e-mail
Tuncay



Joined: 07 Nov 2006
Posts: 1886
Location: Germany

PostPosted: Wed Aug 29, 2007 9:59 pm    Post subject: Reply with quote

I did wrote a function with the ability to search in path (user defined pathes, separated by comma is also possible) a while ago:
Topic
Documentation

File_GetPath() returns the full path or something other information of a search string.

Usage:
Code:
SearchString = *.ahk
PathToSearch = %A_WorkingDir%
RegExMatch =
File := File_GetPath(SearchString, RegExMatch, 1, PathToSearch)
If ErrorLevel
    MsgBox "%File%" found.
Back to top
View user's profile Send private message Send e-mail Visit poster's website
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Wed Aug 29, 2007 11:18 pm    Post subject: Reply with quote

Previous code now enhanced to look up the registry. This code should return fullpath for atleast 90% of the commonly installed files.

Code:
MsgBox, % WhereIs( "pinball.exe" )
MsgBox, % WhereIs( "iexplore.exe" )
MsgBox, % WhereIs( "firefox.exe" )
MsgBox, % WhereIs( "autohotkey.chm" )
MsgBox, % WhereIs( "user32.dll" )
MsgBox, % WhereIs( "system.ini" )


WhereIs( File ) {

 ; Working Folder
 Path0 := A_WorkingDir
 DllCall( "shlwapi\PathAddBackslashA", UInt,&Path0 ) , VarSetCapacity( Path0, -1 )
 IfExist, % Path0 File, Return Path0 File

 ; Script Folder
 Path1 := A_ScriptDir
 DllCall( "shlwapi\PathAddBackslashA", UInt,&Path1 ) , VarSetCapacity( Path1, -1 )
 IfExist, % Path1 File, Return Path1 File

 ; AutoHotkey Folder
 SplitPath, A_AhkPath,,Path2
 DllCall( "shlwapi\PathAddBackslashA", UInt,&Path2 ) , VarSetCapacity( Path2, -1 )
 IfExist, % Path2 File, Return Path2 File

 ; Standard Library Folder
 Path3 := Path2 "Lib\"
 IfExist, % Path3 File, Return Path3 File

 ; User Library Folder
 Path4 := A_MyDocuments "\Lib\"
 IfExist, % Path4 File, Return Path4 File

 ; Parsing DOS Path variable
 EnvGet, DosPath, Path
 Loop, Parse, DosPath, `;
  {
    IfEqual, A_LoopField,, Continue
    Path5 := A_LoopField
    DllCall( "shlwapi\PathAddBackslashA", UInt,&Path5 ) , VarSetCapacity( Path5, -1 )
    IfExist, % Path5 File, Return Path5 File
  }

 ; Looking up Registry
 RegRead, Path6, HKLM, SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\%File%
 IfExist, % Path6, Return Path6

}


Anybody got suggestions on the order of lookup ?

Smile
Back to top
View user's profile Send private message Send e-mail
Andreone



Joined: 20 Jul 2007
Posts: 257
Location: Paris, France

PostPosted: Thu Aug 30, 2007 9:23 am    Post subject: Reply with quote

I tried WhereIs and it works fine.

About the searching order, it depends of what is searched. Maybe the more generic would be:
* working folder
* script folder
* dos path
* registry
* ahk folder
* library folders

I've done some cosmetic changes:
* lookup the script folder only when different from the working folder
* only 1 local variable (named Path) instead of Path0, Path1 ...
* remove call to "shlwapi\PathAddBackslashA" to be slightly faster (not sure I'm right on that)

Code:
WhereIs(File)
{
   ; Working Folder
   Path := A_WorkingDir "\"
   IfExist, % Path File, Return Path File

   ; Script Folder
   if(A_ScriptDir != A_WorkingDir) {
      Path := A_ScriptDir "\"
      IfExist, % Path File, Return Path File
   }

   ; Parsing DOS Path variable
   EnvGet, DosPath, Path
   Loop, Parse, DosPath, `;
   {
      IfEqual, A_LoopField,, Continue
      Path := A_LoopField "\"
      IfExist, % Path File, Return Path File
   }

   ; Looking up Registry
   RegRead, Path, HKLM, SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\%File%
   IfExist, % Path, Return Path
   
   ; AutoHotkey Folder
   SplitPath, A_AhkPath,,Path
   IfExist, % Path "\" File, Return Path "\" File

   ; Standard Library Folder
   Path := A_AhkPath "Lib\"
   IfExist, % Path File, Return Path File

   ; User Library Folder
   Path := A_MyDocuments "\Lib\"
   IfExist, % Path File, Return Path File
}
Back to top
View user's profile Send private message
SKAN



Joined: 26 Dec 2005
Posts: 8688

PostPosted: Thu Aug 30, 2007 9:55 am    Post subject: Reply with quote

Quote:
About the searching order, it depends of what is searched. Maybe the more generic would be:
* working folder
* script folder
* dos path
* registry
* ahk folder
* library folders


I would agree. Smile

Quote:
* lookup the script folder only when different from the working folder


Nice Smile

Quote:
* only 1 local variable (named Path) instead of Path0, Path1 ...


I named it different for readability .. Anyways - even if you want use a single var, do not name it PATH as it would conflict with Dos variable when #NoEnv is not in effect. Try the following without uncommenting the first line:

Code:
; #NoEnv

F()

F() {

MsgBox, % PATH

}


Quote:
* remove call to "shlwapi\PathAddBackslashA" to be slightly faster (not sure I'm right on that)


If you want to save one line you may use RegEx - But - it is advisable to check the trailing backslash because A_ScriptDir and A_WorkingDir might be the root drive like C:\ , D:\ etc and in such cases Path := A_WorkingDir "\" will result in C:\\ or D:\\
Even AutoHotkey folder can be a root drive when run standalone from an USB drive.

Smile
Back to top
View user's profile Send private message Send e-mail
Andreone



Joined: 20 Jul 2007
Posts: 257
Location: Paris, France

PostPosted: Thu Aug 30, 2007 10:47 am    Post subject: Reply with quote

Quote:
I named it different for readability .. Anyways - even if you want use a single var, do not name it PATH as it would conflict with Dos variable when #NoEnv is not in effect.

I've updated the variables names:
Path -> PathName
File -> FileName

Quote:
If you want to save one line you may use RegEx - But - it is advisable to check the trailing backslash because A_ScriptDir and A_WorkingDir might be the root drive like C:\ , D:\ etc and in such cases Path := A_WorkingDir "\" will result in C:\\ or D:\\
Even AutoHotkey folder can be a root drive when run standalone from an USB drive.

Well, my point was not to save a few lines, but only to do it if it's required.
Actually, c:\\ or c:\\dir\file are still valid paths. However, I understand it can be seen as dirty.
During testing, I felt that msgboxes appeared faster when not calling PathAddBackslashA.

Code:
WhereIs(FileName)
{
   ; Working Folder
   PathName := A_WorkingDir "\"
   IfExist, % PathName FileName, Return PathName FileName

   ; Script Folder
   if(A_ScriptDir != A_WorkingDir) {
      PathName := A_ScriptDir "\"
      IfExist, % PathName FileName, Return PathName FileName
   }

   ; Parsing DOS Path variable
   EnvGet, DosPath, Path
   Loop, Parse, DosPath, `;
   {
      IfEqual, A_LoopField,, Continue
      IfExist, % A_LoopField "\" FileName, Return A_LoopField "\" FileName
   }

   ; Looking up Registry
   RegRead, PathName, HKLM, SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\%FileName%
   IfExist, % PathName, Return PathName
   
   ; AutoHotkey Folder
   SplitPath, A_AhkPath,,PathName
   IfExist, % PathName "\" FileName, Return PathName "\" FileName

   ; Standard Library Folder
   PathName := A_AhkPath "Lib\"
   IfExist, % PathName FileName, Return PathName FileName

   ; User Library Folder
   PathName := A_MyDocuments "\Lib\"
   IfExist, % PathName FileName, Return PathName FileName
}
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Wish List All times are GMT
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group