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!!!!!!!!!!
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.
Re: vb6 - Delete folder with yesterdays date
I think you have to delete the files in the folder before deleting the folder
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.
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")
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)
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? :ehh:
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
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.
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.
Quote:
Aren't both Now() and Date$ returning variants? :ehh:
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.
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...