vbscripting 通过用户输入搜索文件,调用函数提出一系列问题来完成任务 [英] Vbscripting to search for file via user input, calling functions to ask a series of questions to do tasks

查看:63
本文介绍了vbscripting 通过用户输入搜索文件,调用函数提出一系列问题来完成任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我对编程真的很陌生,我对自己想做的事情有所有这些想法,但似乎无法弄清楚.无论如何,我想要一个 vbscript 或批处理文件,此时执行的任何内容都会要求用户输入并说(您要搜索的文件的名称).当我输入 say hello.txt 并按回车键时,它会问我另一个问题,说您希望我在哪里查找这些文件.然后,如果我输入 C:\ 或任何给定的驱动器号,它会搜索整个驱动​​器号目录,包括文件夹内的文件夹,如果我不是特定于实际路径.示例 c:\Program Files (x86)\ 然后它将搜索该目录和该目录中的所有文件夹,而不是整个 C:\ 驱动器.我正在考虑实现这一点,我需要以某种方式调用一个函数,当我以某种方式键入某些内容时,它会调用一个运行特定代码集的函数.例如,第二个问题询问文件位置,所以我输入它的位置,它运行代码,但用我输入的位置替换该位置,这样它不仅适用于写入代码的位置,还可以更新和替换代码行用户输入的位置.否则,如果它没有能力替换部分代码以适应用户输入并提高效率,并且不必每次都重新编写脚本,那么让它问这些问题就毫无意义搜索新文件.

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.

暗淡的输入Input = InputBox("请输入您的姓名")MsgBox ("您输入了:" & Input)

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

推荐答案

这是执行此操作的脚本.稍后我将添加注释以解释每个部分的作用.现在它只是将所有内容输出到一个消息框,但你可以用它做你想做的事.

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.

//添加评论

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

这篇关于vbscripting 通过用户输入搜索文件,调用函数提出一系列问题来完成任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