473,405 Members | 2,187 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,405 developers and data experts.

Create a Folder using VBA

TheSmileyCoder
2,322 Expert Mod 2GB
I have often had the use to be able to create folders on a users harddrive or network drive to store files in. One of the issues however was if the parent folder did not allready exist the standard built in function MkDir would fail. So I made a custom function which would loop through, and create the directories as needed. It will return a True if the creation is succesfull. I am sure there is room for improvement, so if anyone has something to add, go ahead.

Example usage:
Expand|Select|Wrap|Line Numbers
  1. If MakeDir("C:\MyAppWorkfolder\2012\January") Then
  2.     'Proceed to export file
  3. End If
And here is code:
Expand|Select|Wrap|Line Numbers
  1. Public Function MakeDir(ByVal strPath As String) As Boolean
  2. '****************************************************************************************
  3. '* Function: MakeDir
  4. '*
  5. '* Author:   TheSmileyCoder
  6. '* Version:  1.0, Dated: 2012-03-01
  7. '* Input:    Full path to directory desired. For example: "C:\Program Files\MyTool\
  8. '*
  9. '* Output:   True/False indicating whether or not creation was succesfull.
  10. '****************************************************************************************
  11.  
  12. '*  Known issues
  13. '   * No error handling for cases such as network drives,
  14. '     with restricted permissions to create folders.
  15. '   * No input validation
  16.  
  17.  
  18. On Error GoTo err_Handler
  19.  
  20.    'Check if rightmost char is a \
  21.       If Right(strPath, 1) = "\" Then
  22.          'Strip it
  23.          strPath = Left(strPath, Len(strPath) - 1)
  24.       End If
  25.  
  26.    'Check if each individual directory exists, and if not, create it
  27.       Dim strSplitPath() As String
  28.       strSplitPath = Split(strPath, "\")
  29.       Dim intI As Integer
  30.       Dim strCombined As String
  31.  
  32.       'Loop through, creating each directory if needed
  33.          For intI = 0 To UBound(strSplitPath)
  34.             If intI <> 0 Then
  35.                strCombined = strCombined & "\"
  36.             End If
  37.             strCombined = strCombined & strSplitPath(intI)
  38.             If Dir(strCombined, vbDirectory) = "" Then
  39.                MkDir strCombined
  40.             End If
  41.          Next
  42.  
  43.    'Code ran to end without errors, so creation was succesfull
  44.       MakeDir = True
  45.       Exit Function
  46.  
  47. '**************************************
  48. '*      Error Handler
  49. '**************************************
  50. err_Handler:
  51.    MakeDir = False
  52.    MsgBox "Error " & Err.Number & " occured." & vbNewLine & Err.Description
  53.  
  54. End Function
Mar 2 '12 #1
0 10904

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Dave | last post by:
I have an application where I am providing the user the ability to select or create a folder on a domain, using SHBrowseForFolder. When the user selects/creates a folder on a remote computer, it...
2
by: DCount17 | last post by:
I'm new to .NET and C#. I need help in trying to do the following: I need to use a URI (or something else) that allows all users of the application to be able to access a directory and create a...
1
by: sajithm | last post by:
Hi all, I have a problem while trying to create folder and files in a remot storage server using asp.net. Actually I was trying with filesystem object and this says bad usernam and passeord,....
4
by: santel_helvis | last post by:
Hi there, I wanna open the folder using asp.net. I am using anchor link to do that. But in href part how shall I give the location. Anyone plz help me out. Thanks in advance
2
by: Joey | last post by:
Hey, I need to create a share on a remote folder using VB.NET, On the remote server, there is NO WMI, hence I need to find a way to do so, Without using WMI. Thanks ahead, --Joey
2
by: msr | last post by:
Dear all Can anyone help me I've written VB program to save Pid and date into ms-access database but i need to alter my program in a way when i enter pid and date then automatically folders...
1
by: vaikundkrishna | last post by:
how to create a folder, using c# codes
0
by: mrinalaryan | last post by:
create folder & upload file in the web server through web service-C# -------------------------------------------------------------------------------- im using web service to connect windows form...
1
by: meenu_susi | last post by:
hello could u please send me the code for creating a folder in ftp server using vb script I have tried with the following code <script language="vbscript"> Set objFSO =...
2
by: Noorain | last post by:
sir how to create folder to use php script? i know how to file upload in folder to use php. but i can't how to create folder. i type name & its create my folder. Example: Type: test. my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.