|
Post by Rod on Dec 2, 2022 5:52:05 GMT -5
Given the way Windows works these days it would be good to have the path to the user directory and the path to the appdata directory easily available to the programmer. I suggest two new system variables to compliment DefaultDir$ and StartupDir$
'all of these files will be created in DefaultDir$ open "test.bat" for output as #test #test "echo %appdata% > test.txt" '#test "echo %userprofile% > test.txt" close #test run "test.bat /q" ,hide open "test.txt" for input as #test line input #test, path$ close #test path$=trim$(path$)+"\" print path$ print DefaultDir$ result = mkdir( path$;"test")
'create a new folder and file open path$;"test\test.dat" for output as #1 #1 "hello" close #1 open path$;"test\test.dat" for input as #1 i$=input$(#1,lof(#1)) print i$ close #1 end
|
|
|
Post by xxgeek on Dec 2, 2022 13:17:59 GMT -5
Same as in the JB forum....
Rod your code doesn't work here until second run.
"Can't find test.txt". I assume it works for you because you have an existing text.txt. If I put a pause before it tries to open test.txt then it works.
What we have now with: run "cmd.exe /c echo %userprofile% > UserHomePath.txt", HIDE
works well here and with tsh73. Does it not work with your setup?
If you delete DefaultDir$ \ test.txt and try again, does it work on first run? I made the same mistake here. Now I delete the txt file before exiting. And I had to put a slight pause in before trying to open the txt file. This pause may need increasing on other PC's depending on hardware. But here on an i7 using SSD drives 100ms works.
Here's the code that works for me.
call getUserPath
end
sub getUserPath cursor hourglass run "cmd.exe /c echo %userprofile% >UserHomePath.txt", HIDE call pause 100 'get the line of text from UserHomePath.txt with the path to user Home dir open "UserHomePath.txt" for input as #1 while not(eof(#1)) line input #1, line$(x) wend upath$ = line$(x)'text stating path of user Home Dir if upath$ = "" then notice "Sorry, can't find user path " close #1 print upath$ kill "UserHomePath.txt" cursor normal end sub
'sub to create pauses in program sub pause mil t=time$("ms")+mil while time$("ms")<t scan wend end sub
|
|
|
Post by xxgeek on Dec 2, 2022 13:43:33 GMT -5
Turns out that cmd doesn't create the txt file fast enough, and we need a pause after the cmd line. At least here on my PC it does.
No need to kill the txt file though. Can remove the kill line.
|
|
|
Post by Rod on Dec 3, 2022 4:26:58 GMT -5
Ok, I was confusing .txt ,bat and .dat files. It does need to wait a bit for the file else you get the previous file. So simple fix?
'all of these files will be created in DefaultDir$ open "test.bat" for output as #test #test "echo %USERPROFILE% > test.txt" close #test run "test.bat /q" ,hide playwave "beep" open "test.txt" for input as #test line input #test, path$ close #test path$=trim$(path$)+"\" print path$ print DefaultDir$
'now put a new folder and file in the userprofile result = mkdir( path$;"test") open path$;"test\test.dat" for output as #1 #1 "hello" close #1 open path$;"test\test.dat" for input as #1 i$=input$(#1,lof(#1)) print i$ close #1 end
|
|
|
Post by xxgeek on Dec 6, 2022 1:03:08 GMT -5
After considering all the options checking for file existence seems to work best for my intended purpose(s). Adding the function is not a problem since in most cases it will be there anyway. I won't be using the print statement either, just need the variable upath$ loaded with the userhomepath. I'll kill / delete the file too, don't like leaving files around once they've served their purpose.
Using your code I put a loop to check if test.txt exists, then open it. And added the fileExists() function. No sound, no pause, and instant results.
Your code with a loop
'all of these files will be created in DefaultDir$ open "test.bat" for output as #test #test "echo %USERPROFILE% > test.txt" close #test run "test.bat /q" ,hide do loop until fileExists(DefaultDir$, "test.txt") open "test.txt" for input as #test line input #test, path$ close #test path$=trim$(path$)+"\" print path$ print DefaultDir$
'now put a new folder and file in the userprofile result = mkdir( path$;"test") open path$;"test\test.dat" for output as #1 #1 "hello" close #1 open path$;"test\test.dat" for input as #1 i$=input$(#1,lof(#1)) print i$ close #1 end
'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
My code with the loop
call getUserPath global upath$ print upath$ end
sub getUserPath run "cmd.exe /c echo %userprofile% >UserHomePath.txt", HIDE do loop until fileExists(DefaultDir$, "UserHomePath.txt") open "UserHomePath.txt" for input as #1 upath$ = input$(#1, lof(#1)) close #1 if upath$ = "" then notice "Sorry, can't find user path " : end kill DefaultDir$;"\UserHomePath.txt" end sub
'Verify file existence function fileExists(path$, filename$) dim fileExistsInfo$(0,0) files path$, filename$, fileExistsInfo$() fileExists = val(fileExistsInfo$(0, 0)) 'non zero is true end function
|
|