Solved

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

Posted on 2014-04-24
6
284 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
A short film showing how OnPage and Connectwise integration works.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

929 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

16 Experts available now in Live!

Get 1:1 Help Now