Solved

FileSystemObject - Creating Folders

Posted on 2006-06-28
7
385 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now