Solved

FileSystemObject - Creating Folders

Posted on 2006-06-28
7
384 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

760 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

18 Experts available now in Live!

Get 1:1 Help Now