【问题标题】:How can I pass the argument in vbs如何在 vbs 中传递参数
【发布时间】:2022-10-21 20:29:01
【问题描述】:

我是一个 linux 人,并且是 windows 新手,我正在尝试创建一个小的 vbs 脚本来创建一堆文件夹。 我试图根据用户输入创建一个主文件夹,并在其中创建具有静态名称的 4 个子文件夹。我不知何故为主文件夹和子文件夹获得了两个单独的工作脚本,但我想将它们结合起来,这意味着一旦根据用户输入创建主文件夹,接下来它应该在其下创建子文件夹。

根据用户输入创建 Mainfolder.vbs 的脚本:

strfolder = InputBox("Please enter a name for your new folder:")
set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CreateFolder "C:\test\" & strfolder

创建 subfolders.vbs 文件夹的脚本:

Option Explicit

dim wshShell
set wshShell = wscript.CreateObject("WScript.Shell")
DIM fSO

DIM foldername1
DIM foldername2
DIM foldername3
DIM foldername4

foldername1=("subfolder1")
foldername2=("subfolder2")
foldername3=("subfolder3")
foldername4=("subfolder4")

dim folderpath
SET FSO=CreateObject("Scripting.FileSystemObject")

folderpath = "C:\test" & _        
             "\" & foldername1
wscript.echo "Creating folder: " & folderpath
FSO.CREATEFOLDER(folderpath)

folderpath = "C:\test" & _            
             "\" & foldername2 
wscript.echo "Creating folder: " & folderpath
FSO.CREATEFOLDER(folderpath)

folderpath = "C:\test" & _
             "\" & foldername3 
wscript.echo "Creating folder: " & folderpath
FSO.CREATEFOLDER(folderpath) 

folderpath = "C:\test" & _
             "\" & foldername4 
wscript.echo "Creating folder: " & folderpath
FSO.CREATEFOLDER(folderpath)

简而言之,如何根据用户输入创建主文件夹和具有静态名称的子文件夹?

想将这两者结合起来并作为一个脚本一起工作。

【问题讨论】:

    标签: windows vbscript


    【解决方案1】:

    你可以试试这个子程序:智能创建文件夹(strFolder)


    Sub SmartCreateFolder(strFolder)
        With CreateObject("Scripting.FileSystemObject")
            If Not .FolderExists(strFolder) then
                SmartCreateFolder(.getparentfoldername(strFolder))
                .CreateFolder(strFolder)
            End If
        End With 
    End Sub
    

    组合后的整个脚本可以这样写:

    Option Explicit
    Const Title = "SmartCreateFolder"
    Const Time2Wait = 2
    Dim ws,MainFolderPath,MainFolderName,SubFolderPath,Arr_SubFolder_Name,i
    Set ws  = CreateObject("wscript.shell")
    Arr_SubFolder_Name = Array("subfolder1","subfolder2","subfolder3","subfolder4")
    
    MainfolderName = InputBox(_
    "Please enter a name for your new folder :",Title,"Type the name here")
    If MainfolderName = "" Then Wscript.Quit(1)
    
    MainFolderPath = "C:	est" & MainfolderName
    
    For i=LBound(Arr_SubFolder_Name) To UBound(Arr_SubFolder_Name)
        Call SmartCreateFolder(MainFolderPath)
        SubFolderPath = MainFolderPath & "" & Arr_SubFolder_Name(i)
        Call SmartCreateFolder(SubFolderPath) 
    Next
    ws.run "Explorer " & MainFolderPath
    '---------------------------------------------------------------
    Sub SmartCreateFolder(strFolder)
        With CreateObject("Scripting.FileSystemObject")
            If Not .FolderExists(strFolder) then
                ws.Popup "Creating folder: " & StrFolder,Time2Wait,_
                Title,vbInformation+vbSystemModal
                SmartCreateFolder(.getparentfoldername(strFolder))
                .CreateFolder(strFolder)
            End If
        End With 
    End Sub
    '---------------------------------------------------------------
    

    【讨论】:

    • @user692942 答案已编辑参考您的评论!谢谢 !祝你有愉快的一天 !
    • 绝对是更好的方法。
    • 非常感谢您的帮助。它可以无缝运行。但它会覆盖存在的文件夹。如果文件夹存在,如何添加 if 条件以停止?
    • @Vinoth 通过添加 If 语句和 checking if the folder exists
    • @Vinoth 只是为了清楚代码不会覆盖存在的文件夹,而只是重复 MsgBox。无论如何,我调整了上面的代码,试一试并告诉我结果!因此,编辑代码是为了添加仅用于创建新文件夹的弹出消息。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-20
    • 2012-12-02
    • 2013-08-24
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多