Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 119
Default Making Directory if None Exists

I need help making a directory if there isn't one present. I know you have to
use the mkdir command, but I don't know how to use it.

Here's an example:

I run a file, clear its contents, and save it. If I am clearing a December
file, I want a January file in a folder named 2010 in the current directory
and go up one folder (above the 2009 folder).

Basically, I need code to check whether a 2010 folder exists in a directory
path, and, if not, create it and save the file there.

TIA.
--
I am running on Office 2003, unless otherwise stated.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Making Directory if None Exists

You can start with this one

Change this line
FolderPath = "C:\Users\Ron\test3"

Sub Test_Folder_Exist_FSO_Early_binding()
'If you want to use the the Intellisense help showing you the properties
'and methods of the objects as you type you can use Early binding.
'Add a reference to "Microsoft Scripting Runtime" in the VBA editor
'(ToolsReferences) if you want that.

Dim FSO As Scripting.FileSystemObject
Dim FolderPath As String

Set FSO = New Scripting.FileSystemObject

FolderPath = "C:\Users\Ron\test3"

If Right(FolderPath, 1) < "\" Then
FolderPath = FolderPath & "\"
End If

If FSO.FolderExists(FolderPath) = False Then
FSO.CreateFolder FolderPath
Else
MsgBox "Folder exist"
End If

End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm




"Orion Cochrane" wrote in message
...
I need help making a directory if there isn't one present. I know you have to
use the mkdir command, but I don't know how to use it.

Here's an example:

I run a file, clear its contents, and save it. If I am clearing a December
file, I want a January file in a folder named 2010 in the current directory
and go up one folder (above the 2009 folder).

Basically, I need code to check whether a 2010 folder exists in a directory
path, and, if not, create it and save the file there.

TIA.
--
I am running on Office 2003, unless otherwise stated.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 119
Default Making Directory if None Exists

Thanks. I'll try it tonight. I added the Microsoft Scripting Runtime to my
PERSONAL.xls file, which is where I am going to run my macro from.
--
I am running on Office 2003, unless otherwise stated.


"Ron de Bruin" wrote:

You can start with this one

Change this line
FolderPath = "C:\Users\Ron\test3"

Sub Test_Folder_Exist_FSO_Early_binding()
'If you want to use the the Intellisense help showing you the properties
'and methods of the objects as you type you can use Early binding.
'Add a reference to "Microsoft Scripting Runtime" in the VBA editor
'(ToolsReferences) if you want that.

Dim FSO As Scripting.FileSystemObject
Dim FolderPath As String

Set FSO = New Scripting.FileSystemObject

FolderPath = "C:\Users\Ron\test3"

If Right(FolderPath, 1) < "\" Then
FolderPath = FolderPath & "\"
End If

If FSO.FolderExists(FolderPath) = False Then
FSO.CreateFolder FolderPath
Else
MsgBox "Folder exist"
End If

End Sub


--

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm




"Orion Cochrane" wrote in message
...
I need help making a directory if there isn't one present. I know you have to
use the mkdir command, but I don't know how to use it.

Here's an example:

I run a file, clear its contents, and save it. If I am clearing a December
file, I want a January file in a folder named 2010 in the current directory
and go up one folder (above the 2009 folder).

Basically, I need code to check whether a 2010 folder exists in a directory
path, and, if not, create it and save the file there.

TIA.
--
I am running on Office 2003, unless otherwise stated.



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Making Directory if None Exists

I just ignore any error that may happen if the folder is already the

Assuming that C:\my documents\excel exists:

on error resume next
mkdir "C:\my documents\excel\2010"
mkdir "C:\my documents\excel\2010\January"
on error goto 0

======
Another option:

Here's something that Jim Rech Posted:

Option Explicit
Declare Function MakePath Lib "imagehlp.dll" Alias _
"MakeSureDirectoryPathExists" (ByVal lpPath As String) As Long

