Have a look at this It is tested on Win10, uses "clip" to pass path into clipboard .3 second is a guess You better check if you got something starting from "C:\Users\" as a result before trying to create things in there. EDIT: added a check
run "cmd.exe /c echo %userprofile% |clip" call pause 300 '.3 sec for windows to do the job
open "aux clip window" for text as #clipText #clipText "!paste" #clipText "!contents? value$" close #clipText
if instr(value$, "C:\Users\")=1 then print "And the user home folder is..." print value$ else print "OOPS failed to collect user folder" end if
sub pause mil timer mil, [timesup] wait [timesup] timer 0 end sub
EDIT changed if instr(value$, "C:\Users\") then to if instr(value$, "C:\Users\")=1 then So whole program just pasted into JB does not pass this check! %)
If you are distributing code the user is really in charge of where it is put and to where the files might be placed. Unless of course you use a fully functional installer package that can obtain permissions and be digitally signed to pass security checks.
A much simpler way is just to distribute a .zip file containing all necessary files within a single folder. Sub folders can exist in this folder.
The user will choose where to unzip this folder to, it may be a user location or it might be a root folder or indeed a secondary disc drive.
In any event you can determine where you are by using DefaultDir$
So use relative paths to sub folders in your code \bmp\my.bmp No path is necessary but DefaultDir$ will be the path.
Experiment with zipping and unzipping to different locations and experiment with DefaultDir$ and associated StartupDir$
There is a cleaner method with no ugly cmd window showing up, and if you don't mind a bit more code. It may be a bit faster too.
This uses a vbs script to write to a file the path to Home dir, then gets the path and writes to the mainwin, but you could use the variable path$ to write a file instead. It was something I was going to get around to, and your post gave me a reason to get in gear.
Title - Get_User_Home_Dir_Path Author - xxgeek (code always free to use for ANY purpose)
'nomainwin
'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" 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) path$ = line$(x) 'text stating path of user Home Dir if path$ = "" then notice "Sorry, can't find path " close #1
'print the line to mainwin (or do whatever you want with the path using path$ variable) print " ";path$
'delete the 2 temp files created by this code (userHomePath.vbs and UserHomePath.txt) res = fileExists(DefaultDir$,"UserHomePath.txt") if res then kill userHomeDir$ res = fileExists(DefaultDir$,"userHomePath.vbs") if res then kill userHomePath$ wait
'function for checking file existence function fileExists(path$, filename$) dim info$(0, 0) files path$, filename$, info$() fileExists = val(info$(0, 0)) 'non zero is true end function
Would like some help as i doing some trial and error while coding
My current coding is
open "C:\test.ini" for output as #mainset
how do i put it so that the program can create file in any pc under own user homepath eg
c:\user\andy\test.ini
or
c:\user\susan\test.ini
thank you
In order to copy test.ini from c:\ each user would need to have test.ini already on their c: drive. or You could put the test.ini file along side your bas file(same dir) and copy it from there to each users Home dir.
Post all the code you are using, and describe exactly what you want to do.
tsh73 - These files need to be in their own dir, and not used from the JB IDE as "untitled" since Windows won't allow user to create files in the appdata dir of JB using vbs scripts.
The cmd window is "UGLY" even if it pops up for 1/2 a second - sticks tongue out. (if you were nicknamed DosMaster for 20 years you may think differently)
Strange thing happened when I ran your code too. Happened a few times when I ran it in Win 10 Pro Instead of producing what you intended, it wrote your whole code in mainwin for some odd reason. It may be a cmd glitch. In Lixux/WINE it writes the code you posted every time I run it, instead of giving intended output. Very strange.
The following code needs to be saved to a file, in it's own folder. When run, it should do what you are asking, but will give a runtime error "file not found" if no test.ini file is found on c: drive. Instructions are in the code to change the variable test$ = c:\test.ini" to test$ "test.ini" so it looks for test.ini in the same dir as this file is run from. If you make the change to the code, make sure you put test.ini in the same dir as this bas file is run from.
When run, there will be nothing to see. The file should be copied to the users Home dir without any messages etc.
nomainwin
'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" close #1
'verify vbs file userHomePath.vbs was written to DefaultDir$(same dir as this file is run from) do res = fileExists(DefaultDir$,"userHomePath.vbs") if res then exit do scan loop until res
'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(read) 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) path$ = line$(x) 'text stating path of user Home Dir if path$ = "" then notice "Sorry, can't find path " close #1
'change to: test$ = "test.ini" if you put test.ini in same dir as this bas file is run from) test$ = "c:\test.ini" value$ = path$;"\test.ini"
'copy c:\test.ini to each users Home Dir (each user would need to have test.ini on C drive already) open test$ for input as #mainset open value$ for output as #2 print #2, input$(#mainset, lof(#mainset)); close #2 close #mainset
'delete the 2 temp files created by this code (userHomePath.vbs and UserHomePath.txt) res = fileExists(DefaultDir$,"UserHomePath.txt") if res then kill userHomeDir$ res = fileExists(DefaultDir$,"userHomePath.vbs") if res then kill userHomePath$ wait
'function for checking file existence function fileExists(path$, filename$) dim info$(0, 0) files path$, filename$, info$() fileExists = val(info$(0, 0)) 'non zero is true end function
Strange thing happened when I ran your code too. Happened a few times when I ran it in Win 10 Pro Instead of producing what you intended, it wrote your whole code in mainwin for some odd reason. It may be a cmd glitch.
Happened to me once. I guess it goes like this:
You copy this code from a clipboard - then you run program Windows could be slow starting new things, so ... ...command did not completed in 0.3 seconds allotted by program So CLIP was not executed So you still have previous clipboard - that is source code you just copied.
And code checks if instr(value$, "C:\Users\") then but of course whole program *has* this string, and whole program passes this check! Have to be mended to if instr(value$, "C:\Users\")=1 then - will fix in posted code above
EDIT
In Lixux/WINE it writes the code you posted every time I run it, instead of giving intended output.
Does Lixux/WINE *HAS* clip.exe at all? It started from Windows Vista, so Win XP for example does not have it. And then in Win XP I execute command echo %userprofile% from command line, it returns "C:\Documents and Settings\" instead of "C:\Users\" - that might be reason, too.
tsh73 you were right about clip.exe. I copied it from my Win 10 Pro install to WINE\drive_c\windows\system32 and WINE\drive_c\windows\syswow64 dirs and it stopped giving me the code, but gave "oops failed to collect user folder" so Instead of if instr(value$, "C:\Users\")=1 then
I used if instr(value$, "C:\Users\")=0 then and it works now.
The following code using tsh73's method works in Linux\WINE after copying clip.exe from my Win 10 Pro install to WINE\drive_c\windows\system32 and WINE\drive_c\windows\syswow64 dirs (as long as test.ini exists in c:\
I'll check if it works in Win 10 Pro in a few minutes.
There will be nothing to see. The file gets copied to the User Home Dir silently. No need for any pausing here, so I took out the pause function. A lot less code than vbs way. Very fast. And no command window, just a flash.
nomainwin
'get user Home path, echo to clipboard - hide command window run "cmd.exe /c echo %userprofile% |clip" , HIDE
'paste clipboard to variable named value$ open "aux clip window" for text as #clipText #clipText "!paste" #clipText "!contents? value$" close #clipText
if instr(value$, "C:\Users\")=0 then 'change to: test$ = "test.ini" if you put test.ini in same dir as this bas file is run from) test$ = "c:\test.ini" value$ = value$;"\test.ini" 'add filename to end of user Home path
'copy c:\test.ini to each users Home Dir (each user would need to have test.ini on C drive already) open test$ for input as #mainset open value$ for output as #2 print #2, input$(#mainset, lof(#mainset)); close #2 close #mainset end else notice "OOPS failed to collect user folder" end if end
Ok, here's another anomaly. In Win 10 Pro I needed to change if instr(value$, "C:\Users\")=0 then back to if instr(value$, "C:\Users\")=1 then and it works.
For Win 10 Pro nomainwin
'get user Home path, echo to clipboard - hide command window run "cmd.exe /c echo %userprofile% |clip" , HIDE
'paste clipboard to variable named value$ open "aux clip window" for text as #clipText #clipText "!paste" #clipText "!contents? value$" close #clipText
if instr(value$, "C:\Users\")=1 then 'change to: test$ = "test.ini" if you put test.ini in same dir as this bas file is run from) test$ = "c:\test.ini" value$ = value$;"\test.ini" 'add fiename to end of user Home path
'copy c:\test.ini to each users Home Dir (each user would need to have test.ini on C drive already) open test$ for input as #mainset open value$ for output as #2 print #2, input$(#mainset, lof(#mainset)); close #2 close #mainset end else notice "OOPS failed to collect user folder" end if end
It works here on Win 10 Pro with no pause, no instr$(check), with a quick flash.
As long as test.ini exists on c:\
nomainwin
'get user Home path, echo to clipboard - hide command window run "cmd.exe /c echo %userprofile% |clip" , HIDE
'paste clipboard to variable named value$ open "aux clip window" for text as #clipText #clipText "!paste" #clipText "!contents? value$" close #clipText
'change to: test$ = "test.ini" if you put test.ini in same dir as this bas file is run from) test$ = "c:\test.ini" value$ = value$;"\test.ini" 'add fiename to end of user Home path
'copy c:\test.ini to each users Home Dir (each user would need to have test.ini on C drive already) open test$ for input as #mainset open value$ for output as #2 print #2, input$(#mainset, lof(#mainset)); close #2 close #mainset end
There was a problem if the clipboard has existing text, it was using that text as value$, so a slight pause was added with a for next loop. I know, bad coding, but it is to halt jb code while cmd writes to clipboard without a sub to pause code. Less code overall.
I also used less lines of code by compacting lines. Works in Linux/WINE too.(if clip.exe is added to system32 dir)
nomainwin 'get user Home path, echo to clipboard - hide command window - slight pause run "cmd.exe /c echo %userprofile% |clip" , HIDE : for t = 1 to 100000 : next t
'paste clipboard to variable named value$ open "aux clip window" for text as #clipText #clipText "!paste" : #clipText "!contents? value$" : close #clipText
'change test$ = "c:\test.ini" to: test$ = "test.ini" if you put test.ini in same dir as this bas file is run from) test$ = "c:\test.ini" : value$ = value$;"\test.ini"
'copy c:\test.ini to each users Home Dir (each user would need to have test.ini on C drive already) open test$ for input as #mainset : open value$ for output as #2 print #2, input$(#mainset, lof(#mainset)); : close #2 : close #mainset : end