Solved

FileSystemObject - Creating Folders

Posted on 2006-06-28
7
388 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 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

751 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