Solved

vba converter to C# - creating subdirectories within a directory.

Posted on 2014-04-24
6
285 Views
Last Modified: 2014-04-24
I've got the following VBA code that makes folders and any subdirectories thrown in.

I was trying to convert it to C# but all the online vb.net to C# keep crashing on the first line item.

FYI - System.IO.Directory.CreateDirectory()  does not work me and thus I'm desperate to get this code working.  Obviously does not work if the path is something like:  "C:\WORK\changes\file\guarantee" as a string will not work with System.IO.Directory.CreateDirectory() cause of the multiple subfolders.

One of the other reasons I like the code below is that it checks to be sure there is not a file extension in the path and grabs only the directory part to ensure that it exists.  It also checks if everything exists then just cancel out.   I know it could be rearranged better but it works and has for a long time.


Public Sub MakeFolders(sFileAdj As String)

  Dim i As Long
  Dim sFileAdjusted As String
  sFileAdjusted = sFileAdj
  
  Dim pFS1 As Object
  Dim sRight As String
  Set pFS1 = CreateObject("Scripting.FileSystemObject")
  If Right(sFileAdjusted, 3) = ".mdb" Or Right(sFileAdjusted, 6) = ".accdb" Or Right(sFileAdjusted, 9) = "_be.accdb" Or Right(sFileAdjusted, 4) = ".vbs" Or Right(sFileAdjusted, 4) = ".txt" Or Right(sFileAdjusted, 4) = ".pdf" Or Right(sFileAdjusted, 4) = ".mdb" Or Right(sFileAdjusted, 4) = ".ppt" Or Right(sFileAdjusted, 4) = ".doc" Or Right(sFileAdjusted, 5) = ".docx" Or Right(sFileAdjusted, 4) = ".xls" Or Right(sFileAdjusted, 5) = ".xlsx" Then
    sFileAdjusted = Left(sFileAdjusted, InStrRev(sFileAdjusted, "\"))
  End If
  If pFS1.FolderExists(sFileAdjusted) = False Then
  i = 0
    Do Until i = Len(sFileAdjusted)
      i = i + 1
      If Mid(sFileAdjusted, i, 1) = "\" Then
        If Not pFS1.FolderExists(Mid$(sFileAdjusted, 1, i - 1)) Then
          pFS1.CreateFolder (Mid$(sFileAdjusted, 1, i - 1))
        End If
      End If
    Loop
  If Not pFS1.FolderExists(sFileAdjusted) Then
    sRight = Right(sFileAdjusted, 4)
    If Left(sRight, 1) <> "." Then
      pFS1.CreateFolder (sFileAdjusted)
    End If
  End If
  End If
  Set pFS1 = Nothing

  
End Sub

Open in new window

0
Comment
Question by:stephenlecomptejr
  • 3
  • 3
6 Comments
 
LVL 20

Expert Comment

by:Russ Suter
ID: 40020915
What parameter are you passing to System.IO.Directory.CreateDirectory()?
0
 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40020930
"C:\WORK\changes\file\guarantee" as a string
0
 
LVL 20

Expert Comment

by:Russ Suter
ID: 40020934
Does each of those directories exist?

For example, if "C:\WORK\changes\file" already exists then it should have no problem creating the "guarantee" directory as long as permissions are properly set but if only "C:\WORK\changes" exists then the parameter you sent wont automatically create the "file" directory first. You'll have to do that yourself.

Is this the issue? If so I can help you with the proper code.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40020942
No in each case sometimes they may exist - sometimes not.

Sometimes C:\work may exist but C:\work\changes\files may not.
Sometimes C:\work\changes will exist but not c:\work\changes\files will not.
Sometimes C:\work won't exist at all but the entire directory should be created.

I just wish they would have made System.IO.Directory.CreateDirectory() to account for such scenarios.
0
 
LVL 20

Accepted Solution

by:
Russ Suter earned 500 total points
ID: 40021064
Actually I misspoke. After looking at the MSDN docs for this method it turns out it will create ALL of the directories in the path if needed which means the problem is most likely one of permissions.

If it cannot create the directory it will throw an exception. What is the exception?
0
 
LVL 1

Author Comment

by:stephenlecomptejr
ID: 40021225
I think when a user is not admin -that's when it does not work... it doesn't throw an error at all....it just goes to the next line.

I think you are right - I tested and the subdirectories did create.  Thank you for your help.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 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