-
May 20th, 2024, 05:06 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] how to create a monthly folder automatically
I have a monthly function here.
What would it be like to create a folder for each month?
I mean end of the month create a new folder
day 31-01-2024 create a new folder that says 01-02-2024
Code:
Private Function SetDates (pDate As Date) As String
Select Case Month (pDate)
Case 1, 3, 5, 7, 8, 10, 12
SetDates = “31”
Case 4, 6, 9, 11
SetDates = “30”
Case 2
If (Year(pDate) Mod 4) = 0 Then
SetDates = “29”
Else
SetDates = “28”
End If
End Select
End Function
thank you
-
May 20th, 2024, 05:19 PM
#2
Re: how to create a monthly folder automatically
Just an idea:
Code:
' Return a foldername DD-MM-YYYY if the given date is the last day of the month
' otherwise return ""
Private Function pFolderName(ByVal lDate As Long) As String
If pIsLastDayOfMonth(lDate) Then pFolderName = Format(lDate + 1, "DD\-MM\-YYYY")
End Function
' Check if the given date is the last day of the month
Private Function pIsLastDayOfMonth(ByVal lDate As Long) As Boolean
pIsLastDayOfMonth = (Day(lDate + 1) = 1)
End Function
-
May 20th, 2024, 05:27 PM
#3
Thread Starter
Hyperactive Member
Re: how to create a monthly folder automatically
thank you
to activate the functions as would be
thank you
-
May 21st, 2024, 08:58 AM
#4
Thread Starter
Hyperactive Member
Re: how to create a monthly folder automatically
Code:
' Return a foldername DD-MM-YYYY if the given date is the last day of the month
' otherwise return ""
Private Function pFolderName(ByVal lDate As Long) As String
If pIsLastDayOfMonth(lDate) Then pFolderName = Format(lDate + 1, "DD\-MM\-YYYY")
End Function
' Check if the given date is the last day of the month
Private Function pIsLastDayOfMonth(ByVal lDate As Long) As Boolean
pIsLastDayOfMonth = (Day(lDate + 1) = 1)
End Function
to create a folder
Private Sub Command1_Click()
Dim createfolder As Object
Dim name As String
Dim route As String
route = App.Path & "\"
name = InputBox("enter the name of a folder", "folder name")
Set createfolder = CreateObject("scripting.filesystemobject")
createfolder.createfolder route & name
MsgBox "Your folder has been created successfully", vbInformation, "created folder"
End Sub
How to make the two functions work to make a folder
day 31-01-2024 create a new folder that says 01-02-2024
thank you
-
May 21st, 2024, 09:04 AM
#5
Re: how to create a monthly folder automatically
This code adds one day to the current date:
Code:
DateAdd("d", 1, Date)
Then you can make a folder with "MkDir". Can you put them together?
-
May 21st, 2024, 10:10 AM
#6
Re: how to create a monthly folder automatically
Just as a tip, the easiest way to get the "last day of the month" is to create a date that's the first day of the next month, and then subtract one (day). That way, you let Windows (or VB6) deal with all the leap year stuff and how many days in each month.
And just in case you didn't get MSDN installed. Here are most of the relevant VB6 functions you'll need:
- DateValue
- DateSerial
- DateDiff
- DateAdd
- Year
- Month
- Day
Last edited by Elroy; May 21st, 2024 at 10:14 AM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
May 21st, 2024, 05:36 PM
#7
Thread Starter
Hyperactive Member
Re: how to create a monthly folder automatically
hello
I need the first day of each month and year
I mean
01-01-2024 create folder
01-02-2024 create folder
01-03-2024 create folder
01.04-2024 create folder
etc,etc
thank you
-
May 21st, 2024, 06:05 PM
#8
Re: how to create a monthly folder automatically
 Originally Posted by ontro
hello
I need the first day of each month and year
I mean
01-01-2024 create folder
01-02-2024 create folder
01-03-2024 create folder
01.04-2024 create folder
etc,etc
thank you
So, what problem are you having doing that?
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
May 22nd, 2024, 02:08 AM
#9
Re: how to create a monthly folder automatically
 Originally Posted by Elroy
