AutoHotkey Community

It is currently May 25th, 2012, 6:15 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 20 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: August 29th, 2007, 1:20 pm 
Offline

Joined: July 20th, 2007, 10:23 am
Posts: 257
Location: Paris, France
It would be nice to have IfExist / FileExist having an option to support path environment variable, like Run does.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 29th, 2007, 2:15 pm 
Offline

Joined: April 22nd, 2007, 6:33 pm
Posts: 1832
what does that mean?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 29th, 2007, 3:34 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
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.

:)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 29th, 2007, 4:23 pm 
Offline

Joined: July 20th, 2007, 10:23 am
Posts: 257
Location: Paris, France
The example with Notepad is exactly what I meant.

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

What's an UDF?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 29th, 2007, 4:30 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
Andreone wrote:
What's an UDF?


User Defined Function

* Redundant code edited out *

:)


Last edited by SKAN on August 30th, 2007, 12:13 am, edited 2 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 29th, 2007, 4:50 pm 
Offline

Joined: July 20th, 2007, 10:23 am
Posts: 257
Location: Paris, France
Ok, I will write a function.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 29th, 2007, 4:52 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
Did you try my version ? :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 29th, 2007, 7:17 pm 
Offline

Joined: July 20th, 2007, 10:23 am
Posts: 257
Location: Paris, France
not yet, but sure I will. Thanks :)

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 29th, 2007, 9:49 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
It could happen. However, it's more likely to become part of the standard library.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 29th, 2007, 9:50 pm 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
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()

:)

Edit: I am late by a minute :(


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 29th, 2007, 10:59 pm 
Offline

Joined: November 7th, 2006, 9:47 pm
Posts: 1933
Location: Germany
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 30th, 2007, 12:18 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
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 ?

:)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 30th, 2007, 10:23 am 
Offline

Joined: July 20th, 2007, 10:23 am
Posts: 257
Location: Paris, France
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
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 30th, 2007, 10:55 am 
Offline
User avatar

Joined: December 26th, 2005, 4:40 pm
Posts: 8775
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. :)

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


Nice :)

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.

:)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 30th, 2007, 11:47 am 
Offline

Joined: July 20th, 2007, 10:23 am
Posts: 257
Location: Paris, France
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
}


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 20 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google Feedfetcher and 2 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
cron
Powered by phpBB® Forum Software © phpBB Group