Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create a folder using Date 1

Status
Not open for further replies.

bobbyforhire

Technical User
Mar 11, 2008
253
US
I am creating a program that grabs my security logs and then moves the event log into a folder.

Starts at C:\LOGS
then
C:\LOGS\NAMEOFSERVER
then
DATE



The issue i am running into is that i want to use the date but when you get the DATE it will show you 6/24/2008 when i need for it to be 6\24\2008. or even better 2008\6\24

example of code:

If fso.FolderExists("C:\LOGS") Then
Else
Set fldr = fso.CreateFolder("C:\LOGS\")
End If

If fso.FolderExists("C:\LOGS\" & Combo1.Text) Then
Else
Set fldr = fso.CreateFolder("C:\LOGS\" & Combo1.Text)
End If



If fso.FolderExists("C:\LOGS\" & Combo1.Text & Date) Then
MsgBox "yup date"
Else
Set fldr = fso.CreateFolder("C:\LOGS\" & Combo1.Text & "\" & Date)
End If


It will crash because of the date...anyone have a good way of getting this done? I have already tried day,month,year but that doesn't seem to work.

 

Did you try format it?
Code:
Debug.Print Format$(Date, "YYYY\MM\DD")
or, if that does not work, you can always do:
Code:
Debug.Print Year(Date) & "\" Month(Date) & "\" & Day(Date)


Have fun.

---- Andy
 
Hi,

I generally use the following (VBA) code to generate a string to create a data-based folder


Code:
cstr(year(now)) + rightpad(cstr(month(now))) + rightpad(cstr(day(now)))

In order to generate the correct format (YYYYMMDD) ie 20080624, I use the following function

Code:
function rightpad(val_in)

rightpad = right("00" + val_in, 2)
end function

This can then be passed into the CreateFolder function at the end of any root path you want.

Hope it helps

**************************************************************
Rock is Dead (Long Live Paper and Scissors)
**************************************************************
 

I don't think that will work. You will get a "Path not found" because you are trying to create several levels of sub folders with one CreateFolder method.

This will produce something like
Code:
Set fldr = fso.CreateFolder(X & "2008\06\26")
where "X" is the top level folder; "2008" is a sub-folder under it; "06" is a sub-folder under that and "26" is yet another level of sub folder under "06".

Try something like
Code:
Set fldr = fso.CreateFolder("C:\LOGS\" & Combo1.Text & "\" & Format(Date,"yyyy-mm-dd")

 
Well i broke it down and this seems to be working...

If fso.FolderExists("C:\LOGS\" & Combo1.Text & "\" & Year(Date)) Then
Else
Set fldr = fso.CreateFolder("C:\LOGS\" & Combo1.Text & "\" & Year(Date))
End If

If fso.FolderExists("C:\LOGS\" & Combo1.Text & "\" & Year(Date) & "\" & Month(Date)) Then
Else
Set fldr = fso.CreateFolder("C:\LOGS\" & Combo1.Text & "\" & Year(Date) & "\" & Month(Date))
End If


If i tried to use it all as once it would have an issue creating the folder's.
 
Andrzejek

You need to do it this way
Code:
Debug.Print Format$(Date, "YYYY\\MM\\DD")

"\" is the escape character for format strings. With only one "\" you will get "2008M6D24
 
You cannot use the characters \ / : * ? " < > | to be part of a file name or folder.


BTW
Code:
If fso.FolderExists("C:\LOGS\" & Combo1.Text & "\" & Year(Date)) Then
Else
Set fldr = fso.CreateFolder("C:\LOGS\" & Combo1.Text & "\" & Year(Date))
End If

is equal to
Code:
If Not fso.FolderExists("C:\LOGS\" & Combo1.Text & "\" & Year(Date)) Then
Set fldr = fso.CreateFolder("C:\LOGS\" & Combo1.Text & "\" & Year(Date))
End If
 
>You cannot use the characters \ / : * ? " < > | to be part of a file name or folder.


Well ... there ARE ways ... but let's not go there ... ;-)
 
strongm

I would like a tour, if you have the time.
If it costs me a new thread to be opened, I 'll be glad to
 
It is the filehandling API calls in Win32 that enforce the apparant restriction. NTFS itself only restricts the use of \0 (null) and /.

But there is a special escape sequence (\\?\) that can be prepended that basically tells many of the Win32 API calls not to enforce their rules.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top