I'm writing a vbs file and I need to promt the user to choose a folder. Is there a way to do this with vbscript (maybe using other object models or win32 api) ?
Thanx
I suppose what you want is to have the user to input a directory string of his/her liking.
A simple loop does the job. But the main work you have to supplement is to parse the inputted string to determine it meets all the characteristic feature of it being a directory at all before proceding to perform the rest of the job.
Dim sFolder, flag
flag = False
Do While flag = False
sFolder = InputBox("Please input your folder : ",,"C:\"
sFolder=trim(sFolder)
If sFolder = "" Then
sFolder = to_quit
End If
'
' You have better to verify the characteristic features of a folder are met.
'
If (MsgBox("You have chosen : " & sFolder & vbCrLf & _
"Are you sure ?", vbYesNo+vbQuestion) = vbNo) Then
flag = False
Else
flag = True
End If
Loop
WScript.Echo "Your choice : " & sFolder & " has been accepted."
If sFolder = to_quit Then WScript.Quit(1)
As there seems to be no volunteer to fill in the gap answering your question after I'd posted above, I can, nevertheless, provide you a script which takes it the hard way.
First, a few words on the easy way in line with my above posting. You can specify the fixed directory choices in the inputbox message and let the user to make a multiple choice characterized by a series of numbers corresponding to the directories themselves. Then you may parse those numbers to determine what have been chosen. So, it is only a couple of obvious steps forward to develop it from that I posted.
The hard way I am going to post here is to use the IE frondend. I was hesitating to post it because it looks really silly because it is long script. For a simple job of the kind, it may look really out of proportion. Even though the script below is not of production grade, I had built quite a few dynamical features in it which are applicable to a very broad multiple choice jobs. The core of the script is the function <checkedTableArray> which return a boolean array holding the information of the choice the user made. The main part calling the function is scripted for illustration purpose only. You can change it to more complicated & reasonable situations as you like and the function has the built-in functionality to handle that.
(Use highlight-copy-paste to make a copy of the script to avoid misleading linewrap etc.)
Try the script out, in particular use the function in stressing environment, and see how you like it.
regards - tsuji
PS I would invite other members to try it out and feedback your testing comments. I like to know that too to make future improvements. So, any comments are much appreciated.
Dim info
info = ""
If errcode<>0 Then
WScript.Echo "Unexpected error occurred. Operation aborted." & vbCrLf &_
"Error Code = " & errcode
Else
For i=0 to UBound(exDirArray)
info=info & exDirArray(i) & vbTab & exDirPassArray(i) & vbCrLf
Next
WScript.Echo info
End If
Dim option_dim
option_dim = UBound(strOptionArray)
Dim i
ReDim InputNameArray(option_dim)
ReDim takeInputValueArray(option_dim)
ReDim InputValueArray(option_dim)
For i = 0 to option_dim
InputNameArray(i) = chr(65+i) 'chr(65)="A"
InputValueArray(i) = cstr(i)
takeInputValueArray(i) = False
Next
Dim InputForm
InputForm = "InputForm"
Dim html
html = ""
html=html & "<html><head></head><body>" & vbCrLf
Dim maxchar
maxchar = 0
For i = 0 to option_dim
If (maxchar <= len(strOptionArray(i))) Then maxchar = len(strOptionArray(i))
Next
If maxchar<len("Submit" Then maxchar = len("Submit"
If maxchar<len("strCaption" Then maxchar = len("strCaption"
oIE.width = BoxWidth
oIE.height = BoxHeight
oIE.menubar = 0
oIE.toolbar = 0
oIE.navigate("about:blank"
While oIE.Busy : WScript.Sleep 100 : WEnd
oIE.document.write( html )
oIE.document.close
oIE.FullScreen = True
oIE.document.ParentWindow.resizeto BoxWidth, BoxHeight
oIE.document.ParentWindow.moveto oIE.document.ParentWindow.screen.width - BoxWidth -60,0
oIE.document.ParentWindow.document.body.style.backgroundcolor="lightsteelblue" '"Lavender"
If option_dim < 25 Then
oIE.document.ParentWindow.document.body.scroll="no"
Else
oIE.document.ParentWindow.document.body.scroll="yes"
End If
oIE.visible = True
While oIE.Busy : WScript.Sleep 100 : WEnd
On Error Resume Next
Do
WScript.Sleep 200
Loop While (oIE.document.script.CheckVal()=False)
If Err <> 0 Then
oIE.Quit(1)
Set oIE = Nothing
errcode=Err
checkedTableValueArray = takeInputValueArray
Exit Function
End If
On Error Goto 0
For i =0 to option_dim
takeInputValueArray(i)=oIE.document.InputForm(i).checked 'True= -1, False = 0
Next
oIE.Quit()
Set oIE = Nothing
checkedTableArray = takeInputValueArray
End Function 'checkedTableArray as boolean array
'------------------------------------------------------------------------------------------------------
'------------/tsuji/---------------------
There are several examples given in VBscript for getting a folder and it's contents. Since this is a Vbscript forum, I'm assuming you are not using ASP pages.
foldername = Inputbox("Enter foldername> ", "Get Folder"
Set fso = CreateObject("Scripting.FileSystemObject"
If NOT (fso.FolderExists(foldername)) Then
msgbox foldername & " doesn't exist."
wscript.quit
End If
Set Fldr = fso.GetFolder(foldername)
Set Subf = Fldr.SubFolders
s = ""
'
For Each f1 in Subf
If f1.name = "" then
Msgbox "No folders found"
wscript.quit
End If
s = s & f1.name
Next
Msgbox s
The following requires Win95/NT4 or later and also IE4 or later:
Set oShell = CreateObject("Shell.Application"
Set oFolder = oShell.BrowseForFolder(0,"Title",0,"C:\"
WScript.Echo oFolder.Items.Item(0).Path Jon Hawkins
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.