Vbscripting to search for file via user input, cal

2019-09-15 17:52发布

First of all I'm really new into programming i have all these ideas on what i want to do but cannot seem to figure it out. Anyways i want a vbscript or batch file, anything at this point that when executed will ask for user input and say (Name of the file you want to search for). When i type in say hello.txt and hit enter it will then ask me another question saying where do you want me to look for these files. Then if i type in C:\ or any given drive letter it will search the entire drive letter directory including folders inside of folders if im not specific on actual path. Example c:\Program Files (x86)\ it will then search that directory and all of the folders in that directory and not the entire C:\ drive. I'm thinking to achieve this i need to call a function somehow that when i type something in a certain way it calls a function that runs a specific set of code. Example the second question asked file location so i type its location and it runs the code but replaces the location with the location i entered, this way its not only working for the location written into the code and can update and replace the line of code with user input of the location entered. Otherwise having it ask those questions were competently pointless if it doesn't have the ability to be able to replace parts of the code to adapt to user input and be more efficient, and not having to re write the script every single time you wanted to search for a new file.

Sorry lot of rambling on but i have looked everywhere found things like this but nothing close would be greatly appreciated if someone could help me out or point me in the rite direction.

This is what i have tryed for user input nothing close to what i want but here it is.

Dim Input Input = InputBox("Enter your name") MsgBox ("You entered: " & Input)

It ask for your name and then says the name you entered i need this concept but when i type that in it calls a function and executes it. Hope someone knows what I'm talking about. Thanks

1条回答
Evening l夕情丶
2楼-- · 2019-09-15 18:17

Here's a script to do that. I'll add comments to it later to explain what each part does. Right now it just outputs everything to a message box, but you can do what you want with it.

// Comments added

Dim fso, userfile, userdir, fileslist()
Set fso = CreateObject("Scripting.FileSystemObject")
ReDim fileslist(-1) ' Find results will be stored in this dynamic array

userfile = InputBox("Enter file to search for") ' Get file to search for
userdir = InputBox("Enter search directory") ' Get search directory

If Len(userfile) < 1 Then ' Check length of file name to ensure something was entered.
    MsgBox "No file name entered", 4096 + 16 ' Message box. 4096 = "System modal", essentially always on top.
ElseIf Len(userdir) < 1 Then ' Check length of dir
    MsgBox "No directory entered", 4096 + 16 ' 16 = exclamation/error
ElseIf Not fso.FolderExists(userdir) Then ' Make sure search directory actually exists
    MsgBox "Folder " & userdir & " doesn't exist", 4096 + 16
Else
    FindFile userfile, userdir ' Call FindFile sub, with the user's file and dir args passed to it
    If UBound(fileslist) >= 0 Then ' After sub completes, check whether any results were found. 
        MsgBox Join(fileslist, vbCrLf), 4096, "Results" ' If so, output the whole array ("join"), one result per line (delimited with vbcrlf)
    Else
        MsgBox "File " & userfile & " not found", 4096 + 48 ' Otherwise file not found, message stating so
    End If
End If


Sub FindFile(searchname, searchdir) ' Two parameters: file name, search directory
    On Error Resume Next ' Enable error handling so we don't crash out on access denied errors
    Dim file, folder, subfolder
    For Each file In fso.GetFolder(searchdir).Files ' Process each file (as a file object) in the search directory
        If LCase(searchname) = LCase(file.Name) Then ' See if file name matches. Using LCase to convert both to lowercase for case insensitivity.
            ReDim Preserve fileslist(UBound(fileslist) + 1) ' If match found then increase array size by 1
            fileslist(UBound(fileslist)) = file.Path ' Store the file path in newly added array entry
        End If
    Next
    ' Now the recursive bit. For any subfolders in current search directory, call FindFile again
    ' with (1) the same file name originally passed in as "searchname", and (2) a new search 
    ' directory of the subfolder's name. FindFile then starts again on this new directory: finds files, 
    ' adds matches to the fileslist array, then does the same on each subfolder found. This 
    ' is how it searches each subfolder (and subfolders of subfolders... etc) in a directory
    For Each subfolder In fso.GetFolder(searchdir).SubFolders
        FindFile searchname, subfolder.Path
    Next
    On Error GoTo 0
End Sub
查看更多
登录 后发表回答