我该如何通过VBS中的论点

发布于 2025-02-05 09:08:39 字数 1465 浏览 2 评论 0原文

我是Linux的家伙,是Windows的新手,我正在尝试创建一个小型VBS脚本来创建一堆文件夹。 我试图根据用户输入和4个带有静态名称的子文件夹创建一个主基础。我以某种方式为MainFolder和Subfolder提供了两个分开的工作脚本,但是我想将它们结合起来,这意味着一旦根据用户输入创建了MainFolder,它应该在下面创建子文件夹。

用户输入创建mainfolder.vbs:

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

脚本创建子文件夹。

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)

脚本根据 名字?

希望将两者结合在一起,并作为一个脚本一起工作。

I'm a linux guy and new to windows, im trying to create a small vbs script to create bunch of folders.
Im trying to create a mainfolder according to user input and 4 subfolder in it with static names. I somehow got two seperate working scripts for mainfolder and subfolder but I would like to combine them both which means once the mainfolder is created as per user input next it should create subfolders below that.

Script which creates Mainfolder.vbs as per user input:

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

Script which creates subfolders.vbs folders:

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)

In short, how can I create a mainfolder as per user input and subfolders with static names?

would like to combine these both and work together as a single script.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼趣 2025-02-12 09:08:40

您可以尝试使用此子例程: smartcreatefolder(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:\test\" & 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
'---------------------------------------------------------------

You can try with this subroutine : SmartCreateFolder(strFolder)


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

The combined whole script can be written like that :

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:\test\" & 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
'---------------------------------------------------------------
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文