PDA

View Full Version : Solved: Creating Folders or Directories



jigar1276
07-07-2008, 04:34 AM
Hi Experts,

I have "D:\Performance Indicator" folder. Under that i have folders for each year named 2007, 2008 etc. Under each year i have folders for each months named 01-2008, 02-2008 etc. Under each month i have folders for each date named 01, 02, 03 etc.

I need macro which should create folder for previous day of current date.

This may required to create folders for new months or years also if the previouse day of current date is falling in previouse month or year.

Please help.

Thanks.

Bob Phillips
07-07-2008, 04:46 AM
Const FOLDER_ROOT As String = "D:\Performance Indicator\"
Dim TestDate As Date
Dim FolderPath As String
Dim FolderName As String
Dim Folders As Variant

TestDate = Date - 1
FolderPath = FOLDER_ROOT & _
Year(TestDate) & Application.PathSeparator & _
Format(TestDate, "mm-yyyy") & Application.PathSeparator & _
Format(TestDate, "dd")
Folders = Split(FolderPath, Application.PathSeparator)
On Error Resume Next
FolderName = FOLDER_ROOT
For i = LBound(Folders) + 2 To UBound(Folders)

FolderName = FolderName & Folders(i) & Application.PathSeparator
MkDir FolderName
Next i
On Error GoTo 0

jigar1276
07-07-2008, 05:08 AM
Thanks very much Xld,

This macro is working fine and creating the folders same as my requiremetns.

Only one small change in folder path:
The folder "D:\Jigar\Performance Indicator\" is already exists and if i run the macro for first time its creating the folder "D:\Jigar\Performance Indicator\Performance Indicator\2008\07-2008\06" (Performance Indicator as sub folder inside the main folder Performance Indicator").

Thanks once again.

jigar1276
07-07-2008, 05:18 AM
Its done Xld,

I was trying to understand the logic.

I changed
"For i = LBound(Folders) + 2 To UBound(Folders) "
with
"For i = LBound(Folders) + 3 To UBound(Folders) "

Thanks for teaching me something which i dont know.

Bob Phillips
07-07-2008, 05:50 AM
Why did you change it? Your description only made the first 2 items absolute.

Bob Phillips
07-07-2008, 05:54 AM
Oops, just saw the previous post.

To be flexible, better to use



Const FOLDER_ROOT As String = "D:\Performance Indicator\"
Dim CountRoot As Long
Dim TestDate As Date
Dim FolderPath As String
Dim FolderName As String
Dim Folders As Variant
Dim i As Long

TestDate = Date - 1
CountRoot = Len(FOLDER_ROOT) - Len(Replace(FOLDER_ROOT, Application.PathSeparator, ""))
FolderPath = FOLDER_ROOT & _
Year(TestDate) & Application.PathSeparator & _
Format(TestDate, "mm-yyyy") & Application.PathSeparator & _
Format(TestDate, "dd")
Folders = Split(FolderPath, Application.PathSeparator)
On Error Resume Next
FolderName = FOLDER_ROOT
For i = LBound(Folders) + CountRoot To UBound(Folders)

FolderName = FolderName & Folders(i) & Application.PathSeparator
MkDir FolderName
Next i
On Error GoTo 0

jigar1276
07-08-2008, 03:09 AM
Thats great Xld.

This was one part of my project. once the folders are created i have macro to copy files into that and preparation of reports.

Thanks once again.