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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete contents of a Folder

Status
Not open for further replies.

jpotucek

Technical User
Jan 26, 2005
144
US
Here is my code (Someone on THIS forum helped me with it it) to rename a file with the current date and time.

What code can I ADD to this that will delete the previous days file??? In other words, I need to delete the contents of c:\schematic\uploads BEFORE renaming and moving my file... Can anyone help?

Dim file1, file2

file1 = "C:\schematic\olnsc_s.csv"
' file2 = "C:\schematic" & year(date) & month(date) & Day(Date) & ".txt"
file2 = "C:\schematic\uploads\Versus." & Year(Date) & Right("0" & Month(Date),2) & Right("0" & Day(Date),2) & ".csv"
Set fso = CreateObject("Scripting.FileSystemObject")

If fso.FileExists(file1) = true then
If fso.FileExists(file2) = true then
fso.DeleteFile(file2)
End if
fso.MoveFile file1, file2
'else
' msgbox "File does not exist"
End If

Set FSO = Nothing
 
previous days file
Use Date-1 for yesterday.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Just thought you should know this.

Using the FSO object to delete files and folders has a potential "gotchya". Files and folders deleted this way are NOT moved/tracked by the recycle bin.

This might be something to consider if there may be a need to recover the previous file for some unknown reason. I have a script that does something similar but I move/rename the previous files to an archive folder that is pruned once a month. I only add one file per month so this isn't a problem. You may want/need to short the archive retention.

Food for thought.


Thanks

John Fuhrman
Titan Global Services
 
Thanks! My file will be replaced once a day and deleting the previous days file is not a problem because the file is easily recreated. Just having a hard time figuring out how to code deleting the existing file due to the fact that the filename is different everyday!
 
Is this what you mean:

Dim file1, file2

file1 = "C:\schematic\olnsc_s.csv"
' file2 = "C:\schematic" & year(date) & month(date) & Day(Date) & ".txt"
file2 = "C:\schematic\uploads\Versus." & Year(Date) & Right("0" & Month(Date),2) & Right("0" & Day(Date),2) & ".csv"
Set fso = CreateObject("Scripting.FileSystemObject")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("C:\schematic\uploads\*.csv")

objFile.Delete

If fso.FileExists(file1) = true then
If fso.FileExists(file2) = true then
fso.DeleteFile(file2)
End if
fso.MoveFile file1, file2
'else
' msgbox "File does not exist"
End If

Set FSO = Nothing

It does not like the wildcard in line 9 :(
 
No...more like

objFSO.DeleteFile "C:\schematic\uploads\*.csv"

if you want it to force the delete

objFSO.DeleteFile "C:\schematic\uploads\*.csv", True

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
You are awesome!! Thank you so much! Getting my brain around this stuff has been hard.. but when someone helps me with some code and I can actually see it work, it definitely helps me understand it better.

I was wondering, how hard would it be to add a line or 2 of code to FTP my file?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top