Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-04-24
6
Medium Priority
?
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
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.

 
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 2000 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

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.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

609 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