[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

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

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
stephenlecomptejr
Asked:
stephenlecomptejr
  • 3
  • 3
1 Solution
 
Russ SuterCommented:
What parameter are you passing to System.IO.Directory.CreateDirectory()?
0
 
stephenlecomptejrAuthor Commented:
"C:\WORK\changes\file\guarantee" as a string
0
 
Russ SuterCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
stephenlecomptejrAuthor Commented:
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
 
Russ SuterCommented:
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
 
stephenlecomptejrAuthor Commented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now