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

Discussion in 'ASP General' started by Ken Fine, Jan 11, 2004.

  1. Ken Fine

    Ken Fine Guest

    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
    Ken Fine, Jan 11, 2004
    #1
    1. Advertising

  2. <%
    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

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




    "Ken Fine" <> wrote in message
    news:btsf3c$n4m$...
    > 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
    >
    >
    >
    Aaron Bertrand [MVP], Jan 12, 2004
    #2
    1. Advertising

  3. Ken Fine

    Ken Fine Guest

    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




    "Aaron Bertrand [MVP]" <> wrote in message
    news:%...
    > <%
    > 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
    >
    > --
    > Aaron Bertrand
    > SQL Server MVP
    > http://www.aspfaq.com/
    >
    >
    >
    >
    > "Ken Fine" <> wrote in message
    > news:btsf3c$n4m$...
    > > 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
    > >
    > >
    > >

    >
    >
    Ken Fine, Jan 12, 2004
    #3
  4. 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/




    "Ken Fine" <> wrote in message
    news:#...
    > 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
    >
    >
    >
    >
    > "Aaron Bertrand [MVP]" <> wrote in message
    > news:%...
    > > <%
    > > 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
    > >
    > > --
    > > Aaron Bertrand
    > > SQL Server MVP
    > > http://www.aspfaq.com/
    > >
    > >
    > >
    > >
    > > "Ken Fine" <> wrote in message
    > > news:btsf3c$n4m$...
    > > > 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
    > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Aaron Bertrand [MVP], Jan 12, 2004
    #4
  5. Ken Fine

    Ken Fine Guest

    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

    "Aaron Bertrand [MVP]" <> wrote in message
    news:...
    > 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/
    >
    >
    >
    >
    > "Ken Fine" <> wrote in message
    > news:#...
    > > 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
    > >
    > >
    > >
    > >
    > > "Aaron Bertrand [MVP]" <> wrote in message
    > > news:%...
    > > > <%
    > > > 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
    > > >
    > > > --
    > > > Aaron Bertrand
    > > > SQL Server MVP
    > > > http://www.aspfaq.com/
    > > >
    > > >
    > > >
    > > >
    > > > "Ken Fine" <> wrote in message
    > > > news:btsf3c$n4m$...
    > > > > 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
    > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Ken Fine, Jan 12, 2004
    #5
  6. 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





    > 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
    >
    > "Aaron Bertrand [MVP]" <> wrote in message
    > news:...
    > > 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/
    > >
    > >
    > >
    > >
    > > "Ken Fine" <> wrote in message
    > > news:#...
    > > > 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
    > > >
    > > >
    > > >
    > > >
    > > > "Aaron Bertrand [MVP]" <> wrote in message
    > > > news:%...
    > > > > <%
    > > > > 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
    > > > >
    > > > > --
    > > > > Aaron Bertrand
    > > > > SQL Server MVP
    > > > > http://www.aspfaq.com/
    > > > >
    > > > >
    > > > >
    > > > >
    > > > > "Ken Fine" <> wrote in message
    > > > > news:btsf3c$n4m$...
    > > > > > 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
    > > > > >
    > > > > >
    > > > > >
    > > > >
    > > > >
    > > >
    > > >

    > >
    > >

    >
    >
    Aaron Bertrand [MVP], Jan 12, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michal Maciejczak
    Replies:
    1
    Views:
    1,978
    Matthias Kaeppler
    Apr 23, 2005
  2. Bill
    Replies:
    10
    Views:
    730
    Fulvio
    Apr 13, 2006
  3. Replies:
    1
    Views:
    444
    Steve W. Jackson
    Dec 22, 2006
  4. Highlander
    Replies:
    13
    Views:
    2,879
    Highlander
    Jan 2, 2007
  5. Mufasa
    Replies:
    0
    Views:
    397
    Mufasa
    Sep 5, 2008
Loading...

Share This Page