Post by xxgeek on Jul 16, 2021 22:26:45 GMT
Uses wscript, so no black window popping up.
Saves and retrieves path for use in variable.
Could be adapted for a function, or sub
Saves and retrieves path for use in variable.
Could be adapted for a function, or sub
q$ = chr$(34) ' quotes
nomainwin 'don't open the mainwin (text window)
'write the following vbs script to temp file named FolderDialog.vbs
folderDialog$ = "FolderDialog.vbs"
open folderDialog$ for output as #1
'print each line to file (FolderDialog.vbs)
#1,"Function BrowseFolder( myStartLocation, blnSimpleDialog )"
#1,"Const MY_COMPUTER = &H11&"
#1,"Const WINDOW_HANDLE = 0"
#1,"Dim numOptions, objFolder, objFolderItem"
#1,"Dim objPath, objShell, strPath, strPrompt"
#1,"strPrompt = ";q$;"Select a folder:";q$
#1,"If blnSimpleDialog = True Then"
#1,"numOptions = 0"
#1,"Else"
#1,"numOptions = &H10&"
#1,"End If"
#1,"Set objShell = CreateObject( ";q$;"Shell.Application";q$;" )"
#1,"If UCase( myStartLocation ) = ";q$;"MY COMPUTER";q$;" Then"
#1,"Set objFolder = objShell.Namespace( MY_COMPUTER )"
#1,"Set objFolderItem = objFolder.Self"
#1,"strPath = objFolderItem.Path"
#1,"Else"
#1,"strPath = myStartLocation"
#1,"End If"
#1,"Set objFolder = objShell.BrowseForFolder( WINDOW_HANDLE, strPrompt, _"
#1,"numOptions, strPath )"
#1,"If objFolder Is Nothing Then"
#1,"BrowseFolder = ";q$;q$
#1,"Exit Function"
#1,"End If"
#1,"Set objFolderItem = objFolder.Self"
#1,"objPath = objFolderItem.Path"
#1,"BrowseFolder = objPath"
#1,"End Function"
#1,"Dim bf"
#1,"bf = BrowseFolder( ";q$;"My Computer";q$;", False )"
#1,"Dim fso : Set fso = CreateObject(";q$;"Scripting.FileSystemObject";q$;")"
#1,"Dim sp : sp = fso.GetFile(Wscript.ScriptFullName)"
#1,"Dim fp: fp = fso.GetParentFolderName(sp) & ";q$;"\FolderPath.txt";q$
#1,"Dim f : Set f = fso.CreateTextFile(fp)"
#1,"f.WriteLine bf"
#1,"f.Close"
close #1
'run the script (creates temp file FolderPath.txt)
run "wscript ";"FolderDialog.vbs"
'loop until the selected folder path is written to temp file (FolderPath.txt)
do
folderpath$ = "FolderPath.txt"
res = fileExists(DefaultDir$,folderpath$)
if res then exit do
scan
loop until res
'get the text in FolderPath.txt (the selected path of folder)
open "FolderPath.txt" for input as #1
line input #1, line$(x)
selectedpath$ = line$(x)
close #1
'delete both temp files (FolderPath.txt, and FileDialog.vbs)
res = fileExists(DefaultDir$,folderpath$)
if res then kill folderpath$
res = fileExists(DefaultDir$,folderDialog$)
if res then kill folderDialog$
if selectedpath$ = "" then end
'show path is retrieved with a notice
notice "Selected Folder = ";selectedpath$
end
'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