Post by xxgeek on Dec 1, 2022 18:14:08 GMT
This topic was discussed in an earlier thread, but needs revisiting since some testing has proved a better way.
When first trying to get the users home path I wrote a Visual basic script to get the job done.
At the time tsh73 came back with another method.
Instead of using a VB script, tsh73 used CMD.exe to pipe to the clip board the %userprofile%
This code worked well too, but there was a pause to allow Windows clip.exe time to copy the %userprofile% to the clipboard. Depending on the users Hardware, the pause could be a second or more.
Since I was using this in some of my programs, and needed the users home path to get things done, it was necessary to get the users home path immediately when the program starts. This caused the program to take a second or more before loading.
Today, I revisited this code to do some testing and found a better, faster and more solid way, with no pause needed, without using clip.exe or the clipboard.
Still using cmd.exe , but piping to a file, instead of the clipboard.
Then looping until the file existence is verified. (which is far faster than waiting for clip.exe.)
Then, I took out the verifying of the file's existence, and it still works. No pause needed either.
Then for kicks, I took out the echo in the line:
run "cmd.exe /c echo %userprofile% |UserHomePath.txt", HIDE
It still works, but an interesting find happened.
With nomainwin being used, I was surprised when a window opened with a line of text (the text was the users home path)
I thought it was the mainwin, so checked my code, and sure enough nomainwin was there.
After a second look, it was Notepad that had opened with the users homepath written in the first, and only, line.
So, it appears that when cmd.exe is used to pipe to a file, it also opens notepad to the file. (if echo is in the line with the CMD command)
Without 'echo' notepad doesn't open.
The users home path still resides in upath$ as expected
Learning something new everyday.
Thought it would be a good idea to share this info.
I will use this new method from now on when running CMD commands that pipe to files.
When first trying to get the users home path I wrote a Visual basic script to get the job done.
'Title - Get_User_Home_Dir_Path
'Author - xxgeek (code always free to use for ANY purpose)
'write a vbs script to get the user Home path (creates a temp file userHomePath.vbs - same dir as code is run from)
userHomePath$ = "userHomePath.vbs"
open userHomePath$ for output as #1
q$ = chr$(34) 'used for quotes in text when writing to file
#1, "Set WshShell = CreateObject(";q$;"WScript.Shell";q$;")"
#1, "strHomeFolder = WshShell.ExpandEnvironmentStrings(";q$;"%USERPROFILE%";q$;")"
#1, "Dim filesys, filetxt, path"
#1, "Set filesys = CreateObject(";q$;"Scripting.FileSystemObject";q$;")"
#1, "Set filetxt = filesys.CreateTextFile(";q$;"UserHomePath.txt";q$;", True)"
#1, "path = filesys.GetAbsolutePathName(";q$;"UserHomePath.txt";q$;")"
#1, "filetxt.WriteLine(strHomeFolder)"
#1, "filetxt.Close"
#1, "Wscript.Sleep(200)"
close #1
'run the vbs script created (it creates a temp file UserHomePath.txt in same dir as this code is run)
run "wscript ";userHomePath$'loop until file UserHomePath.txt is verified saved to disk
do
res = fileExists(DefaultDir$,"UserHomePath.txt")
'if res then exit do
scan
loop until res
'get the line of text from UserHomePath.txt with the path to user Home dir
userHomeDir$ = "UserHomePath.txt"
open userHomeDir$ for input as #1
line input #1, line$(x)
upath$ = line$(x) 'text stating path of user Home Dir
if upath$ = "" then notice "Sorry, can't find user path "
print "upath$ = ";upath$
close #1
'Verify file existence function
function fileExists(path$, filename$)
dim fileExistsInfo$(0,0)
files path$, filename$, fileExistsInfo$()
fileExists = val(fileExistsInfo$(0, 0)) 'non zero is true
end function
At the time tsh73 came back with another method.
Instead of using a VB script, tsh73 used CMD.exe to pipe to the clip board the %userprofile%
call getUserPath
end
sub getUserPath
cursor hourglass
run "cmd.exe /c echo %userprofile% |clip", HIDE
call pause 600
open "GetUserPath" for text as #1
#1 "!paste"
#1 "!contents? upath$"
upath$ = trim$(upath$)
close #1
print upath$
cursor normal
end sub
sub pause mil
t=time$("ms")+mil
while time$("ms")<t
scan
wend
end sub
This code worked well too, but there was a pause to allow Windows clip.exe time to copy the %userprofile% to the clipboard. Depending on the users Hardware, the pause could be a second or more.
Since I was using this in some of my programs, and needed the users home path to get things done, it was necessary to get the users home path immediately when the program starts. This caused the program to take a second or more before loading.
Today, I revisited this code to do some testing and found a better, faster and more solid way, with no pause needed, without using clip.exe or the clipboard.
Still using cmd.exe , but piping to a file, instead of the clipboard.
Then looping until the file existence is verified. (which is far faster than waiting for clip.exe.)
Then, I took out the verifying of the file's existence, and it still works. No pause needed either.
Then for kicks, I took out the echo in the line:
run "cmd.exe /c echo %userprofile% |UserHomePath.txt", HIDE
It still works, but an interesting find happened.
With nomainwin being used, I was surprised when a window opened with a line of text (the text was the users home path)
I thought it was the mainwin, so checked my code, and sure enough nomainwin was there.
After a second look, it was Notepad that had opened with the users homepath written in the first, and only, line.
So, it appears that when cmd.exe is used to pipe to a file, it also opens notepad to the file. (if echo is in the line with the CMD command)
Without 'echo' notepad doesn't open.
The users home path still resides in upath$ as expected
Learning something new everyday.
Thought it would be a good idea to share this info.
I will use this new method from now on when running CMD commands that pipe to files.
call getUserPath
end
sub getUserPath
cursor hourglass
run "cmd.exe /c %userprofile% |UserHomePath.txt", HIDE
'do
'res = fileExists(DefaultDir$,"UserHomePath.txt")
'if res then exit do
'scan
'loop until res
'get the line of text from UserHomePath.txt with the path to user Home dir
userHomeDir$ = "UserHomePath.txt"
open userHomeDir$ for input as #1
line input #1, line$(x)
upath$ = line$(x) 'text stating path of user Home Dir
if upath$ = "" then notice "Sorry, can't find user path "
close #1
cursor normal
end sub