【问题标题】:How to alter this vbscript to create sub-folders如何更改此 vbscript 以创建子文件夹
【发布时间】:2015-04-28 07:42:34
【问题描述】:

我有一个工作的 vbscript,它在 Windows 2008 r2 服务器上的用户选择的目录中创建一组文件夹。我想修改它以在正在创建的主文件夹中创建其他子文件夹。我用谷歌搜索了,需要建议。 这是工作脚本:

' 26Apr2015 jkw -- q&d

Option Explicit

Dim g_fso:  Set g_fso = CreateObject("Scripting.FileSystemObject")

Dim tgt: tgt = BrowseFolder(".", False)


Dim subdirs:  subdirs = Array(_
"Accounting",_
"Anchors",_
"Approvals",_
"Bid Documents",_
"Engineering",_
"Mold Drawings",_
"Plans and Specs",_
"Revisions and Cost Changes",_
"Shops",_
"Transmittals"_
)

Dim subdir
For Each subdir in subdirs
    WScript.Echo g_fso.CreateFolder(tgt & "\" & subdir)
Next

Function BrowseFolder( myStartLocation, blnSimpleDialog )
' This function generates a Browse Folder dialog
' and returns the selected folder as a string.
'
' Arguments:
' myStartLocation   [string]  start folder for dialog, or "My Computer", or
'                             empty string to open in "Desktop\My Documents"
' blnSimpleDialog   [boolean] if False, an additional text field will be
'                             displayed where the folder can be selected
'                             by typing the fully qualified path
'
' Returns:          [string]  the fully qualified path to the selected folder
'
' Based on the Hey Scripting Guys article
' "How Can I Show Users a Dialog Box That Only Lets Them Select Folders?"
' http://www.microsoft.com/technet/scriptcenter/resources/qanda/jun05/hey0617.mspx
'
' Function written by Rob van der Woude
' http://www.robvanderwoude.com
    Const MY_COMPUTER   = &H11&
    Const WINDOW_HANDLE = 0 ' Must ALWAYS be 0

    Dim numOptions, objFolder, objFolderItem
    Dim objPath, objShell, strPath, strPrompt

    ' Set the options for the dialog window
    strPrompt = "Select a folder in which to create subdirectories:"
    If blnSimpleDialog = True Then
        numOptions = 0      ' Simple dialog
    Else
        numOptions = &H10&  ' Additional text field to type folder path
    End If

    ' Create a Windows Shell object
    Set objShell = CreateObject( "Shell.Application" )

    ' If specified, convert "My Computer" to a valid
    ' path for the Windows Shell's BrowseFolder method
    If UCase( myStartLocation ) = "MY COMPUTER" Then
        Set objFolder = objShell.Namespace( MY_COMPUTER )
        Set objFolderItem = objFolder.Self
        strPath = objFolderItem.Path
    Else
        strPath = myStartLocation
    End If

    Set objFolder = objShell.BrowseForFolder( WINDOW_HANDLE, strPrompt, _
                                              numOptions, strPath )

    ' Quit if no folder was selected
    If objFolder Is Nothing Then
        BrowseFolder = ""
        Exit Function
    End If

    ' Retrieve the path of the selected folder
    Set objFolderItem = objFolder.Self
    objPath = objFolderItem.Path

    ' Return the path of the selected folder
    BrowseFolder = objPath
End Function

任何帮助将不胜感激。

【问题讨论】:

    标签: vbscript


    【解决方案1】:

    类似的东西。

    For Each subdir in subdirs
        set subdirnew = g_fso.CreateFolder(tgt & "\" & subdir)
        'then repeat for your other folders
        g_fso.CreateFolder(subdirnew.path & "\" & subsubdir)
    Next
    

    【讨论】:

    • 我是否应该在不按照以下建议更改数组的情况下尝试此操作?
    • 不错的尝试触发器!不过感谢您的努力:~)
    • 我的方式需要一个名为 subsubdir 的额外变量。您应该使用此处显示的两种模式中的任何一种来完成您的任务。
    【解决方案2】:

    您可以像这样将它们添加到您的列表中:

    Dim subdirs:  subdirs = Array(_
    "Accounting",_
    "Anchors",_
    "Approvals",_
    "Approvals\NewFolder",_
    "Approvals\NewFolder\Something",_
    "Approvals\AnotherFolder",_
    "Bid Documents",_
    "Engineering",_
    "Mold Drawings",_
    "Plans and Specs",_
    "Revisions and Cost Changes",_
    "Shops",_
    "Transmittals"_
    )
    

    只要确保它们是有序的,例如在 Approvals\NewFolder 之前不能有 Approvals\NewFolder\Something,因为 Approvals\NewFolder 还不会被创建。

    【讨论】:

    • 仅此一项并不能完成。它会生成一个错误“文件已存在”。
    • 我的错误——我忘记删除之前试用的文件夹。对于空目录,它会生成第 28 行 char 5 variable is undefined "subdirnew" 错误。这来自 Trigger 的代码添加。
    • 删除 Trigger 的建议后,Dan 的代码有效。呃……忘记删除这些文件夹都是我的错。感谢您的帮助!
    • 还有一个小问题。在选择要在其中创建文件夹的选定目录后,每次创建文件夹时,都会向用户显示带有“确定”按钮的对话框。正如我所说,这是次要的,但我知道我的最终用户会抱怨需要多次单击“确定”。我们可以让它只是一个 OK 确认吗?我喜欢让用户停下来检查他们的位置的想法,但一次就足够了,对吧?
    • 这是因为您正在使用 WScript.Echo Echo 告诉它将结果“回显”到屏幕上。我对 VBS 并没有真正失望,但您“应该”也许可以删除 echo 命令?
    猜你喜欢
    • 1970-01-01
    • 2021-03-14
    • 1970-01-01
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多