Link to home
Start Free TrialLog in
Avatar of NAORC
NAORC

asked on

FileSystemObject - Creating Folders

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!!!
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

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
Avatar of NAORC
NAORC

ASKER

VB6.

does this affect the code?
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NAORC

ASKER

what needs to be done to modify the code to work for me?
The above is tested, working VB6 code....

Try it out.  =)
Avatar of NAORC

ASKER

Have done... it is very reliant on accurate data being fed into it, but i managed to tweak it into working :)
thanks
Avatar of NAORC

ASKER

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