• Create a folder tree using vbscript

    Author
    Topic
    #461389

    I have been asked to create a program using vbscript that creates a folder “tree.” So, on my firm’s Z: drive, there is currently a folder called 001_Client Folder. I would like to press a button and create 3 subfolders, similar to below:

    Z:
    001_Client Folder
    Doe, Jane
    6-10-09
    42625

    Doe, Jane is the client name, inserted by the user
    6-10-09 is the date this folder is created
    42625 is the job number, inserted by the user

    Our firm is on Windows XP.

    The user should click a button and be prompted for the client name and then the job number, only and then the program creates the 3 subfolders in the 001_Client Folder.

    Something close to this code must already exist. Any suggestions?

    Thanks,
    Larry

    Viewing 2 reply threads
    Author
    Replies
    • #1170391

      Try this:

      Dim strClient, strJobNo, strPath
      Dim fso, intPos, intLen
      strClient = InputBox(“Please enter the client name”)
      strJobNo = InputBox(“Please enter the job number”)
      strPath = “Z:01_Client Folder” & strClient & “” & Month(Date)
      strPath = strPath & “-” & Day(Date) & “-” & Year(Date) & “” & strJobNo
      intLen = Len(strPath)
      Set fso = CreateObject(“Scripting.FileSystemObject”)
      If Left(strPath, 2) = “\” Then
      ‘ UNC path – skip server and share
      intPos = InStr(3, strPath, “”)
      intPos = InStr(intPos + 1, strPath, “”)
      Else
      ‘ drive letter – skip C: part
      intPos = 3
      End If
      Do
      intPos = InStr(intPos + 1, strPath, “”)
      If intPos = 0 Then
      intPos = intLen + 1
      End If
      If fso.FolderExists(Left(strPath, intPos – 1)) = False Then
      fso.CreateFolder Left(strPath, intPos – 1)
      End If
      Loop Until intPos = intLen + 1
      Set fso = Nothing

      You can add bells and whistles, such as checking that the user actually entered a client name and job number.

      • #1170636

        Try this:

        Dim strClient, strJobNo, strPath
        Dim fso, intPos, intLen
        strClient = InputBox(“Please enter the client name”)
        strJobNo = InputBox(“Please enter the job number”)
        strPath = “Z:01_Client Folder” & strClient & “” & Month(Date)
        strPath = strPath & “-” & Day(Date) & “-” & Year(Date) & “” & strJobNo
        intLen = Len(strPath)
        Set fso = CreateObject(“Scripting.FileSystemObject”)
        If Left(strPath, 2) = “\” Then
        ‘ UNC path – skip server and share
        intPos = InStr(3, strPath, “”)
        intPos = InStr(intPos + 1, strPath, “”)
        Else
        ‘ drive letter – skip C: part
        intPos = 3
        End If
        Do
        intPos = InStr(intPos + 1, strPath, “”)
        If intPos = 0 Then
        intPos = intLen + 1
        End If
        If fso.FolderExists(Left(strPath, intPos – 1)) = False Then
        fso.CreateFolder Left(strPath, intPos – 1)
        End If
        Loop Until intPos = intLen + 1
        Set fso = Nothing

        You can add bells and whistles, such as checking that the user actually entered a client name and job number.

        Hans,
        I am very interested in learning how to run this script…But I don’t where to begin. I know vbscript is differerent from VBA…and that is probably the extent of my knowledge.
        Anyway, how would I go about running your code, is this something I copy into notepad and save it with a vbs extension to make it run? Sorry, I am so lost—but maybe others will also benefit from an expanded discussion. Thank you.
        JimC

        • #1170659

          Yes, you can copy the code to a text file and save it with an extension .vbs. You can then run it from Windows Explorer by opening the .vbs file.

          Please note that the code “as is” assumes that there is a Z: drive. If you want to experiment with it, you could change the line

          strPath = “Z:01_Client Folder” & strClient & “” & Month(Date)

          to

          strPath = “C:” & strClient & “” & Month(Date)

          It will then create folders on the C: drive.

          • #1170698

            Yes, you can copy the code to a text file and save it with an extension .vbs. You can then run it from Windows Explorer by opening the .vbs file.

            Please note that the code “as is” assumes that there is a Z: drive. If you want to experiment with it, you could change the line

            strPath = “Z:01_Client Folder” & strClient & “” & Month(Date)

            to

            strPath = “C:” & strClient & “” & Month(Date)

            It will then create folders on the C: drive.

            Hans,
            Again many thanks for your extreme patience and willingness to further explain solutions…I have learned so much from reading your posts—whether in response to my questions or others. Thanks again for your GREAT contribution to the lounge. JimC

            • #1170728

              Your’re welcome! I appreciate your kind words, thank you!

      • #1171062

        Hans,
        Thank you so much.
        Larry

    • #1171816

      I found the following code on Microsoft’s website and modified it to determine if the Z: drive is mapped. It looks at all of the drives attached to your PC.

      I want to determine if (1) the Z: drive is mapped and ready and if not, to (2) map Z: drive. Is there a simple way to accomplish these two tasks? Also, if the program can’t find the Z: drive or you’re not attached to a network that has a Z: drive, to (3) prompt the user (msgbox “Z drive not found and can’t be mapped to this PC”). This is a bit much, so I understand if this is beyond the scope of Woody’s Lounge.

      Also, I found that some of the programs on Microsoft’s website use the WScript.Network object model. When should a programmer use the Scripting.FileSystemObject versus the WScript.Network? What is the difference?

      Function CheckDriveList()

      ‘Checks Drive List for Z:

      ‘Original code from Drives Collection
      ‘Progran name: ShowDriveList on MSDN website

      Dim fso, d ‘, S ‘n
      Set fso = CreateObject(“Scripting.FileSystemObject”)
      Set dc = fso.Drives
      For Each d In dc
      If d.DriveLetter = “Z” Then
      If d.DriveType = 3 And d.IsReady Then
      CheckDriveList = True ‘n = d.VolumeName
      Set fso = Nothing
      Exit Function
      Else
      CheckDriveList = False ‘n = “[Drive not ready]”
      Set fso = Nothing
      Exit Function
      End If
      Else
      CheckDriveList = False
      End If
      Next
      Set fso = Nothing
      End Function

      • #1171819

        Scripting.FileSystemObject is a general object that lets you work with drives, folders and files, as well as manipulate text files. It does not have specific network-related features.

        WScript.Network is, as the name indicates, network-oriented. It lets you create and delete drive mappings and printer connections. It does not offer methods for working with folders and files.

        So they have quite different purposes. You can use them side by side, depending on what you want to do.

        Hey, Scripting Guy! How Can I Map a Drive, Copy a File to That Drive, and Then Unmap the Drive? shows how to use WScript.Network to map a drive, then use Scripting.FileSystemObject to copy a file to that drive.

        • #1171831

          Hans,
          Thank you again for your continued assistance.
          All the best.
          Larry

    • #1171899

      Hans,
      Once again, you are an excellent resource. Thank you very much.
      Larry

    Viewing 2 reply threads
    Reply To: Create a folder tree using vbscript

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: