Solved

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

Posted on 2014-04-24
6
280 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
Comment Utility
What parameter are you passing to System.IO.Directory.CreateDirectory()?
0
 
LVL 1

Author Comment

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

Expert Comment

by:Russ Suter
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:stephenlecomptejr
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

744 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

10 Experts available now in Live!

Get 1:1 Help Now