Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do I open a directory in wsh ?

Status
Not open for further replies.

stemy

Programmer
Aug 25, 2001
39
IL
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
 
Hello, stemy.

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.

regards - tsuji

'-----------------------xxxx.vbs file------
Option Explicit

Const to_quit = "to Quit"

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)

'
' your other job here
'

WScript.Quit
'-----------------------------------------
 
Upon re-reading your message, maybe you are looking for a choice from a menu. If yes, the above won't do it, so, please ignore it.

regards - tsuji
 
Hello, again.

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.

'------------/tsuji/---------------------
Option Explicit

Dim strRequest, errcode, i
strRequest = &quot;Exposed Directories&quot;
Dim exDirArray, exDirPassArray
exDirArray = Array( &quot;E:\DataBank\Financial&quot;,&quot;E:\DataBank\Managerial&quot;,_
&quot;E:\DataBank\Personnel&quot;,&quot;E:\Databank\Vendors&quot;,_
&quot;E:\DataBank\Customers&quot;,&quot;E:\DataBank\Board_Minutes&quot;)

exDirPassArray = checkedTableArray(exDirArray, strRequest, errcode)

Dim info
info = &quot;&quot;
If errcode<>0 Then
WScript.Echo &quot;Unexpected error occurred. Operation aborted.&quot; & vbCrLf &_
&quot;Error Code = &quot; & errcode
Else
For i=0 to UBound(exDirArray)
info=info & exDirArray(i) & vbTab & exDirPassArray(i) & vbCrLf
Next
WScript.Echo info
End If

WScript.Quit

'----------------/return checkbox table/-----------------------------------------------------------------
Function checkedTableArray(ByVal strOptionArray, strCaption, ByRef errcode) 'as boolean array

errcode = 0 'reset errcode

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)=&quot;A&quot;
InputValueArray(i) = cstr(i)
takeInputValueArray(i) = False
Next

Dim InputForm
InputForm = &quot;InputForm&quot;

Dim html
html = &quot;&quot;
html=html & &quot;<html><head></head><body>&quot; & vbCrLf

html=html & &quot;<Script Language=&quot;&quot;VBScript&quot;&quot;>&quot;&vbCrLf
html=html &&quot;<!--&quot;&vbCrLf&&quot;Dim done&quot;&vbCrLf&&quot;Public TheForm&quot;&vbCrLf
html=html & &quot;Sub buttonsubmit_onClick&quot;&vbCrLf&&quot;done=True&quot;&vbCrLf&&quot;End Sub&quot;&vbCrLf
html=html & &quot;Sub window_onLoad()&quot;&vbCrLf&&quot;Set TheForm = document.InputForm&quot;&vbCrLf &&quot;done=False&quot;&vbCrLf&&quot;End Sub&quot;&vbCrLf
html=html & &quot;Public Function CheckVal()&quot;&vbCrLf&&quot;CheckVal=done&quot;&vbCrLf&&quot;End Function&quot;&vbCrLf
html=html &chr(39)&&quot;--></Script>&quot; &vbCrLf

html=html & &quot;<form name=&quot; & chr(34) & InputForm & chr(34) & &quot;><center><p>&quot;&vbCrLf
html=html & &quot;<table BORDER==0 CELLSPACING=4 CELLPADDING=4 WIDTH=220 >&quot;&vbCrLf
html=html & &quot;<caption align=&quot;&quot;top&quot;&quot;><strong>&quot; & strCaption & &quot;</strong></caption>&quot;&vbCrLf
html=html & &quot;<tr><td COLSPAN=2>&quot;&vbCrLf

ReDim strTbl(option_dim)
For i =0 to option_dim
strTbl(i) = &quot;<input type=&quot;&quot;checkbox&quot;&quot; name=&quot; & chr(34) & InputNameArray(i) & chr(34) & _
&quot; &quot;&&quot;value=&quot; & chr(34) & InputValueArray(i) & chr(34) & &quot;>&quot; & _
&quot;<font size=-1>&quot; & strOptionArray(i) & &quot;</font><br>&quot;&vbCrLf
Next
For i = 0 to option_dim
html=html & strTbl(i)
Next

html=html & &quot;</td></tr>&quot;&vbCrLf
html=html & &quot;<tr><td COLSPAN=2 bgcolor=990000><center><font color =&quot;&quot;white&quot;&quot;>&quot;&vbCrLf
html=html & &quot;<input type=&quot;&quot;button&quot;&quot; name=&quot;&quot;buttonsubmit&quot;&quot; value=&quot;&quot;Submit&quot;&quot;>&quot;&vbCrLf
html=html & &quot;</center></font></td></tr></table></p></center></form>&quot;&vbCrLf
html=html & &quot;</body></html>&quot;

Dim oIE
Set oIE = WScript.CreateObject(&quot;InternetExplorer.Application&quot;)

Dim BoxWidth, BoxHeight
Const unitWidth = 8
Const unitHeight = 28

Dim maxchar
maxchar = 0
For i = 0 to option_dim
If (maxchar <= len(strOptionArray(i))) Then maxchar = len(strOptionArray(i))
Next
If maxchar<len(&quot;Submit&quot;) Then maxchar = len(&quot;Submit&quot;)
If maxchar<len(&quot;strCaption&quot;) Then maxchar = len(&quot;strCaption&quot;)

BoxWidth = (maxchar + 8) * unitWidth
BoxHeight = (option_dim + 4) * unitHeight

oIE.width = BoxWidth
oIE.height = BoxHeight
oIE.menubar = 0
oIE.toolbar = 0
oIE.navigate(&quot;about:blank&quot;)
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=&quot;lightsteelblue&quot; '&quot;Lavender&quot;
If option_dim < 25 Then
oIE.document.ParentWindow.document.body.scroll=&quot;no&quot;
Else
oIE.document.ParentWindow.document.body.scroll=&quot;yes&quot;
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/---------------------
 
stemy,

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(&quot;Enter foldername> &quot;, &quot;Get Folder&quot;)
Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
If NOT (fso.FolderExists(foldername)) Then
msgbox foldername & &quot; doesn't exist.&quot;
wscript.quit
End If

Set Fldr = fso.GetFolder(foldername)
Set Subf = Fldr.SubFolders
s = &quot;&quot;
'
For Each f1 in Subf
If f1.name = &quot;&quot; then
Msgbox &quot;No folders found&quot;
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(&quot;Shell.Application&quot;)
Set oFolder = oShell.BrowseForFolder(0,&quot;Title&quot;,0,&quot;C:\&quot;)
WScript.Echo oFolder.Items.Item(0).Path Jon Hawkins
 
Errata
------
Just a quick correction to a typo.

In the &quot;If Err<>0 Then ... End If&quot; section of my posting #3, the line :

checkedTableValueArray = takeInputValueArray [_typo_]

should be read as :

checkedTableArray = takeInputValueArray

(a residue of modifications as I change the name of the function.)

Thanks for your attention.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top