使用VBS创建文件夹和子文件夹

时间:2015-04-02 13:58:06

标签: vbscript directory subdirectory

我希望能够在目录中创建一定数量的文件夹和子文件夹。我已经有一个代码循环并创建文件夹和子文件夹。反正有没有创建一定数量的这些文件夹?此外,我希望能够按顺序创建它们。例如,我已经有2000个文件夹。我想创建一千多个,但它会从2001年到3000年开始。我基本上想要自动化下面的代码,所以没有人必须进入并不断更改脚本中的值。谢谢!

以下是代码:

Dim oFSO,Folder
Set oFSO = CreateObject("Scripting.FileSystemObject")
For i = 1001 To 2000
 '  x=msgbox("Directorie " & i ,64, "MakeDir")

If Not oFSO.FolderExists(i) Then
  oFSO.CreateFolder i
End If
If Not oFSO.FolderExists(i & "/Text") Then
  oFSO.CreateFolder i & "/Text"
End If
If Not oFSO.FolderExists(i & "/Text") Then
  oFSO.CreateFolder i & "/Text"
End If
If Not oFSO.FolderExists(i & "/Text") Then
  oFSO.CreateFolder i & "/Text"
End If
If Not oFSO.FolderExists(i & "/TestData") Then
  oFSO.CreateFolder i & "/TestData"
End If
 Next

1 个答案:

答案 0 :(得分:1)

试一试......

BaseFolder = "C:\temp"  'Root folder to look for/create subfolders in
MaxSize = 5             'the number of characters to allow in folder name
PaddingCharacter = "0"  'padding folder names with zeros for proper sorting
NumFolders = 10         'number of additional folder to create

intStart = GetLastFolder(BaseFolder)

If IsNull(intStart) Then
    intStart = 1
Else 
    'skip
End If

For i = intStart To intStart + NumFolders
    strFolderName = BaseFolder & "\" & RightPad( i, MaxSize, PaddingCharacter )
    Wscript.Echo strFolderName
    CreateFolders(strFolderName)
Next

Function GetLastFolder(strFolder)
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set objFolder = fso.GetFolder(BaseFolder)   
   Set subFlds = objFolder.SubFolders

    For Each fld in subFlds
        s = fld.Name
    Next

    x=Len(s)    
    For i=0 to x-1
        If Mid(s,i+1,1) = "0" Then
            'skip
        Else
            s = Mid(s,i+1,x)
            Exit For
        End If
    Next 

   GetLastFolder = s
End Function


Function CreateFolders(i)
    Dim oFSO,Folder
    Set oFSO = CreateObject("Scripting.FileSystemObject")

    If Not oFSO.FolderExists(i) Then
      oFSO.CreateFolder i
    End If
    If Not oFSO.FolderExists(i & "/Text") Then
      oFSO.CreateFolder i & "/Text"
    End If
    If Not oFSO.FolderExists(i & "/Text") Then
      oFSO.CreateFolder i & "/Text"
    End If
    If Not oFSO.FolderExists(i & "/Text") Then
      oFSO.CreateFolder i & "/Text"
    End If
    If Not oFSO.FolderExists(i & "/TestData") Then
      oFSO.CreateFolder i & "/TestData"
    End If
End Function

Function RightPad( strText, intLen, chrPad )
    'Example: RightPad( "1000", 7, "0" ) = "0001234"
    'Example: RightPad( "1000", 4, "0" ) = "1000"
    RightPad = Right( String( intLen, chrPad ) & strText, intLen )
End Function