VB GUI界面运行VBS脚本 [英] VB GUI Interface to run VBS Script

查看:165
本文介绍了VB GUI界面运行VBS脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VBS脚本,我需要每月运行一次,以捕获文件信息suhc作为文件名,类型,修改日期等信息.当我处理每个文件时,它将全部保存到CSV文件中,以便可以在Excel上进行处理.

I have a VBS script which I need to run on a monthly basis which captures file information suhc as file Name, Type, Date Modified and more. When I processes each file it saves it all onto a CSV file so that I can process it on Excel.

要运行脚本,请设置一个批处理文件.bat

To run the script I setup a batch file .bat

问题是我需要某种GUI界面,以便在运行批处理或vbs文件时,它将要求用户输入要扫描的驱动器号.

The issue is I need a GUI interface of some sort so that when the batch or vbs file is run it will ask the user to enter a drive letter to scan.

这是我的代码:

test.vbs:
Option Explicit
Dim objFS, objFld
Dim objArgs
Dim strFolder, strDestFile, blnRecursiveSearch
Dim strLines()
Dim i
Dim strCsv

    i = 0

'   'Get the commandline parameters
'   Set objArgs = WScript.Arguments 
'   strFolder = objArgs(0)
'   strDestFile = objArgs(1)
'   blnRecursiveSearch = objArgs(2)

    '###################################
    'MAKE SURE THESE VALUES ARE CORRECT
    '###################################
    strFolder = "C:\" 
    strDestFile = "C:\test\Output.csv" 
    blnRecursiveSearch = True

    'Create the FileSystemObject
    Set objFS=CreateObject("Scripting.FileSystemObject")
    'Get the directory you are working in 
    Set objFld = objFS.GetFolder(strFolder)

    'Now get the file details
    GetFileDetails objFld, blnRecursiveSearch 

    'Write the csv file
    Set strCsv = objFS.CreateTextFile(strDestFile, True)
    strCsv.Write Join(strLines, vbCrLf)

    'Close and cleanup objects
    strCsv.Close
    Set strCsv = Nothing
    Set objFld = Nothing
    Set strFolder = Nothing
    Set objArgs = Nothing


Private Sub GetFileDetails(fold, blnRecursive)
Dim fld, fil
dim strLine(5)

    If blnRecursive Then
        'Work through all the folders and subfolders
        For Each fld In fold.SubFolders
            GetFileDetails fld, True 
        Next
    End If

    'Now work on the files
    For Each fil in fold.Files
        strLine(0) = fil.Path
        strLine(1) = fil.Type
        strLine(2) = fil.Size
        strLine(3) = fil.DateCreated
        strLine(4) = fil.DateLastModified
        strLine(5) = fil.DateLastAccessed

        Redim Preserve strLines(i)
        strLines(i) = Join(strLine, ",")
        i = i + 1
    Next
end sub

还有run.bat

cscript.exe C:\script\test.vbs

如您所见,test.vbs指定要扫描和捕获的部分.代码:strFolder = "C:\"

As you can see test.vbs specifies what section to scan and capture. code: strFolder = "C:\"

您的最佳建议是什么,运行此命令的人比使用VB的人要经验少得多,因此他们将需要某种GUI界面,要求输入驱动器号,然后将行代码strFolder = "C:\"修改为他们输入的任何驱动器号,然后运行test.vbs.

What would be your best recommendation, the people running this are alot more less experienced then me with VB, so they will need some sort of GUI interface that will ask for a drive letter input and then modify line code strFolder = "C:\" to whatever drive letter they entered and then run test.vbs.

感谢您的帮助.

:)干杯

推荐答案

将驱动器号添加为vbscript的参数将是最简单的.

It would be easiest just to add the drive letter as a parameter of your vbscript.

Set oArgs = WScript.Arguments
DriveLetter = oArgs(0)
strFolder = DriveLetter & ":\"

然后,您可以像添加驱动器号一样运行脚本.

Then you can just run the script like you were with the drive letter appended.

cscript.exe C:\script\test.vbs C

然后,如果用户确实需要脚本,则可以将其包装在VB GUI中(如之前建议的输入框所示).或者更好的是,您的脚本可以只要求他们键入驱动器号.

You can then wrap the script in VB GUI (input box as suggested before) if the users really need it. Or better yet your script could just ask them to type in the drive letter.

旁注(取决于您使用的Windows版本以及日期的使用方式),dir命令将使用/t开关打印特定的文件日期.因此,dir /ta将打印上次访问的日期.不幸的是,它一次只能执行一次(访问,修改,创建).您可能可以使用它并将其通过管道传输到文件(dir /ta > Output.txt),而不用编写单独的脚本.

Side note (depending on which version of windows you are using and how you need the dates), the dir command will print a specific file date using the /t switch. So dir /ta would print the last accessed date. Unfortunately it only does one (accessed, modified, created) at a time. You might be able to use that and pipe it to a file (dir /ta > Output.txt) instead of writing a separate script.

这篇关于VB GUI界面运行VBS脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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