 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Andreone
Joined: 20 Jul 2007 Posts: 257 Location: Paris, France
|
Posted: Wed Aug 29, 2007 12:20 pm Post subject: FileExist in path environment |
|
|
| It would be nice to have IfExist / FileExist having an option to support path environment variable, like Run does. |
|
| Back to top |
|
 |
tic
Joined: 22 Apr 2007 Posts: 1786
|
Posted: Wed Aug 29, 2007 1:15 pm Post subject: |
|
|
| what does that mean? |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Wed Aug 29, 2007 2:34 pm Post subject: |
|
|
| 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.
 |
|
| Back to top |
|
 |
Andreone
Joined: 20 Jul 2007 Posts: 257 Location: Paris, France
|
Posted: Wed Aug 29, 2007 3:23 pm Post subject: |
|
|
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 |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Wed Aug 29, 2007 3:30 pm Post subject: |
|
|
| Andreone wrote: | | What's an UDF? |
User Defined Function
* Redundant code edited out *

Last edited by SKAN on Wed Aug 29, 2007 11:13 pm; edited 2 times in total |
|
| Back to top |
|
 |
Andreone
Joined: 20 Jul 2007 Posts: 257 Location: Paris, France
|
Posted: Wed Aug 29, 2007 3:50 pm Post subject: |
|
|
| Ok, I will write a function. |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Wed Aug 29, 2007 3:52 pm Post subject: |
|
|
Did you try my version ?  |
|
| Back to top |
|
 |
Andreone
Joined: 20 Jul 2007 Posts: 257 Location: Paris, France
|
Posted: Wed Aug 29, 2007 6:17 pm Post subject: |
|
|
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 ? |
|
| Back to top |
|
 |
Chris Site Admin
Joined: 02 Mar 2004 Posts: 10716
|
Posted: Wed Aug 29, 2007 8:49 pm Post subject: |
|
|
| It could happen. However, it's more likely to become part of the standard library. |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Wed Aug 29, 2007 8:50 pm Post subject: |
|
|
| 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  |
|
| Back to top |
|
 |
Tuncay
Joined: 07 Nov 2006 Posts: 1886 Location: Germany
|
Posted: Wed Aug 29, 2007 9:59 pm Post subject: |
|
|
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 |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Wed Aug 29, 2007 11:18 pm Post subject: |
|
|
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 ?
 |
|
| Back to top |
|
 |
Andreone
Joined: 20 Jul 2007 Posts: 257 Location: Paris, France
|
Posted: Thu Aug 30, 2007 9:23 am Post subject: |
|
|
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 |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Thu Aug 30, 2007 9:55 am Post subject: |
|
|
| 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.
 |
|
| Back to top |
|
 |
Andreone
Joined: 20 Jul 2007 Posts: 257 Location: Paris, France
|
Posted: Thu Aug 30, 2007 10:47 am Post subject: |
|
|
| 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 |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|