0

How can I take input as folder through GUI by using VBScript? Example: I don't want to use following Window method.

Function Browse4Folder(strPrompt, intOptions, strRoot)
    Dim objFolder, objFolderItem, objShell
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.BrowseForFolder(0, strPrompt, intOptions, strRoot) 
    If (objFolder Is Nothing) Then
        Browse4Folder = ""
    Else
        Set objFolderItem = objFolder.Self
        Browse4Folder = objFolderItem.Path      
        Set objFolderItem = Nothing
        Set objFolder = Nothing
    End If  
    Set objShell = Nothing
End Function

I want to achieve the following:

  1. I have a folder containing .h, .c, .exe, …
  2. I have written the operation to removed unwanted files (like I required only .h file).
  3. The end result should be only getting .h files.

All operation I have done but for statement (1), I have written the above snippet which provide the manual selection of the folder, but I want to make it automate (if I will run the script, all the operation should be complete without doing anything manually).

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Singh306
  • 21
  • 4
  • use a HTA in this case ! – Hackoo Mar 31 '16 at 10:00
  • If the code you gave works -- why don't you want to use it? @Hackoo 's idea of an HTA is natural but would require more work and probably have more runtime overhead. Perhaps you could find a way to use Office's file picker dialog, but again it seems like a lot of work. – John Coleman Mar 31 '16 at 10:30
  • 1
    It's not clear to me what the actual question is here. What do you want to achieve in the end (i.e. what is your use case)? Why can't you use the function you posted? What have you tried so far? – Ansgar Wiechers Mar 31 '16 at 10:38
  • You can try with an InputBox instead ! – Hackoo Mar 31 '16 at 15:19
  • Possible duplicate of [VBScript file or folder selection](http://stackoverflow.com/questions/28632270/vbscript-file-or-folder-selection) – MikeC Apr 01 '16 at 00:53
  • Thank you for your response But my requirement is to write the code to make the operation automate. I don't want to select the input (Folder) manually. – Singh306 Apr 01 '16 at 03:56
  • @user3384603 can you please describe what is the operation you want to automate with VBS? If you have some GUI application showing BrowseForFolder dialog you mention, then elaborate what is the application, and how is it launched? – omegastripes Apr 03 '16 at 09:58
  • @user3384603 Then how do you want to select the folder? And what do you want to do with it when you have it selected? If you expect anyone to answer you need to give us something to work with. – Ansgar Wiechers Apr 03 '16 at 12:06
  • Please check the following statements (1)Input = Folder (Contains .h, .c, .exe, ......) (2)I have written the operation to removed the unwanted file(like i required only .h file) (3)Output= only getting .h file All operation I have done but for statement (1), I have written the above Snippet which provide the manual selection of the folder But I want to make it automate(If I will run the Script, all the operation should be complete without doing anything manually) – Singh306 Apr 04 '16 at 06:49
  • In the future please add information like that to your question rather than burying it in a comment. Explaining exactly what you need *in your question* greatly helps getting the answer you're looking for. – Ansgar Wiechers Apr 04 '16 at 10:12

1 Answers1

1

If I understand your question correctly, you want to remove all files from a given folder, except those with a specific extension (.h). That could be achieved with a procedure like this:

Sub DeleteExcept(path, extension)
  Set fso = CreateObject("Scripting.FileSystemObject")
  For Each f In fso.GetFolder(path).Files
    If LCase(fso.GetExtensionName(f)) <> LCase(extension) Then f.Delete True
  Next
End Sub

DeleteExcept "C:\your\folder" "h"
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328