Results 1 to 11 of 11

Thread: vb6 - Delete folder with yesterdays date

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2008
    Posts
    170

    vb6 - Delete folder with yesterdays date

    Hi everybody, can someone kindly tell me the most simple way to delete a specific folder with yesterdays date.
    My code creates a test folder everyday with the current date.
    fso.createfolder "c:\" & "Test" & Date$

    what i want to do is to that before the code creates folder with current date, it should delete folder with the name "Test" alongwith yesterdays date...

    if fso.folderexist("c:\" & "Test" & Date$-1) = True then
    fso.deletfolder "c:\" & "Test" & Date$-1
    fso.createfolder "c:\" & "Test" & Date$
    But it doesn't delete and i know something wrong with the underlined code.

    Thanx!!!!!!!!!!

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: vb6 - Delete folder with yesterdays date

    One problem is that Date$ does not return a date - it returns a String containing a formatted representation of a date.

    You can use (Date - 1) to get yesterdays date, and when you append it to a String it will be formatted the same way as Date$


    An important thing to note is that the formatting used in either of these cases is dependent on the options in "Control Panel"->"Regional Settings", so if that ever changes your code will not find the previous folder. It would probably be better to use the Format function to specify the formatting.

  3. #3
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: vb6 - Delete folder with yesterdays date

    I think you have to delete the files in the folder before deleting the folder
    Waiting for a full featured smart phone with out marrying a provider
    Go Android
    Go raiders

  4. #4
    Junior Member
    Join Date
    Dec 2008
    Posts
    17

    Re: vb6 - Delete folder with yesterdays date

    You have to strip the date from time. If you compare date to date they will never equal and this is for a simple reason. The date field has hours, minutes, seconds. So, compare only the date stripped from time in both cases and this should work.

  5. #5
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: vb6 - Delete folder with yesterdays date

    Quote Originally Posted by tfbasta
    You have to strip the date from time. If you compare date to date they will never equal and this is for a simple reason. The date field has hours, minutes, seconds. So, compare only the date stripped from time in both cases and this should work.
    or you could use the Format command
    PHP Code:
    Format(Date$,"mm-dd-yyyy"
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: vb6 - Delete folder with yesterdays date

    As I mentioned above, Date$ returns a formatted String - based on a quick check, the equivalent of Format(Date,"mm-dd-yyyy")

    Passing a formatted String (such as Date$) to a function like Format is a very bad idea, as the way the value is interpreted varies based on Regional Settings - and would be the wrong value on my computer (UK date format is dd/mm/yyyy) if the day is 12 or less.

    For more information, see the article Why are my dates not working properly? from our Classic VB FAQs (in the FAQ forum, which is shown near the top of our home page)

  7. #7
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228

    Re: vb6 - Delete folder with yesterdays date

    Quote Originally Posted by si_the_geek
    As I mentioned above, Date$ returns a formatted String - based on a quick check, the equivalent of Format(Date,"mm-dd-yyyy")

    Passing a formatted String (such as Date$) to a function like Format is a very bad idea, as the way the value is interpreted varies based on Regional Settings - and would be the wrong value on my computer (UK date format is dd/mm/yyyy) if the day is 12 or less.

    For more information, see the article Why are my dates not working properly? from our Classic VB FAQs (in the FAQ forum, which is shown near the top of our home page)
    I tried a test. I changed the regional settings on my computer and set the date to 11-Jan-2009.
    If I use a Format(Date$,"mm-dd-yyyy"), it screws up the output.
    If I use Format$(Now,"mm-dd-yyyy"), the regional settings do not seem to affect the output.

    Aren't both Now() and Date$ returning variants?
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  8. #8
    Junior Member
    Join Date
    Dec 2008
    Posts
    17

    Re: vb6 - Delete folder with yesterdays date

    Another way to do it is to convert the date to long. Here is an example.

    Code:
    fso.createfolder "c:\" & "Test" & CStr(CLng(Date))
    'Will give you c:\test39480 for today's date January 26, 2009
    
    'Then tomorrow you can use the following two lines to delete it
    if fso.folderexist("c:\" & "Test" & CStr(CLng(Date) - 1))= True then
    fso.deletfolder "c:\" & "Test" & CStr(CLng(Date) - 1)

    This way no matter what date format or systemdate format you are using it will work.

    Good luck,

    tfbasta
    Last edited by tfbasta; Jan 27th, 2009 at 10:37 AM.

  9. #9
    Junior Member
    Join Date
    Dec 2008
    Posts
    17

    Re: vb6 - Delete folder with yesterdays date

    Or if you still want it to be "dd-mm-yyyy" you can do the following changes

    Code:
    fso.createfolder "c:\" & "Test" & CStr(Format(CDate(CLng(Date)), "DD-MM-YYYY"))
    'Will give you c:\test39480 for today's date January 26, 2009
    
    'Then tomorrow you can use the following two lines to delete it
    if fso.folderexist("c:\" & "Test" & CStr(Format(CDate(CLng(Date) - 1), "DD-MM-YYYY")) = True then
    fso.deletfolder "c:\" & "Test" & CStr(Format(CDate(CLng(Date) - 1), "DD-MM-YYYY"))
    Please note that Date is a vb function that returns today's date.

  10. #10
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: vb6 - Delete folder with yesterdays date

    CDate(CLng(Date)) does more work than it needs to - just using Date by itself will give the same results (but if you use Now rather than Date, you would need that extra work to remove the time portion).

    Quote Originally Posted by abhijit
    I tried a test. I changed the regional settings on my computer and set the date to 11-Jan-2009.
    If I use a Format(Date$,"mm-dd-yyyy"), it screws up the output.
    If I use Format$(Now,"mm-dd-yyyy"), the regional settings do not seem to affect the output.
    That is to be expected.
    Aren't both Now() and Date$ returning variants?
    As the FAQ article explains, no.

    Now and Date (without a $) return Variant with a sub-type of Date, which is essentially the same thing as a Date.

    Date$ (with a $) returns a String.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Nov 2008
    Posts
    170

    Re: vb6 - Delete folder with yesterdays date

    Thanx to all!!!

    Based on your suggestions i am trying to test and figure out the best possible solution for my code. Will update u guyz shortly...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width