6

I have a small hta file with few vbs codes. It selects folder or file then make a copy to a fixed location.

<html>
<head>
<Title>File Copy </Title>
<style>
img.exco
{
position:absolute;
bottom:10px;
right:10px
}
</style>
<!--Put this sub here to avoid resize flickering.-->
<script language = "VBScript">
 sub DoResize
    'resize   
    window.resizeTo 690,350
    screenWidth = Document.ParentWindow.Screen.AvailWidth
    screenHeight = Document.ParentWindow.Screen.AvailHeight
    posLeft = (screenWidth - 700) / 2
    posTop = (screenHeight - 430) / 2     
    'move to centerscreen
    window.moveTo posLeft, posTop

  end sub

DoResize()
</script>

<HTA:APPLICATION ID=""
   applicationName=""
   version="1.1"
    BORDER="thin"
    BORDERSTYLE="static"
    CAPTION="Yes"
    CONTEXTMENU="no"
    ICON="C:\icon\32x32.ico"
    INNERBORDER="no"
    MAXIMIZEBUTTON="no"
    MINIMIZEBUTTON="no"
    NAVIGATABLE="no"
    SCROLL="no"
    SCROLLFLAT="no"
    SELECTION="no"
    SHOWINTASKBAR="yes"
    SINGLEINSTANCE="yes"
    SYSMENU="yes"
    WINDOWSTATE="normal" 
>

<script language = "VBScript">


Sub BrowseSource_OnClick()
    strStartDir = "K:\Data\"
    Copy_To_PC.txtFile.value = PickFolder(strStartDir)
End Sub 

Function PickFolder(strStartDir)
Dim shell : Set shell = CreateObject("Shell.Application")
Dim file : Set file = shell.BrowseForFolder(0, "Choose a file:", &H4000)
If (Not File Is Nothing) Then
PickFolder = file.self.Path
End If
Set shell = Nothing
Set file = Nothing

End Function

Sub RunScripts_OnClick()
    Copy
    Paste
    OpenWord
End Sub

Sub Copy
End Sub

Sub Paste
            msgBox "Copy Success!"           
End Sub

Sub OpenWord      
End Sub
</script>
</head>
<body>
<p><b><font size="4">Please select the file.</font></b></p>
<form name="Copy_To_PC">
<input type = "text" name = "txtFile" size="100" />
<input type = "button" value = "File Source" Name="BrowseSource">
<input type="button" value="Copy and Paste" name="RunScripts">
</form>
</body>
</html>

I have problems with selecting items (folder or file) when I click the first button.

  1. It picks up folder well but when selecting files, I got "unspecified error on line 60", please help me to troubleshoot. I want the file browser window like the way it is now with OK button on it, not a "open" button, so I can select both folder or file.

  2. Also the file browser does not start up from the location I setup. How to fix it?

Root Loop
  • 3,004
  • 9
  • 46
  • 72
  • you pass in the strStartDir but dont use it, that is why it doesnt open in the location you specify. Replace &h4000 with your strStartDir to get it to open that location. also dont make use count to line 60, you should know which line this is already so point it out. – Sorceri Feb 20 '15 at 15:53
  • Tried that before, end up with "Line: 60 Error: Type mismatch: 'shell.BrowseForFolde"... I can't find a way to select both folder and file in the same dialog window – Root Loop Feb 20 '15 at 15:57
  • I can reproduce the error, it seems to happen when you select a file instead of a folder in the `BrowseForFolder()` dialog. Not sure how to fix that (or even if it can be fixed at all). To pass the root path you must add it as the *last* argument, i.e. *after* the `&H400`. – Tomalak Feb 20 '15 at 16:26
  • @Tomalak Thanks for the hint, I added the path after `&H400`, it worked. Now just need to find a way to pick up files... – Root Loop Feb 20 '15 at 16:36
  • or is there a way to specify the file type ? lets say if i only want to see `.rtm` if exists – Root Loop Feb 20 '15 at 16:39
  • I have a gut feeling that this is going to be very difficult. The error happens *inside* the BrowseForFile call, presumably because the function is hard-coded to return folder objects and does not know how to handle files. – Tomalak Feb 20 '15 at 16:55
  • any suggestion that what else I can do to select folder or file in the same window? – Root Loop Feb 20 '15 at 17:03

4 Answers4

3

How to select folder or file in the same window? By MSDN, The Shell.BrowseForFolder method creates a dialog box that enables the user to select a folder and then returns the selected folder's Folder object.

Thus, for browse for a file we need to use another method. There exists the native <input ...> tag with type="file" attribute. However, using it brings more cons than pros (see 2nd solution below).

In next solution, the Browse for File is achieved by using the HtmlDlgHelper object - an obscure and poorly documented object that is nevertheless available on all Windows systems... In comparison with <input ...> tag mentioned above, this solution offers to define both initial location and file type masking.

