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!

Make Folder with Date

Status
Not open for further replies.

bizybeaver

Technical User
Apr 7, 2004
26
US
How would I go about creating a folder based on today's date? Such as '9-17-04'

I've seen some things posed about CurrentDate or something, but it's way too much, since it has the time stamp as well.

I'm making a simple archival tool, but I'm at a loss as to creating a folder with a variable name...

Thanks in Advance!
 
Option Explicit
Dim oFSO

Set oFSO = CreateObject("Scripting.FileSystemObject")
oFSO.CreateFolder("C:\Archive\" & Month(Now()) & "-" _
& Day(Now()) & "-" & Year(Now()))
Set oFSO = Nothing

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Tom, you are awesome. I'm trying to be an aspiring programmer, and I constantly read through the postings here on Tek-Tips, and I have to say you are so helpful.

One quick question, why would I have to set oFSO = Nothing afterwards?
 
You don't technicaly have to, but it is a good habit to clean up your objects.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
One further step.

Now that I have my folder created. How do I tell the fso to copy a file from a "concrete" source to this variable folder?

I can't use an actual name for this folder when specifying the destination, since it will change...
 
Here's what I was trying:

Option Explicit
Dim ofile
Dim fso

set fso = createobject("scripting.filesystemobject")

set ofile = fso.getfile("Z:\Cover.jpg")
ofile.copy("c:\Archive\" & Month(Now()) & "-" & Day(Now()) & "-" & Year(Now()))

Set ofile = Nothing

Do I need to find out the name of the folder somehow instead? How is it you can "reference" file names?
 
You need to create the directory first.

Something like:
Code:
Option Explicit
Dim ofile
Dim fso
Dim strFolder

strFolder = "C:\Archive\" & Month(Now()) & "-" & Day(Now()) & "-" & Year(Now())
oFSO.CreateFolder(strFolder)
set fso = createobject("scripting.filesystemobject")
set ofile = fso.getfile("Z:\Cover.jpg")
ofile.copy(strFolder)
Set ofile = Nothing

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
I keep getting an object not defined for line 7, which is the 'Variable is undefiend: oFSO

So I've tried making a variable for oFSO, and even setting it to the filesystem object, but that doesn't work either.

I've tried distorting my code each and every way, and it seems, I just don't understand the syntax.

Know what went wrong?
 
Furthermore, if I can get it to work, it seems to make the folder but not copy the file. It keeps making a filename called 09-20-2004 with no extension.
 
Ok, my fault. It was untested code. Here are some corrections:
Code:
Option Explicit
Dim ofile
Dim fso
Dim strFolder

strFolder = "C:\Archive\" & Month(Now()) & "-" & Day(Now()) & "-" & Year(Now())
set fso = createobject("scripting.filesystemobject")
oFSO.CreateFolder(strFolder)
set ofile = fso.getfile("Z:\Cover.jpg")
ofile.copy(strFolder & "\Cover.jpg")
Set ofile = Nothing

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
And Thank you Again. I had to dim the oFSO, to get the code to work.

While I'm on this topic, I was experimenting, trying to get my brain wrapped around this. Why couldn't I use the FileCopy command like this. It will make the folder, but won't actually copy the file into the destination, seems that I can't figure out how to use the right syntax for the strFolder, I've tried ( and " to no avail...

strFolder = "C:\Archive\" & Month(Now()) & "-" & Day(Now()) & "-" & Year(Now())
set fso = createobject("scripting.filesystemobject")

fso.CopyFile("Z:\Cover.jpg"), "strFolder
 
Try this:

fso.CopyFile "Z:\Cover.jpg", strFolder

Remember that the folder must exist first.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top