Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

FileSystemObject - Creating Folders

Posted on 2006-06-28
7
Medium Priority
?
390 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 86

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 86

Accepted Solution

by:
Mike Tomlinson earned 1500 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 5

Author Comment

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

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
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.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

688 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