Results 1 to 11 of 11
  1. #1
    Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    54
    Thanks
    6
    Thanked 1 Time in 1 Post
    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

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    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:\001_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.

  3. #3
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Canton, Ohio, USA
    Posts
    270
    Thanks
    3
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='785736' date='22-Jul-2009 08:48']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:\001_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.[/quote]


    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

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    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:\001_Client Folder\" & strClient & "\" & Month(Date)

    to

    strPath = "C:\" & strClient & "\" & Month(Date)

    It will then create folders on the C: drive.

  5. #5
    3 Star Lounger
    Join Date
    Mar 2001
    Location
    Canton, Ohio, USA
    Posts
    270
    Thanks
    3
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='785965' date='23-Jul-2009 14:46']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:\001_Client Folder\" & strClient & "\" & Month(Date)

    to

    strPath = "C:\" & strClient & "\" & Month(Date)

    It will then create folders on the C: drive.[/quote]


    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

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Your're welcome! I appreciate your kind words, thank you!

  7. #7
    Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    54
    Thanks
    6
    Thanked 1 Time in 1 Post
    Hans,
    Thank you so much.
    Larry

  8. #8
    Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    54
    Thanks
    6
    Thanked 1 Time in 1 Post
    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

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    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.

  10. #10
    Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    54
    Thanks
    6
    Thanked 1 Time in 1 Post
    Hans,
    Thank you again for your continued assistance.
    All the best.
    Larry

  11. #11
    Star Lounger
    Join Date
    Dec 2002
    Location
    San Francisco, California, USA
    Posts
    54
    Thanks
    6
    Thanked 1 Time in 1 Post
    Hans,
    Once again, you are an excellent resource. Thank you very much.
    Larry

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •