Mkdir function


G

Greg

Hi,

I'm trying to write a function that creates a directory & Subdirectories. I
can create a String say "C:/DirL1/DirL2/DirL3/" say but I dont know if DirL1
or DirL1/DirL2 exist. what I need is a recursive string function that will
test for the total directory String, if not test for the next level down and
so on until it gets to the root directory, and then create the directories
once true.

I suppose what I really need is a function that will take a directory string
and return the next level up

ie (stringfunc "C:/DirL1/DirL2/DirL3/") and return "C:/DirL1/DirL2/" and so on

Hope this makes sense.

Thanks for any help
 
Ad

Advertisements

S

Stuart McCall

Greg said:
Hi,

I'm trying to write a function that creates a directory & Subdirectories.
I
can create a String say "C:/DirL1/DirL2/DirL3/" say but I dont know if
DirL1
or DirL1/DirL2 exist. what I need is a recursive string function that will
test for the total directory String, if not test for the next level down
and
so on until it gets to the root directory, and then create the directories
once true.

I suppose what I really need is a function that will take a directory
string
and return the next level up

ie (stringfunc "C:/DirL1/DirL2/DirL3/") and return "C:/DirL1/DirL2/" and
so on

Hope this makes sense.

Thanks for any help
Take a look at this. It does exactly what you want AFAICT.

http://www.smccall.demon.co.uk/Strings.htm#CreatePath
 
Ad

Advertisements

Joined
Dec 17, 2007
Messages
57
Reaction score
0
Stuart McCall said:
"Greg" wrote in message
news:D16B1FA0-D88D-409E-87F5-10BCC34F338F@microsoft.com...
> Hi,
>
> I'm trying to write a function that creates a directory & Subdirectories.
> I
> can create a String say "C:/DirL1/DirL2/DirL3/" say but I dont know if
> DirL1
> or DirL1/DirL2 exist. what I need is a recursive string function that will
> test for the total directory String, if not test for the next level down
> and
> so on until it gets to the root directory, and then create the directories
> once true.
>
> I suppose what I really need is a function that will take a directory
> string
> and return the next level up
>
> ie (stringfunc "C:/DirL1/DirL2/DirL3/") and return "C:/DirL1/DirL2/" and
> so on
>
> Hope this makes sense.
>
> Thanks for any help
> --
> Greg McLandsborough


Take a look at this. It does exactly what you want AFAICT.

http://www.smccall.demon.co.uk/Strings.htm#CreatePath


I have used procs for MakeDir RemoveDir and TestDirRoutines
Code:
'---------------------------------------------------------------------------------------
 ' Procedure : MakeDir
 ' DateTime  : 2005-06-28 10:20
 ' Author	: drawbrij
 ' Purpose   : To make a Directory from within VBA. It checks if
 '			 the Directory to be created already exists, and gives
 '			 an Error message if so.
 '
 'Parameters:
 'sDrive  - the Drive on which the new directory is to be built
 'sDir	- the new directoryName
 '
 'Note:   - Only creates 1 level per call
 '		- the sDir must have leading \ for each level of directory
 ' eg  MakeDir "C" ,"\level1"		 <--call 1
 '	 MakeDir "C" ,"\level1\level2"  <--call 2
 '	 will create c:\level1\level2  <--2 Calls required
 '---------------------------------------------------------------------------------------
 '
 Sub MakeDir(sDrive As String, sDir As String)
 
 On Error GoTo ErrorFound
 VBA.FileSystem.MkDir sDrive & sDir
 ErrorFound:
 If Err.Number = 75 Then
  MsgBox "Err 75 - Directory (" & sDrive & sDir & ") already exists"
 ElseIf Err.Number = 0 Then
  Exit Sub
 Else
  MsgBox Err.Number & " other error " & Err.Description
 End If
 
 End Sub
 '---------------------------------------------------------------------------------------
 ' Procedure : RemoveDir
 ' DateTime  : 2005-06-28 10:29
 ' Author	: drawbrij
 ' Purpose   : to remove a directory from within VBA.It checks if
 '			 the Directory to be removed does not exist, and gives
 '			 an Error message if so.
 '
 'Parameters:
 'sDrive  - the Drive from which the directory is to be removed
 'sDir	- the  directoryName
 '
 'Note:   - the sDir must have leading \ for each level of directory
 ' eg  RemoveDir "C" ,"\level1\level2"
 '	 will remove \level2
 ' and will leave C:\level1
 '---------------------------------------------------------------------------------------
 '
 Sub RemoveDir(sDrive As String, sDir As String)
 
 On Error GoTo ErrorFound
 VBA.FileSystem.RmDir sDrive & sDir
 ErrorFound:
 If Err.Number = 76 Then
  MsgBox "Err 76 - Directory (" & sDrive & sDir & ")  doesn't exist"
 ElseIf Err.Number = 0 Then
   Exit Sub
 Else
  MsgBox Err.Number & " other error " & Err.Description
 End If
 
 End Sub
 '---------------------------------------------------------------------------------------
 ' Procedure : testDirRoutines
 ' DateTime  : 2005-08-04 15:28
 ' Author	: drawbrij
 ' Purpose   :to test the MakeDir and RemoveDir procedures.
 ' These were not used with OffLine Directory,
 ' but may have some usefulness
 '---------------------------------------------------------------------------------------
 '
 Sub testDirRoutines()
 RemoveDir "c", "\jack\newdir"
 MakeDir "c", "\jack\newdir\nextDir"
 MakeDir "c", "\jack\newdir\nextDir"
 RemoveDir "c", "\jack\newdir\nextDir"
 RemoveDir "c", "\jack\newdir"
 End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top