Sub Test()
MakeDir "c:\aaa\bbb"
End Sub

Sub MakeDir(DirPath As String)
If Right(DirPath, 1) < "\" Then DirPath = DirPath & "\"
MakePath DirPath
End Sub



Orion Cochrane wrote:

I need help making a directory if there isn't one present. I know you have to
use the mkdir command, but I don't know how to use it.

Here's an example:

I run a file, clear its contents, and save it. If I am clearing a December
file, I want a January file in a folder named 2010 in the current directory
and go up one folder (above the 2009 folder).

Basically, I need code to check whether a 2010 folder exists in a directory
path, and, if not, create it and save the file there.

TIA.
--
I am running on Office 2003, unless otherwise stated.


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Making Directory if None Exists

Try something like

Sub MakeMultiDir(FullPath As String)
Dim V As Variant
Dim N As Long
Dim S As String
V = Split(FullPath, "\")

For N = LBound(V) To UBound(V)
S = S & V(N)
If Dir(S, vbDirectory) = vbNullString Then
MkDir S
End If
S = S & "\"
Next N
End Sub

You can then call this with code like the following:

Sub AAA()
Dim S As String
S = "C:\Test2\TestSub1\TestSubSub1"
MakeMultiDir S
End Sub

MakeMultiDir will create any sub directories necessary in order to
make the full path specified by the FullPath parameter.

Cordially,
Chip Pearson
Microsoft Most Valuable Professional
Excel Product Group, 1998 - 2009
Pearson Software Consulting, LLC
www.cpearson.com
(email on web site)




On Thu, 3 Sep 2009 12:53:01 -0700, Orion Cochrane
wrote:

I need help making a directory if there isn't one present. I know you have to
use the mkdir command, but I don't know how to use it.

Here's an example:

I run a file, clear its contents, and save it. If I am clearing a December
file, I want a January file in a folder named 2010 in the current directory
and go up one folder (above the 2009 folder).

Basically, I need code to check whether a 2010 folder exists in a directory
path, and, if not, create it and save the file there.

TIA.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 119
Default Making Directory if None Exists

That works great! For me, just 2 lines of code is better. Just the Error
handler and the MkDir line.

Thank you all for helping me immensely.
--
I am running on Office 2003, unless otherwise stated.


"Dave Peterson" wrote:

I just ignore any error that may happen if the folder is already the

Assuming that C:\my documents\excel exists:

on error resume next
mkdir "C:\my documents\excel\2010"
mkdir "C:\my documents\excel\2010\January"
on error goto 0

======
Another option:

Here's something that Jim Rech Posted:

Option Explicit
Declare Function MakePath Lib "imagehlp.dll" Alias _
"MakeSureDirectoryPathExists" (ByVal lpPath As String) As Long

Sub Test()
MakeDir "c:\aaa\bbb"
End Sub

Sub MakeDir(DirPath As String)
If Right(DirPath, 1) < "\" Then DirPath = DirPath & "\"
MakePath DirPath
End Sub



Orion Cochrane wrote:

I need help making a directory if there isn't one present. I know you have to
use the mkdir command, but I don't know how to use it.

Here's an example:

I run a file, clear its contents, and save it. If I am clearing a December
file, I want a January file in a folder named 2010 in the current directory
and go up one folder (above the 2009 folder).

Basically, I need code to check whether a 2010 folder exists in a directory
path, and, if not, create it and save the file there.

TIA.
--
I am running on Office 2003, unless otherwise stated.


--

Dave Peterson

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
If A Directory Exists Jim Thomlinson[_5_] Excel Programming 0 February 4th 06 09:00 PM
File exists under directory PeterW[_10_] Excel Programming 2 December 16th 05 01:14 AM
How to check if the directory exists? ira Excel Programming 2 January 19th 04 01:22 PM
Verify a directory exists MacroMan[_4_] Excel Programming 3 August 8th 03 04:38 PM


All times are GMT +1. The time now is 01:16 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"