<HTML>
<HEAD>
<Title>File/Folder Copy B</Title>
<!-- Browse for 
      - file:   based on the HTMLDlgHelper class 
                registered via C:\Windows\System32\mshtmled.dll
                (.OpenFileDlg returns string)
      - folder: based on the Shell.BrowseForFolder method
                (returns the selected folder's Folder object)
-->
<HTA:APPLICATION 
    ID=""
    APPLICATIONNAME="28632270ym"
    SCROLL="no"
    SINGLEINSTANCE="yes"
    WINDOWSTATE="normal"
>

<script language="VBScript">

''''''''''''''''''''''''''
''' Global State Variables
''''''''''''''''''''''''''
  Dim sStartPath, sStartFile, sFilter, sCaption
  sStartPath = "D:\Remote"
  sStartFile = sStartPath & "\nul"
  sFilter    = "All Files (*.*)|*.*|" _
      & "VBScript (*.vbs;*.vbc)|*.vbs;*.vbc|" _
      & "HTML (*.htm;*.html;*.hta)|*.htm;*.html;*.hta|"
  sCaption = "Choose a File:"

Sub Window_Onload
    self.Focus()
    self.moveTo 1, 1
    self.ResizeTo 760,400
End Sub

Sub BrForFolder
    sBFF = PickFolder(sStartPath)
    If not sBFF = "" Then 
      document.Copy_To_PC.txtfile.value = sBFF
      document.Copy_To_PC.OKbuton.value = "Treat folder"
    End If
End Sub 

Sub BrForFile_onclick
    sBFF = Dlg.OpenFileDlg( CStr(sStartFile), , CStr(sFilter), CStr(sCaption))
    If not sBFF = "" Then 
      document.Copy_To_PC.txtfile.value = sBFF
      document.Copy_To_PC.OKbuton.value = "Treat file"
    End If
End Sub 

Function PickFolder(sStartPath)
    Dim shell, oFldr
    Set shell = CreateObject("Shell.Application")
    Set oFldr = shell.BrowseForFolder(0, "Choose a folder:" _
        , &H0001 + &H0004 + &H0010 + &H0020, sStartPath)
    'See MSDN "BROWSEINFO structure" for constants
    If (Not oFldr Is Nothing) Then
        PickFolder = oFldr.Self.Path
    Else
        PickFolder = ""
    End If
    Set shell = Nothing
    Set oFldr = Nothing
End Function

Sub DoFileOK()
    MsgBox document.Copy_To_PC.OKbuton.value _
        & " """ & document.Copy_To_PC.txtfile.value & """"
    document.Copy_To_PC.txtfile.value = ""
    document.Copy_To_PC.OKbuton.value = "Undefined"
End Sub

</script>

</HEAD><BODY>
<OBJECT id=Dlg classid="CLSID:3050F4E1-98B5-11CF-BB82-00AA00BDCE0B" 
    width=0 height=0>
</OBJECT>
<FORM  name="Copy_To_PC">
<p><b><font size="3">Please select a file or folder.</font></b></p>
<input style="width:600px;" type="text" id="txtfile" name="txtfile" />
<br>
<input type="button" value="Browse for File..."  id="BrForFile">
&nbsp;&nbsp;or&nbsp;&nbsp;
<input type="button" value="Browse for Folder..." onClick=BrForFolder()>
<br>
<br>Action&nbsp;
<input type="button" value="Undefined" id="OKbuton" onClick=DoFileOK()>
<br>
<br>Quit:&nbsp;
<input type="button" value="All done" onClick=self.window.close()>
</FORM></BODY></HTML>

2nd solution:

<html>
<head>
<Title>File/Folder Copy A</Title>
<!-- based on <input type="file"> of zero width -->
<HTA:APPLICATION 
    ID=""
    APPLICATIONNAME="28632270my"
    SCROLL="no"
    SINGLEINSTANCE="yes"
    WINDOWSTATE="normal"
>

<script language = "VBScript">
''''''''''''''''''''''''''
''' Global State Variables
''''''''''''''''''''''''''
  sStartPath = "D:\Remote"
  sFilter    = "application/pdf,text/plain"

Sub Window_Onload
    self.Focus()
    self.moveTo 1, 1
    self.ResizeTo 960,400
End Sub

Sub BrForFolder()
    sBFF = PickFolder(sStartPath)
    If sBFF = "" Then
      'do not overwrite .txtfile.value with an empty string  
    Else
      Copy_To_PC.txtfile.value = sBFF
      Copy_To_PC.OKbuton.value = "Treat folder"
    End If
    'NOT possible (security reason): Copy_To_PC.fn_file.value = ""
End Sub 

Sub BrForFile()
    '??? how to know whether a user has pressed 'Open' or 'Cancel' ??? 
    'http://stackoverflow.com/a/4628563/3439404
    'The result of the file dialog is not exposed to the browser.
    '
    '??? how to set initial working directory??? 
    ' and file types??? (MSIE does not care about the accept attribute?)
    sBFF = Copy_To_PC.fn_file.value
    If sBFF = "" Then
      'do not overwrite .txtfile.value with an empty string  
    Else
      Copy_To_PC.txtfile.value = sBFF
      Copy_To_PC.OKbuton.value = "Treat file"
    End If
End Sub 

Function PickFolder(sStartPath)
    Dim shell : Set shell = CreateObject("Shell.Application")
    Dim oFldr : Set oFldr = shell.BrowseForFolder(0, "Choose a folder:" _
        , &H0001 + &H0004 + &H0010 + &H0020, sStartPath)
    'See MSDN "BROWSEINFO structure" for constants
    'Shell.BrowseForFolder method: Creates a dialog box that 
    '   enables the user to select a folder and then returns 
    '   the selected folder's Folder object.
    If (Not oFldr Is Nothing) Then
        PickFolder = oFldr.Self.Path
    Else
        PickFolder = ""
    End If
    Set shell = Nothing
    Set oFldr = Nothing
End Function

Sub DoFileOK()
    MsgBox Copy_To_PC.OKbuton.value _
        & " """ & Copy_To_PC.txtfile.value & """"
    Copy_To_PC.txtfile.value = ""
    Copy_To_PC.OKbuton.value = "Undefined"
End Sub

</script>

</head><body><form  name="Copy_To_PC">
<p><b><font size="3">Please select a file or folder.</font></b></p>
<input style="width:600px;" type="text" id="txtfile" name="txtfile" />
<input style="width:0px;"   type="file" id="fn_file" name="fn_file" 
    accept=sFilter onChange=BrForFile() onBlur=BrForFile()>
&nbsp;or&nbsp; 
<input type="button" value="...for Folder..." onClick=BrForFolder()>
<br>
<br>Action&nbsp;
<input type="button" value="Undefined" id="OKbuton" onClick=DoFileOK()>
<br>
<br>Quit:&nbsp;
<input type="button" value="All done" onClick=self.window.close()>
</form></body></html>
JosefZ
  • 28,460
  • 5
  • 44
  • 83
  • Thanks for your great work. I was thinking the same way to make 2 buttons for folders and files separately. But I m asked to do it in one one window...your both solutions work great, I noticed that when I browse for files, the select button is "OPEN", if for folders is "OK", I only need a "OK" button, if you check my original code, it can pick folders and normal files like "txt, word, jpg,pdf" but for special files we work on, it wont pick up and getting errors. Do you know why @JosefZ – Root Loop Feb 23 '15 at 16:18
2

Here is an example of setting the start directory and the selection of files.

Const GeneratedItemFlag = &h4000

dim shellApp 
dim folderBrowseDialog
dim filePath
set shellApp = CreateObject("Shell.Application")

set folderBrowseDialog = shellApp.BrowseForFolder(0,"Select the file", GeneratedItemFlag, "c:\")


if folderBrowseDialog is nothing then
    msgbox "No file was selected.  This will now terminate."
    Wscript.Quit
else
    filePath= folderBrowseDialog.self.path
end if
Sorceri
  • 7,870
  • 1
  • 29
  • 38
  • Thanks , but this one got the same error. I am thinking maybe the cause is the file type or extension? Cus when I use your code or my code to pick up some common file type like pdf file or text file, there is no error at all, but when I pick up some of special file like `.rms or .rtm` it will get errors. – Root Loop Feb 20 '15 at 17:18
1

There is actually an error in the HTA portion of the first code example that will cause some problems down the road. The tag says NAVIGATABLE="no". It should be NAVIGABLE="no". You need to lose the AT.

Cowfool
  • 11
  • 1
0
<!-- :: Batch section
@echo off
setlocal

echo Select an option:
for /F "delims=" %%a in ('mshta.exe "%~F0"') do set "HTAreply=%%a"

tasklist /fi "imagename eq iexplore.exe" |find ":" > nul
if errorlevel 1 taskkill /f /im "iexplore.exe"

echo End of HTA window, reply: "%HTAreply%"
Echo hi...
echo %HTAreply%
call installation.cmd

-->


<HTML>
<HEAD>
<HTA:APPLICATION SCROLL="no" SYSMENU="no" >

<TITLE>Your Project Folder </TITLE>
<SCRIPT language="JavaScript">
window.resizeTo(440,170);

function validateForm() {
var x=document.forms["myForm"]["pname"].value;
if(x==""){
return false;
}
var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.GetStandardStream(1).WriteLine(x);
 window.close();
  }
  function selectPath() {

var objShell = new ActiveXObject("shell.application");
    var objFolder;

    objFolder = objShell.BrowseForFolder(0, "Example", 0, 0);
    if(objFolder){
    document.forms["myForm"]["pname"].value = objFolder.Self.Path;
    }
}
</SCRIPT>

</HEAD>
<BODY>
<form name="myForm" onsubmit="return validateForm()">
pathtostore: <input type="text" name="pname" value="c:\program files">              
<input type="button" onclick="selectPath()" value="Browse">
<br>
<br>
<input type="submit" value="ok">
</form>
</BODY>
</HTML>
Sourav Ghadai
  • 97
  • 1
  • 10