vbscript issue with creating subfolder

Author Message
mac01

  • Total Posts : 2
  • Scores: 0
  • Reward points : 0
  • Joined: 11/4/2011
  • Status: offline
vbscript issue with creating subfolder Tuesday, November 29, 2011 10:10 AM (permalink)
0
Hey Guys,
 
I'm sure this is an easy fix but I can't seem to figure it out. I am trying to create a sub directory within a users home folder but can't seem to get the sub folder created. Granted, this sub folder is based on the variable name of the user that logs in maybe that is the issue? Since it isn't static? Below is the code minus the errors I get.
 

Dim objFSO, objFolder, strComputer, strDirectory, objNet, strTest, objoutlook, objNS, objFName, objTextFile, pstFiles, pstName, strPath, pstFolder, strLocal
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists("C:\supertest\pst_script_log.txt ") Then
Wscript.Quit
Else
strTest = "C:\supertest"
Set objFolder = objFSO.CreateFolder(strTest)

End If



' get username
Dim WshNetwork: Set WshNetwork = wscript.CreateObject("WScript.Network")
Dim user: user = LCase(WshNetwork.UserName)
Set WshNetwork = Nothing



' network path to write pst files to
strLocal = "E:\Local_PST"
strDirectory = "E:\dumb\" & user & "\"       ' <-- im trying to create the sub dir "Local_PST" after the user variable, isn't working.



' Create the File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")



' Note If..Exists. Then, Else ... End If construction
If objFSO.FolderExists(strDirectory) Then

'Open the Folder if it's already created
Set objFolder = objFSO.GetFolder(strDirectory)
WScript.Echo strDirectory & " already created "

'WScript.Quit


ELSE


Set objFolder = objFSO.CreateFolder(strDirectory)
Set objFolder = objFSO.CreateFolder(strLocal)

WScript.Echo "Just created " & strDirectory





End If


 
 
#1
    59cobalt

    • Total Posts : 981
    • Scores: 91
    • Reward points : 0
    • Joined: 7/17/2011
    • Status: offline
    Re:vbscript issue with creating subfolder Tuesday, November 29, 2011 12:00 PM (permalink)
    0
    mac01
    strDirectory = "E:\dumb\" & user & "\"       ' <-- im trying to create the sub dir "Local_PST" after the user variable, isn't working.
    CreateFolder() does not create missing parent folders. You need to create the parent folders first, and then the subfolder.
    strDirectory = "E:\dumb\" & user
    objFSO.CreateFolder strDirectory
    objFSO.CreateFolder strDirectory & "\Local_PST"

    The best approach would be something like this, though:
    strDirectory = "E:\dumb\" & user & "\Local_PST"
    If objFSO.DriveExists(objFSO.GetDriveName(strDirectory)) Then NewFolder strDirectory
    
    Sub NewFolder(path)
     If Not objFSO.FolderExists(path) Then
     ' Recurse upwards ...
     NewFolder objFSO.GetParentFolderName(path)
     ' ... until we have reached an existing folder.
     objFSO.CreateFolder(path)
     End If
    End Sub

     
    #2

      Online Bookmarks Sharing: Share/Bookmark

      Jump to:

      Current active users

      There are 0 members and 1 guests.

      Icon Legend and Permission

      • New Messages
      • No New Messages
      • Hot Topic w/ New Messages
      • Hot Topic w/o New Messages
      • Locked w/ New Messages
      • Locked w/o New Messages
      • Read Message
      • Post New Thread
      • Reply to message
      • Post New Poll
      • Submit Vote
      • Post reward post
      • Delete my own posts
      • Delete my own threads
      • Rate post

      2000-2012 ASPPlayground.NET Forum Version 3.9