Just as a tip, the easiest way to get the "last day of the month" is to create a date that's the first day of the next month, and then subtract one (day). That way, you let Windows (or VB6) deal with all the leap year stuff and how many days in each month.
Or look in Codebank, since we had a "new" LastDayOfMonth just a few weeks ago.
Or you just use DateSerial on the "zero"-th Day of the following month....
Code:
Debug.Print DateSerial(2024, 6, 0)
Returns: 2024-05-31
And nevermind OP should use ISO-Format for the Foldernames.... "YYYY-MM-DD Blablab"
Otherwise he'll get a surprise when sorting the Foldernames...
btw: Why not just create all Folders for the Year in one go, and be done with it? No need for checking if it's the last Day of the Month and whatnot
And instead of MkDir and/or using FSO, i'd rather go for SHCreateFolderExW-API, since that one uses absolute paths and creates any missing intermediate Folders, too
If the Folder already exists, it's a No-Op
Last edited by Zvoni; May 22nd, 2024 at 02:16 AM.
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
May 22nd, 2024, 04:14 AM
#10
Re: how to create a monthly folder automatically
 Originally Posted by Zvoni
btw: Why not just create all Folders for the Year in one go, and be done with it? No need for checking if it's the last Day of the Month and whatnot
thats what I would also do
@ontro
here a sample you can adjust to your needs
Code:
Option Explicit
Private Sub Command1_Click()
Dim nYear As Long
Dim nMonth As Long
Dim nDays As Long
Dim LastDayOfMonth As Date
Dim FirstDayOfMonth As Date
Dim d As Date
For nYear = 2024 To 2026 'set Year or Years to create Folders
For nMonth = 1 To 12
LastDayOfMonth = DateSerial(nYear, nMonth + 1, 0)
FirstDayOfMonth = DateSerial(nYear, nMonth, 1)
'get a Day count in that month
d = DateSerial(nYear, nMonth, 1)
nDays = Day(DateAdd("m", 1, d) - 1)
List1.AddItem ("First Day Of Month : " & Format(FirstDayOfMonth, "dddd dd.MM.yyyy"))
List1.AddItem ("Days in Month : " & nDays)
List1.AddItem ("Last Day of Month : " & Format(LastDayOfMonth, "dddd dd.MM.yyyy"))
List1.AddItem ("---------------------------------------------------------------------")
FolderCreateNew "E:\TestFolder\Reports\" & nYear & "\" & Format(FirstDayOfMonth, "dd-MM-yyyy") & " " & Format(LastDayOfMonth, "dd-MM-yyyy"), True
'create folders like:
'E:\TestFolder\Reports\2024\01-01-2024 31-01-2024
'E:\TestFolder\Reports\2024\01-02-2024 29-02-2024
'etc...
Next nMonth
Next nYear
End Sub
Public Function FolderCreateNew(Path As String, _
Optional ShowError As Boolean = True) As Boolean
Dim s As String
Dim s1() As String
Dim s2 As String
Dim Titel As String
Dim i As Long
Titel = "FolderCreate"
If Len(Trim(Path)) = 0 Then
If ShowError Then
yourError 1, "no path text", Titel
End If
Exit Function
End If
s = Replace(Trim(Path), "\\", "\")
'remove last BackSlash
If Right(s, 1) = "\" Then
s = Left$(s, Len(s) - 1)
End If
'split into Folders
s1() = Split(s, "\")
'does the Path exist
s2 = Join(s1(), "\")
If Len(Dir(s2, vbDirectory)) > 0 Then
If ShowError Then
yourError 2, "Path " & Path & vbCrLf & _
"exist allready", Titel
End If
Exit Function
End If
s2 = s1(LBound(s1))
'check for Drive
If Len(Dir(s2)) = 0 Then
If ShowError Then
yourError 3, "Drive " & s2 & " not found !", Titel
End If
Exit Function
End If
'check the Path(s)
On Error GoTo Fehler
For i = LBound(s1) + 1 To UBound(s1)
s2 = s2 & "\" & s1(i)
If Len(Dir(s2, vbDirectory)) = 0 Then
'Folder doesn't exsit, so create it
MkDir s2
End If
Next
FolderCreateNew = True
Exit Function
Fehler:
If ShowError Then
yourError err.Number, err.Description, Titel
End If
End Function
Public Sub yourError(ErrNumber As Long, ErrDescription As String, _
Optional Titel As String = "")
Dim Msg As String
Msg = "Error " & ErrNumber & vbCrLf & vbCrLf & _
ErrDescription
MsgBox Msg, vbCritical, Titel
End Sub
Last edited by ChrisE; May 22nd, 2024 at 04:17 AM.
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
May 22nd, 2024, 04:27 AM
#11
Re: how to create a monthly folder automatically
 Originally Posted by ChrisE
thats what I would also do
@ontro
here a sample you can adjust to your needs
Code:
Option Explicit
Private Sub Command1_Click()
Dim nYear As Long
Dim nMonth As Long
Dim nDays As Long
Dim LastDayOfMonth As Date
Dim FirstDayOfMonth As Date
Dim d As Date
For nYear = 2024 To 2026 'set Year or Years to create Folders
For nMonth = 1 To 12
LastDayOfMonth = DateSerial(nYear, nMonth + 1, 0)
FirstDayOfMonth = DateSerial(nYear, nMonth, 1)
'get a Day count in that month
d = DateSerial(nYear, nMonth, 1)
nDays = Day(DateAdd("m", 1, d) - 1)
List1.AddItem ("First Day Of Month : " & Format(FirstDayOfMonth, "dddd dd.MM.yyyy"))
List1.AddItem ("Days in Month : " & nDays)
List1.AddItem ("Last Day of Month : " & Format(LastDayOfMonth, "dddd dd.MM.yyyy"))
List1.AddItem ("---------------------------------------------------------------------")
FolderCreateNew "E:\TestFolder\Reports\" & nYear & "\" & Format(FirstDayOfMonth, "dd-MM-yyyy") & " " & Format(LastDayOfMonth, "dd-MM-yyyy"), True
'create folders like:
'E:\TestFolder\Reports\2024\01-01-2024 31-01-2024
'E:\TestFolder\Reports\2024\01-02-2024 29-02-2024
'etc...
Next nMonth
Next nYear
End Sub
Public Function FolderCreateNew(Path As String, _
Optional ShowError As Boolean = True) As Boolean
Dim s As String
Dim s1() As String
Dim s2 As String
Dim Titel As String
Dim i As Long
Titel = "FolderCreate"
If Len(Trim(Path)) = 0 Then
If ShowError Then
yourError 1, "no path text", Titel
End If
Exit Function
End If
s = Replace(Trim(Path), "\\", "\")
'remove last BackSlash
If Right(s, 1) = "\" Then
s = Left$(s, Len(s) - 1)
End If
'split into Folders
s1() = Split(s, "\")
'does the Path exist
s2 = Join(s1(), "\")
If Len(Dir(s2, vbDirectory)) > 0 Then
If ShowError Then
yourError 2, "Path " & Path & vbCrLf & _
"exist allready", Titel
End If
Exit Function
End If
s2 = s1(LBound(s1))
'check for Drive
If Len(Dir(s2)) = 0 Then
If ShowError Then
yourError 3, "Drive " & s2 & " not found !", Titel
End If
Exit Function
End If
'check the Path(s)
On Error GoTo Fehler
For i = LBound(s1) + 1 To UBound(s1)
s2 = s2 & "\" & s1(i)
If Len(Dir(s2, vbDirectory)) = 0 Then
'Folder doesn't exsit, so create it
MkDir s2
End If
Next
FolderCreateNew = True
Exit Function
Fehler:
If ShowError Then
yourError err.Number, err.Description, Titel
End If
End Function
Public Sub yourError(ErrNumber As Long, ErrDescription As String, _
Optional Titel As String = "")
Dim Msg As String
Msg = "Error " & ErrNumber & vbCrLf & vbCrLf & _
ErrDescription
MsgBox Msg, vbCritical, Titel
End Sub
No need for this convoluted CreateFolder-Function:
https://learn.microsoft.com/en-us/wi...tedirectoryexw
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
May 22nd, 2024, 04:36 AM
#12
Re: how to create a monthly folder automatically
@Zvoni
just show your example, Ontro would have two samples to choose from
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
May 22nd, 2024, 05:07 AM
#13
Thread Starter
Hyperactive Member
Re: how to create a monthly folder automatically
okay
now it's fine
I just need to make folders for each day within each month's folder.
thank you
-
May 22nd, 2024, 05:21 AM
#14
Re: how to create a monthly folder automatically
 Originally Posted by ChrisE
@Zvoni
just show your example, Ontro would have two samples to choose from
Can't. No vb6 available, only Office-VBA 64-Bit.
My "Solution" is more like "from the top of my head, what i can remember"
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
May 22nd, 2024, 07:10 AM
#15
Re: how to create a monthly folder automatically
 Originally Posted by ontro
okay
now it's fine
I just need to make folders for each day within each month's folder.
thank you
Maybe you should start your new threads with clear description of your needs.
In this thread after each given answer you come up with additional requirements.
It seems that you just want to create an hierarchical folder structure based on months and days.
And the top level folders need to be YYYY-MM-01 and the subfolders need to be per day.
I would create folders per year, in the year folder have folders for the 12 months and per month create a folder per day.
Code:
+2024
+ 01
+ 01
+ 02
..
+ 28
+ 29
+ 30
+ 31
+ 02
..
+ 03
..
+ 12
-
May 22nd, 2024, 07:20 AM
#16
Re: how to create a monthly folder automatically
 Originally Posted by Arnoutdv
Maybe you should start your new threads with clear description of your needs.
In this thread after each given answer you come up with additional requirements.
It seems that you just want to create an hierarchical folder structure based on months and days.
And the top level folders need to be YYYY-MM-01 and the subfolders need to be per day.
Any bets, that the next question is going to be "How to find out, if it's a Leap Year"?
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
May 22nd, 2024, 07:48 AM
#17
Re: how to create a monthly folder automatically
 Originally Posted by ontro
okay
now it's fine
I just need to make folders for each day within each month's folder.
thank you
you allready have a count of the Days in each month, you need to create another loop for each Day-Folder 01..02 etc..
Code:
'...
'get a Day count in that month
d = DateSerial(nYear, nMonth, 1)
nDays = Day(DateAdd("m", 1, d) - 1)
List1.AddItem ("First Day Of Month : " & Format(FirstDayOfMonth, "dddd dd.MM.yyyy"))
List1.AddItem ("Days in Month : " & nDays)
'....
hope you are not going into Hours and then .....
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
May 22nd, 2024, 09:21 AM
#18
Thread Starter
Hyperactive Member
Re: how to create a monthly folder automatically
I try to do this but it doesn't work
Code:
Dim nYear As Long
Dim nMonth As Long
Dim nDays As Long
Dim LastDayOfMonth As Date
Dim FirstDayOfMonth As Date
Dim d As Date
Dim lor As Long
nDays = 31
For nYear = 2024 To 2026
For nMonth = 1 To 12
For lor = 1 To nDays
LastDayOfMonth = DateSerial(nYear, nMonth + 1, 0)
FirstDayOfMonth = DateSerial(nYear, nMonth, 1)
d = DateSerial(nYear, nMonth, 1)
nDays = Day(DateAdd("m", 1, d) - 1)
FolderCreateNew App.Path & "\" & nYear & "\" & Format(FirstDayOfMonth, "dd-MM-yyyy") & " " & Format(LastDayOfMonth, "dd-MM-yyyy"), True & "\" & lor
Next lor
Next nMonth
Next nYear
-
May 22nd, 2024, 09:32 AM
#19
Re: how to create a monthly folder automatically
The code inside inner loop is not correct
nDays is calculated inside the loop, but you are using it as the "To" in the for loop
Also the call to FolderCreateNew seems not correct
Code:
For lor = 1 To nDays
LastDayOfMonth = DateSerial(nYear, nMonth + 1, 0)
FirstDayOfMonth = DateSerial(nYear, nMonth, 1)
d = DateSerial(nYear, nMonth, 1)
nDays = Day(DateAdd("m", 1, d) - 1)
FolderCreateNew App.Path & "\" & nYear & "\" & Format(FirstDayOfMonth, "dd-MM-yyyy") & " " & Format(LastDayOfMonth, "dd-MM-yyyy"), True & "\" & lor
Next lor
Do you want the following structure?:
Code:
+2024
+01-01-2024 31-01-2024
+1
+2
..
+31
Can you please show an example of the output you want?
A folder with the name "01-01-2024 31-01-2024" will be hard to access programmatically.
-
May 22nd, 2024, 09:39 AM
#20
Thread Starter
Hyperactive Member
Re: how to create a monthly folder automatically
correct
So what would an example with code be like?
thank you
-
May 22nd, 2024, 10:02 AM
#21
Re: how to create a monthly folder automatically
Code:
Option Explicit
Private Declare Function SHCreateDirectoryExW Lib "SHELL32.dll" (ByVal hwnd As Long, ByVal pszPath As Long, ByRef psa As Any) As Long
Private Sub Form_Load()
Dim lYear As Long, lMonth As Long, lDay As Long
Dim lLastDay As Long
Dim sYearFolder As String, sMonthFolder As String, sFolder As String
For lYear = 2024 To 2026
sYearFolder = App.Path & "\" & CStr(lYear)
CreateFolder sYearFolder
For lMonth = 1 To 12
lLastDay = Day(DateSerial(lYear, lMonth + 1, 0))
sMonthFolder = sYearFolder & "\" & Format(DateSerial(lYear, lMonth, 1), "DD\-MM\-YYYY") & " " & Format(DateSerial(lYear, lMonth, lLastDay), "DD\-MM\-YYYY")
CreateFolder sMonthFolder
For lDay = 1 To lLastDay
sFolder = sMonthFolder & "\" & CStr(lDay)
CreateFolder sFolder
Next lDay
Next lMonth
Next lYear
End Sub
Private Sub CreateFolder(sFolder As String)
Debug.Print sFolder
SHCreateDirectoryExW 0, StrPtr(sFolder), Nothing
End Sub
-
May 22nd, 2024, 10:20 AM
#22
Thread Starter
Hyperactive Member
Re: how to create a monthly folder automatically
resolved
thank you so much
-
May 23rd, 2024, 06:14 PM
#23
Thread Starter
Hyperactive Member
Re: [RESOLVED] how to create a monthly folder automatically
to get full path to save data in sequential file
lYear lMonth lDay
thank you
-
May 24th, 2024, 01:46 AM
#24
Re: [RESOLVED] how to create a monthly folder automatically
How to get these variables from a date variable?
Check the help more often. It’s as simple as using Year(), Month() and Day()
-
May 24th, 2024, 02:30 AM
#25
Re: [RESOLVED] how to create a monthly folder automatically
 Originally Posted by ontro
to get full path to save data in sequential file
lYear lMonth lDay
thank you
Is there a question somewhere i can't see?
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
May 24th, 2024, 01:12 PM
#26
Thread Starter
Hyperactive Member
Re: [RESOLVED] how to create a monthly folder automatically
In the month variable I get month 5 and it should come out 05-01-2024 05-31-2024
code
Code:
Dim route As String
route = App.Path & "\" & Year(Now) & "\" & Month(Now) & "\" & Day(Now) & "\" & (nom + ".txt")
thank you
-
May 24th, 2024, 01:15 PM
#27
Thread Starter
Hyperactive Member
Re: [RESOLVED] how to create a monthly folder automatically
]n the month variable I get month 5 and it should come out 01-05-2024 31-05-2024
Code:
Dim route As String
route = App.Path & "\" & Year(Now) & "\" & Month(Now) & "\" & Day(Now) & "\" & (nom + ".txt")
-
May 24th, 2024, 01:41 PM
#28
Re: [RESOLVED] how to create a monthly folder automatically
All the code for creating the folder name is post #21
-
May 24th, 2024, 03:03 PM
#29
Thread Starter
Hyperactive Member
Re: [RESOLVED] how to create a monthly folder automatically
this path is to save the data
in the month(now) variable it reads month 5
but it has to leave 05-01-2024 05-31-2024
and it doesn't come out
Code:
Dim route As String
route = App.Path & "\" & Year(Now) & "\" & Month(Now) & "\" & Day(Now) & "\" & (nom + ".txt")
-
May 24th, 2024, 04:07 PM
#30
Re: [RESOLVED] how to create a monthly folder automatically
The code for defining the folder is in post 21
Read it, take the folder building string code and use it.
I give up..
-
May 24th, 2024, 04:31 PM
#31
Thread Starter
Hyperactive Member
Re: [RESOLVED] how to create a monthly folder automatically
I give up
thank you so much
-
May 24th, 2024, 04:42 PM
#32
Re: [RESOLVED] how to create a monthly folder automatically
It's the international giving up day!
-
May 25th, 2024, 05:17 AM
#33
Thread Starter
Hyperactive Member
Re: [RESOLVED] how to create a monthly folder automatically
I need the current date of:
Code:
sMonthFolder = sYearFolder & "\" & Format(DateSerial(lYear, lMonth, 1), "DD\-MM\-YYYY") & " " & Format(DateSerial(lYear, lMonth, lLastDay), "DD\-MM\-YYYY"
to search
Code:
Dim route As String
route = App.Path & "\" & Year(Now) & "\" & sMonthFolder & "\" & Day(Now) & "\" & (nom + ".txt
thank you
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|