PC Review


Reply
Thread Tools Rate Thread

Code to create folder and subfolders

 
 
JP
Guest
Posts: n/a
 
      16th Oct 2007
Hi all,

Does anyone have code that can create parent folders automatically (if
they don't exist)?

For example I use MkDir to create folders. Currently I am doing it
this way:

MkDir FolderA
MkDir FolderA\FolderB
MkDir FolderA\FolderB\FolderC

to create FolderC

Is there a way to just write in the lowest subfolder (FolderA\FolderB
\FolderC) and Excel/VBA would create the parent folders on its own, if
they don't exist?

Thx!

--JP

 
Reply With Quote
 
 
 
 
Michel Pierron
Guest
Posts: n/a
 
      16th Oct 2007
Hi JP,
You can try :

Private Declare Function MakeSureDirectoryPathExists _
Lib "imagehlp.dll" (ByVal lpPath As String) As Boolean

Sub CreateDirectoryStructure()
On Error GoTo 1
MakeSureDirectoryPathExists "C:\FolderA\FolderB\FolderC\FolderD\"
Exit Sub
1: MsgBox "Error " & Err.Number & vbLf & Err.Description, 64
End Sub

Regards,
MP

"JP" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...
> Hi all,
>
> Does anyone have code that can create parent folders automatically (if
> they don't exist)?
>
> For example I use MkDir to create folders. Currently I am doing it
> this way:
>
> MkDir FolderA
> MkDir FolderA\FolderB
> MkDir FolderA\FolderB\FolderC
>
> to create FolderC
>
> Is there a way to just write in the lowest subfolder (FolderA\FolderB
> \FolderC) and Excel/VBA would create the parent folders on its own, if
> they don't exist?
>
> Thx!
>
> --JP
>



 
Reply With Quote
 
Crowbar via OfficeKB.com
Guest
Posts: n/a
 
      16th Oct 2007
This will create a directory tree if it doenst exsist.

CheckDir is the important bit for you as this is the directory name you will
be looking for

I have set up an example. This should check if "C:\MatchBox\64\" exsists. If
it doenst you will be prompted and then it wil create it

Function CreateDir()

Dim Fso
Dim Answer
Dim File
Dim w As Long
Dim TargetDir As Boolean
Dim CheckDir As String
TargetDir = False

CheckDir = "C:\MatchBox\64\"
File = CheckDir
Set Fso = CreateObject("Scripting.FileSystemObject")
'Checks if the whole save directory exists, if it doesn't it finds out
what is missing

If Not Fso.folderexists(File) Then

For w = 1 To Len(CheckDir)

If Mid(CheckDir, w, 1) = "\" Then
File = Mid(CheckDir, 1, w)
Set Fso = CreateObject("Scripting.FileSystemObject")

If Not Fso.folderexists(File) Then

If TargetDir = False Then

Answer = MsgBox("The save dir '" & CheckDir & "' does not
exsit" & Chr(10) & Chr(10) & "Would you like to create it?", vbInformation +
vbYesNo, "Save Directory Error")

If Answer = vbYes Then
TargetDir = True
On Error GoTo errorhandler
MkDir File
Else
Exit Function
End If

ElseIf TargetDir = True Then
MkDir File
End If

End If

End If

Next w

End If

TargetDir = False

errorhandler:
If Err.Number > 10 Then
MsgBox "Cannot Create Dir!!!"
End If

End Function

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200710/1

 
Reply With Quote
 
JP
Guest
Posts: n/a
 
      16th Oct 2007
Thank you, I modified your code as follows. It's a bit awkward with
all of the IF statements, but it gets the job done. However, at my
office we use UNC paths which means the first pass causes the macro to
see every CheckDir variable as non-existent. Is there a way to account
for the initial double-slash?


Sub CreateDir()

Dim Fso As Scripting.FileSystemObject
Dim sFolder As String
Dim w As Long
Dim TargetDir As Boolean
Dim CheckDir As String

TargetDir = False

CheckDir = "\\p111filclu01\Drug\RANDOMS\NewClient\2007\DOT\"
sFolder = CheckDir
Set Fso = CreateObject("Scripting.FileSystemObject")

'Checks if the whole save directory exists, if it doesn't it finds out
what is missing

If Not Fso.folderexists(sFolder) Then
For w = 1 To Len(CheckDir)
If Mid(CheckDir, w, 1) = "\" Then
If w > 1 Then
If Mid(CheckDir, w - 1, 2) <> "\\" Then
sFolder = Mid(CheckDir, 1, w)
If Not Mid(sFolder, w, 2) = "\\" Then
If Not Fso.folderexists(sFolder) Then
If TargetDir = False Then
Select Case MsgBox("The directory '" &
CheckDir & "' does not exist" & vbCrLf & vbCrLf & "Would you like to
create it, along with with its parent folders?", vbYesNo)
Case vbYes
TargetDir = True
On Error GoTo errorhandler
MkDir sFolder
Case Else
Exit Sub
End Select
ElseIf TargetDir = True Then
MkDir sFolder
End If
End If
End If
End If
End If
End If
Next w
End If

TargetDir = False

Exit Sub

errorhandler:
If Err.Number > 10 Then
MsgBox "Cannot Create Dir!!!"
End If
End Sub


On Oct 16, 1:47 pm, "Crowbar via OfficeKB.com" <u15117@uwe> wrote:
> This will create a directory tree if it doenst exsist.
>
> CheckDir is the important bit for you as this is the directory name you will
> be looking for
>
> I have set up an example. This should check if "C:\MatchBox\64\" exsists. If
> it doenst you will be prompted and then it wil create it



 
Reply With Quote
 
JP
Guest
Posts: n/a
 
      2nd Nov 2007
Hey I thought you might like to know I found this code on Chip
Pearson's website, should do what I am looking for so I'm posting the
link.

http://www.cpearson.com/excel/MakeDirMulti.htm

Thx,
JP


On Oct 16, 1:47 pm, "Crowbar via OfficeKB.com" <u15117@uwe> wrote:
> This will create a directory tree if it doenst exsist.
>
> CheckDir is the important bit for you as this is the directory name you will
> be looking for
>
> I have set up an example. This should check if "C:\MatchBox\64\" exsists. If
> it doenst you will be prompted and then it wil create it
>
> Function CreateDir()
>
> Dim Fso
> Dim Answer
> Dim File
> Dim w As Long
> Dim TargetDir As Boolean
> Dim CheckDir As String
> TargetDir = False
>
> CheckDir = "C:\MatchBox\64\"
> File = CheckDir
> Set Fso = CreateObject("Scripting.FileSystemObject")
> 'Checks if the whole save directory exists, if it doesn't it finds out
> what is missing
>
> If Not Fso.folderexists(File) Then
>
> For w = 1 To Len(CheckDir)
>
> If Mid(CheckDir, w, 1) = "\" Then
> File = Mid(CheckDir, 1, w)
> Set Fso = CreateObject("Scripting.FileSystemObject")
>
> If Not Fso.folderexists(File) Then
>
> If TargetDir = False Then
>
> Answer = MsgBox("The save dir '" & CheckDir & "' does not
> exsit" & Chr(10) & Chr(10) & "Would you like to create it?", vbInformation +
> vbYesNo, "Save Directory Error")
>
> If Answer = vbYes Then
> TargetDir = True
> On Error GoTo errorhandler
> MkDir File
> Else
> Exit Function
> End If
>
> ElseIf TargetDir = True Then
> MkDir File
> End If
>
> End If
>
> End If
>
> Next w
>
> End If
>
> TargetDir = False
>
> errorhandler:
> If Err.Number > 10 Then
> MsgBox "Cannot Create Dir!!!"
> End If
>
> End Function
>
> --
> Message posted via OfficeKB.comhttp://www.officekb.com/Uwe/Forums.aspx/excel-programming/200710/1



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ron de Bruin's code "Copy a range from all files in a folder and subfolders (optional)" ??? Mark Ivey Microsoft Excel Programming 10 23rd Dec 2007 03:54 PM
Create New Folder Using Code =?Utf-8?B?TmlnZWw=?= Microsoft Excel Programming 2 22nd May 2007 06:29 PM
SyncToy won't exclude subfolders or "create folder" operations George Windows XP Photos 1 4th Jun 2006 03:16 AM
How to create new folder by code =?Utf-8?B?Uml0YUs=?= Microsoft VB .NET 3 2nd Mar 2005 01:03 AM
Create subfolders in a folder Myrna Rodriguez Microsoft Excel Programming 2 15th Jul 2004 04:10 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:55 PM.