Solved

FileSystemObject - Creating Folders

Posted on 2006-06-28
7
387 Views
Last Modified: 2010-04-07
Hi peoples,

Any chance of a sample code?

I have a string variable which contains either "C:\program\file.txt", "C:\program\subfolder\file.txt", "C:\program\subfolder\subfolder2\file.txt" and so on...

The folder C:\program\ already exists, but right now it is empty.

I need code that will look at the string variable, take of the "file.txt" from the end of the string, check to see if the folder structure exists, and if it doesnt, create it.

I also need the fully intact string at the end so i can use it in order to copy a file from a location to that one.

HELP!!!
0
Comment
Question by:NAORC
  • 4
  • 3
7 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17001226
Are you working with VBScript (.vbs)?

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim fullPath
fullPath = "C:\program\subfolder\subfolder2\file.txt"
createPath (Left(fullPath, InStrRev(fullPath, "\")))

function createPath(path)
    folders = Split (path, "\")
    for i = lbound(folders) to ubound(folders)
        if folders(i) <> "" then
            p = p & folders(i) & "\"
            if (not fso.FolderExists (p)) then
                fso.CreateFolder (p)
            end if
        end if
    next
end function
0
 
LVL 5

Author Comment

by:NAORC
ID: 17001243
VB6.

does this affect the code?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 17001264
Not really...just change the syntax a little...

Option Explicit

Private fso As Object

Private Sub Form_Load()
    Set fso = CreateObject("Scripting.FileSystemObject")
End Sub

Private Sub Command1_Click()
    Dim fullPath As String
    fullPath = "C:\program\subfolder\subfolder2\file.txt"
    createPath Left(fullPath, InStrRev(fullPath, "\"))
End Sub

Private Function createPath(ByVal path As String)
    Dim i As Integer
    Dim folders As Variant
    Dim p As String
    folders = Split(path, "\")
    For i = LBound(folders) To UBound(folders)
        If folders(i) <> "" Then
            p = p & folders(i) & "\"
            If (Not fso.FolderExists(p)) Then
                fso.CreateFolder (p)
            End If
        End If
    Next
End Function
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 5

Author Comment

by:NAORC
ID: 17001470
what needs to be done to modify the code to work for me?
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17001710
The above is tested, working VB6 code....

Try it out.  =)
0
 
LVL 5

Author Comment

by:NAORC
ID: 17002068
Have done... it is very reliant on accurate data being fed into it, but i managed to tweak it into working :)
thanks
0
 
LVL 5

Author Comment

by:NAORC
ID: 17030763
Just for reference of others, this code is an optimised and much more efficient version of the code above :)


Private Function createPath(ByVal path As String)

Dim j&, currentParentFolder$

    j& = 0
    Do
        'Find the next slash, after the last one found.
        j& = InStr(j& + 1, path, "\")
        If j& > 0 Then 'found another slash...
            currentParentFolder$ = Left$(path, j&)
            If (m_fso.FolderExists(currentParentFolder$) = False) Then
                m_fso.CreateFolder currentParentFolder$
            End If
        End If
    Loop Until j& = 0
   
End Function
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question