FSO question: create directory/subdir based on month/year

K

Ken Fine

I want my application to maintain a directory tree based on months and
years, e.g.:

2004
January
file
file
file
February
file
file
...
Anyone have some FSO code that would do something similar to the following?
Grab the current date based on the system time
If user tries to do X action and the appropriate year directory doesn't
exist, create a directory named by the year (e.g. 2004)
If the appropriate month directory doesn't exist, create a subdirectory of
the appropriate year directory named by the month
Copy file (or do whatever) to the appropriate year/month directory based on
the current system time.

Thanks in advance.

-KF
 
A

Aaron Bertrand [MVP]

<%
yearFolder = server.Mappath("/" & year(date()) & "/") & "\"
monthFolder = yearFolder & monthname(month(date())) & "\"

set fso = CreateObject("Scripting.FileSystemObject")
if not fso.folderExists(monthFolder) then
fso.createFolder monthFolder
if not fso.folderExists(yearFolder) then
fso.createFolder yearFolder
end if
end if

' now, you can copy anything to monthFolder

set fso = nothing

%>

Maybe a useful tutorial here?
http://www.aspfaq.com/2039
 
K

Ken Fine

Ah, Aaron, so good, thanks so much. I already have the code running and am
madly writing millions of files to my filesystem with datetime stamps
concatinated to filenames. Soon I will be auto-categorizing photo uploads,
doing file maintenance, writing dynamic pages to static instances, and many
other cool things with the wonders of FSO.

Folks who want to play around with image management and manipulation in the
context of CMS-ey type stuff should look at the zImage component...I expect
I'll be able to do some pretty amazing things with this and a little FSO.

I think I noted a small error in the code you provided; I had to reverse the
sequence of checks on monthFolder and yearFolder to get this to work, as
shown below:

set fso = CreateObject("Scripting.FileSystemObject")

if not fso.folderExists(yearFolder) then
fso.createFolder yearFolder
if not fso.folderExists(monthFolder) then
fso.createFolder monthFolder
end if
end if
response.write monthFolder
'now, you can copy anything to monthFolder

Thanks again for all of your help.

-KF
 
A

Aaron Bertrand [MVP]

I had nested them with this logic in mind: "if the month folder doesn't yet
exist, then we know we have to create the month folder. We might *also*
have to create the year folder." I didn't test it thoroughly, but it
certainly seems like it should work as written. I'm not sure how you can
expect the yearFolder check to return false but the monthFolder to return
true. If the monthFolder returns true, we don't have to check if its parent
exists...

--
Aaron Bertrand
SQL Server MVP
http://www.aspfaq.com/
 
K

Ken Fine

Naive question, but is it possible that the month subfolder can't be created
before the parent year folder is in place?

That was the assumption that led me to try switching the two around...code
worked after. But maybe I made another change without noticing...

-KF
 
A

Aaron Bertrand [MVP]

Oh yeah, I did that in the wrong order. Like I said, testing wasn't
extensive. :)

BTW, the way you have it, the month folder will only get created if the year
folder doesn't already exist. My nesting was correct, just got the order of
operations wrong.

yearFolder = server.Mappath("/" & year(date()) & "/") & "\"
monthFolder = yearFolder & monthname(month(date())) & "\"

set fso = CreateObject("Scripting.FileSystemObject")
if not fso.folderExists(monthFolder) then
if not fso.folderExists(yearFolder) then
fso.createFolder yearFolder
end if
fso.createFolder monthFolder
end if
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top