 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Tue Jul 25, 2006 11:02 am Post subject: |
|
|
Following this topic, no?
Well, you overlooked something, which is missing from the manual as well:
| Code: | Tooltip % A_ScriptName " - " GetShortPathName(A_ScriptFullPath)
Sleep 1000
Tooltip % "Windows Directory - " GetShortPathName(A_Windir)
Sleep 1000
Tooltip % "Temp Directory - " GetShortPathName(A_Temp)
Sleep 1000
Tooltip % "Desktop - " GetShortPathName(A_Desktop)
Sleep 1000
Tooltip % "Desktop (All Users) - " GetShortPathName(A_DesktopCommon)
Sleep 1000
Tooltip % "AutoHotkey.exe - " GetShortPathName(A_AhkPath)
Sleep 1000
Return
GetShortPathName(_longPath)
{
Loop %_longPath%, 1
{
Return A_LoopFileShortPath
}
}
|
| AHK's Manual wrote: | | IncludeFolders? | 1 All files and folders that match the wildcard pattern are retrieved. |
Two side notes on your function:
1) You don't need to add 1 to StrLen - "This length does not include the internal zero terminator. For example, specifying 1 would allow the variable to hold up to one character in addition to its internal terminator."
2) It is a good idea to initialize a struct or string to 0 (last parameter of VarSetCapacity), or just omit this parameter. _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5890
|
Posted: Tue Jul 25, 2006 11:26 am Post subject: |
|
|
Dear PhiLho,
I have mentioned that topic as reference in my post!
| Quote: | Well, you overlooked something, which is missing from the manual as well:
| AHK's Manual wrote: | | IncludeFolders? | 1 All files and folders that match the wildcard pattern are retrieved. |
|
Yes .. I did overlook it .. I have re-posted the corrected version in the other post!
| Quote: | | 1) You don't need to add 1 to StrLen - "This length does not include the internal zero terminator. For example, specifying 1 would allow the variable to hold up to one character in addition to its internal terminator." |
I had problem with converting A_WinDir to 8.3 because length of Shortname was exactly equal to Longname .. (In my case it is C:\WINNT) . The VB example (from which I had adapted this wrapper) did add 1.
| Quote: | | 2) It is a good idea to initialize a struct or string to 0 (last parameter of VarSetCapacity), or just omit this parameter. |
I will correct this ..
.. and thanks for all this help and info
Regards,  _________________ SKAN - Suresh Kumar A N |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Tue Jul 25, 2006 11:57 am Post subject: |
|
|
| Goyyah wrote: | I have mentioned that topic as reference in my post!  | Oups, sorry, I missed it. There are so many colored and underlined texts in your post that it was a bit confusing (plus I skimmed the start, to be honest. ). _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5890
|
Posted: Tue Sep 26, 2006 6:13 pm Post subject: |
|
|
| Quote: | GetPriority()
Usage : GetPriority(processName)
GetPriority() ascertains the priority level for an existing process and returns one of the following strings:
Low
BelowNormal
Normal
AboveNormal
High
Realtime
Note: If no parameter is passed, the script's own priority level is retrieved.
MSDN Reference : GetPriorityClass() , OpenProcess() , CloseHandle() , Process Security and Access Rights
| Code: | ; Example along with the GetPriority() Function.
Run, calc.exe
WinWait, Calculator
Process, Exist, calc.exe
PID := ErrorLevel
Process, Priority, %PID%, Low
MsgBox, 64, Process Priority [CALC.EXE], % GetPriority("calc.exe")
Process, Priority, %PID%, BelowNormal
MsgBox, 64, Process Priority [CALC.EXE], % GetPriority("calc.exe")
Process, Priority, %PID%, AboveNormal
MsgBox, 64, Process Priority [CALC.EXE], % GetPriority("calc.exe")
Process, Priority, %PID%, High
MsgBox, 64, Process Priority [CALC.EXE], % GetPriority("calc.exe")
Process, Priority, %PID%, Realtime
MsgBox, 64, Process Priority [CALC.EXE], % GetPriority("calc.exe")
Process, Priority, %PID%, Normal
MsgBox, 64, Process Priority [CALC.EXE], % GetPriority("calc.exe")
Return
; The Function
GetPriority(process="") {
Process, Exist, %process%
PID := ErrorLevel
IfLessOrEqual, PID, 0, Return, "Error!"
hProcess := DllCall("OpenProcess", Int,1024, Int,0, Int,PID)
Priority := DllCall("GetPriorityClass", Int,hProcess)
DllCall("CloseHandle", Int,hProcess)
IfEqual, Priority, 64 , Return, "Low"
IfEqual, Priority, 16384, Return, "BelowNormal"
IfEqual, Priority, 32 , Return, "Normal"
IfEqual, Priority, 32768, Return, "AboveNormal"
IfEqual, Priority, 128 , Return, "High"
IfEqual, Priority, 256 , Return, "Realtime"
Return ""
} |
Cross reference: Ask-for-help Topic : How to get the process priority?
|
_________________ SKAN - Suresh Kumar A N |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5890
|
Posted: Sun Oct 08, 2006 4:22 pm Post subject: |
|
|
| Quote: | GetFileFolderSize()
Usage : GetFileFolderSize(fPath)
GetFileFolderSize ascertains and return the Folder size or retrieves and returns the File size for a given path or path+filename.
fPath can be any valid path or path+filename like: "C:"
"C:\"
"C:\Windows"
"C:\Windows\"
A_WinDir
A_Temp The function will return a negative value on error.
The size returned will be in bytes. To arrive the size of a folder / file in MB you may follow the below method:
FolderSize_MB := GetFileFolderSize(A_WinDir) // 2 ** 20 ( Credit: Titan .. See the post. )
| Code: | GetFileFolderSize(fPath="") {
IfNotExist, %fPath%, Return, -1 ; file/path not found!
StringRight, LChar, fPath, 1
If (StrLen(fPath) != 3 AND LChar = "\")
StringTrimRight, fPath, fPath, 1
FileGetAttrib, Attributes, %fPath% ; to ascertain file/folder
If (!InStr(Attributes,"D")) { ; if path is a file
FileGetSize, FileSize, %fPath%
Return FileSize
}
If (InStr(Attributes,"D")) { ; if path is a folder
Loop, %fPath%\*.*, 1, 1
FolderSize += %A_LoopFileSize%
Return FolderSize
}
Return -2 ; unknown error!
} |
** Short Version posted on 06-June-2008 **
| Code: | GetFileFolderSize(fPath="") {
If InStr( FileExist( fPath ), "D" ) {
Loop, %fPath%\*.*, 1, 1
FolderSize += %A_LoopFileSize%
Return FolderSize ? FolderSize : 0
} Else
If ( FileExist( fPath ) <> "" ) {
FileGetSize, FileSize, %fPath%
Return FileSize ? FileSize : 0
} Else
Return -1
} |
|
_________________ SKAN - Suresh Kumar A N
Last edited by SKAN on Fri Jun 06, 2008 9:47 pm; edited 1 time in total |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5068 Location: imaginationland
|
Posted: Sun Oct 08, 2006 11:14 pm Post subject: |
|
|
Here is a simplified version that should work faster: | Code: | Size(res) {
SplitPath, res, , , e ; check if there is file extension
If !e ; if there isn't, it must be a directory so:
res = %res%\*.* ; loop over all files
Loop, %res%, , 1 ; recurse subfolders
s += A_LoopFileSize ; add size
Return, s ; total size in bytes (divide by 1024 for KiB)
} |
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4016 Location: Pittsburgh
|
Posted: Sun Oct 08, 2006 11:57 pm Post subject: |
|
|
| There can be directories with extension-like names, AHK.FILES or Backup.old or Watcom-1.3. Some filenames don't have extensions, like ntldr. It is safer to check the directory attribute. |
|
| Back to top |
|
 |
Titan
Joined: 11 Aug 2004 Posts: 5068 Location: imaginationland
|
Posted: Mon Oct 09, 2006 12:04 am Post subject: |
|
|
This can be added to the beginning of the function for non-standard files: | Code: | If FileExist(dir) {
FileGetSize, s, %dir%
Return, s
} |
|
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5890
|
Posted: Thu Oct 12, 2006 11:11 am Post subject: |
|
|
| Quote: | VarExist()
Usage : VarExist(Var)
VarExist determines the existence of variable and returns a numerical value..
where,
0 indicates that the variable does not exist
1 indicates that the variable does exist and contains data
2 indicates that the variable does exist and is empty
Edit: 03-Dec-2006 : The following fine piece of 56 character code was written by Titan:
| Code: | varExist(ByRef v) { ; Requires 1.0.46+
return &v = &n ? 0 : v = "" ? 2 : 1
} | * One should not pass AHK Variables like: A_AhkPath
Note:
See the next two posts for a variants by PhiLho/Laszlo which returns a string instead of numerical value.
I have adapted a suggestion made by Laszlo. The shortest version is his (as usual ) and I will be using it too.. I retain the following code for the sake of code comments!
I have retained the original version for those wanting to understand the logic behind:
| Code: | VarExist(Var="") { ; * * * Determines a variable existence. * * *
;
IfEqual, Var,,Return, 0 ; If no parameter, return 0
;
VarAddr = % & %Var% ; Obtain pointer to the variable
;
x := "Var" . Chr(160) ; x will contain a non-existing variable name
NonExistent := &%x% ; Obtain pointer to the non-existing variable
;
if ( VarAddr = NonExistent ) { ; Compare both the pointers and
Return, 0 ; if equal, Return 0 to indicate that
} ; the variable does not exist
else ;
If ( %Var% = "" ) { ; If Var is empty Return 2
Return, 2 ;
} ;
Return 1 ; If above conditions do not apply, Return 1
} ; to indicate variable exists with data |
Notes:
- You may test existence of AutoHotkey's buit-in variables like A_WinDir, A_Gui ( but not the Environment variables like temp, windir). for example, If (VarExist("A_IconFile")=2) is equivalent to If (A_IconFile=""). Testing in-built variables is redundant! The VarExist() function has been tested to handle it (for fault tolerance).. and that is why I chose against accepting the variable as a byref parameter.
- Note: It is not possible to retrieve a pointer to AHK variables like: Pointer := &A_WinDir
- AutoHotkey does not initialise an empty variable!
| Code: | Variable := ""
Result := VarExist("Variable") ; Result will be 0. |
AutoHotkey does not delete an empty variable!
| Code: | Variable=Suresh
Result := VarExist("Variable") ; Result will be 1.
Variable=
Result := VarExist("Variable") ; Result will be 2.
|
|
Regards,  _________________ SKAN - Suresh Kumar A N
Last edited by SKAN on Sun Dec 03, 2006 12:05 pm; edited 3 times in total |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Thu Oct 12, 2006 11:31 am Post subject: |
|
|
Veeery clever!
Might break on future versions of AHK, since it rely on undocumented feature/implementation, but it seems it works now...
Note that returning a number is old-fashion, why not return an explicit value, it doesn't cost more...
| Code: | VarExist(_var="")
{
local varAddr, nonExistentAddr
If (_var = "")
Return "NO"
varAddr := & %_var%
nonExistentAddr := & $I_Doubt?Such@Variable[Would]Exist#
If (varAddr = nonExistentAddr)
Return "NO"
Else If (%_var% = "")
Return "EMPTY"
Return "YES"
}
r := VarExist("Foo")
Foo := 55
r := r "-" VarExist("Foo")
Foo =
r := r "-" VarExist("Foo")
Foo := ""
r := r "-" VarExist("Foo")
MsgBox %r%
| You are very reactive and fast!
How did you got the idea to test the address? _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5890
|
Posted: Thu Oct 12, 2006 11:51 am Post subject: |
|
|
Dear PhiLho,
Thanks ..
| Quote: | | Might break on future versions of AHK, since it rely on undocumented feature/implementation, but it seems it works now... |
I would be happy if Mr.Chris gives an anwser.
| Quote: | | Note that returning a number is old-fashion, why not return an explicit value, it doesn't cost more... |
That is a good idea too .. This what I had in my mind when I wrote the function:
| Code: | If !VarExist("A_Variable")
A_Variable = Suresh |
So, in future, if AutoHotkey has a built-in variable A_Variable, my script does not break.
| Quote: | | How did you got the idea to test the address? |
I am trying to write a supplementary DLL ( in Visual C++) for cheetah2.dll to ease database manipulation. C++ seems to return only pointers and not the actual strings . So I have been trying all sorts of nonsense with pointers . This is when I found that a non-existent variable always return the same pointer.
Regards,  _________________ SKAN - Suresh Kumar A N |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4016 Location: Pittsburgh
|
Posted: Thu Oct 12, 2006 7:35 pm Post subject: |
|
|
Really clever, Goyyah! I found only one limitation so far: built in variables, like A_TAB are reported empty. It looks like a bug in AHK, though.
It is really hard to find a variable name, which is surely never used in scripts. I have a debug function, which reads the source file and processes it. It finds your A_N_o_n_E_x_i_s_t or PhiLho's I_Doubt?Such@Variable[Would]Exist# variables and access their values, which implicitly creates them in the AHK name space. An alternative was to use a character, which you cannot type but AHK still accepts it as a variable name: like Chr(129) or Chr(160) - which looks like Space, not accepted as a variable name when typed directly. It is not 100% safe, either, but other scripts will cause us trouble (the ones which use dynamic naming of variables).
Why do you check for empty names? If somebody asks if a variable with empty name exists, he deserves what comes to him. With the two trivial changes (and texts returned), your script becomes | Code: | VarExist(VarName) {
x := Chr(160)
If (&%VarName% = &%x%)
Return "NotUsed"
Else If (%VarName% = "")
Return "EMPTY"
Return "EXISTS"
} |
Edit: Chr(128) = € is changed to Chr(160).
Last edited by Laszlo on Fri Oct 13, 2006 7:45 pm; edited 1 time in total |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 5890
|
Posted: Fri Oct 13, 2006 8:22 am Post subject: |
|
|
Dear Laszlo,
| Quote: | | Really clever, Goyyah! I found only one limitation so far: built in variables, like A_TAB are reported empty. It looks like a bug in AHK, though. |
I even checked with A_Null := Chr(0) but missed A_Tab
| Quote: | It is really hard to find a variable name, which is surely never used in scripts.
...
An alternative was to use a character, which you cannot type but AHK still accepts it as a variable name: Chr(128). It is not 100% safe, either, but other scripts will cause us trouble (the ones which use dynamic naming of variables). |
Thank you very much.. that was a valuable tip! .. Typing Alt+0128 also works, but I prefer your x := Chr(128):
| Code: | VarExist(VarName) {
If (&%VarName% = &€) ; "€" is Chr(128)
Return 0
Else If (%VarName% = "")
Return 2
Return 1
} |
Thanks again for testing and optimising the function.
Regards,  _________________ SKAN - Suresh Kumar A N |
|
| Back to top |
|
 |
PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Fri Oct 13, 2006 9:36 am Post subject: |
|
|
| Laszlo wrote: | | I found only one limitation so far: built in variables, like A_TAB are reported empty. It looks like a bug in AHK, though. | It is not really a bug, since it is undocumented... Built-in variables are probably not real variables (they are read-only...), idem for UInt and friends. _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4016 Location: Pittsburgh
|
Posted: Fri Oct 13, 2006 5:17 pm Post subject: |
|
|
| Since Chr(128) is the Euro symbol in international fonts, maybe using Chr(129) is safer? It does not represent a printable character in my editor. Or Chr(160), which looks like a Space, which cannot name a variable. |
|
| 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
|