By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,780 Members | 1,126 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,780 IT Pros & Developers. It's quick & easy.

Hi..Help Needed for deletion using VB

P: 19
Hi

I need one small help in VB
I have written a macro which produces some output and stores them in the form of an excel files (about 10 files are generated) in a particular directory in C:\.
The requirement is that each time when the macro runs, it shall first delete the existing files in that directory and rewrite new files.So the problem comes in deletion of existing file...The generation of file is taken care by macro.
If u can provide me the code for deletion, I will b thankful

Regards
Sanket
Nov 16 '06 #1
Share this Question
Share on Google+
7 Replies


100+
P: 267
Hi

I need one small help in VB
I have written a macro which produces some output and stores them in the form of an excel files (about 10 files are generated) in a particular directory in C:\.
The requirement is that each time when the macro runs, it shall first delete the existing files in that directory and rewrite new files.So the problem comes in deletion of existing file...The generation of file is taken care by macro.
If u can provide me the code for deletion, I will b thankful

Regards
Sanket
hi

use the statement KILL path & filename
Nov 16 '06 #2

P: 19
hi

use the statement KILL path & filename
Hi,

Thanks for the reply...I tried that before using the following function code

Function delfiles1()

Dim strFldr As String
Dim strFile As String

strFldr = "C:\Results\Reports1" ' select the directory
strFile = Dir(strFldr & "\*.*") ' get list of files in folder

Kill strFldr & "\" & strFile

End Function

But the problem is that it is not deleting the files using above code and hence when the macro runs for the second time ....it shows up the message while saving the file .."that the file already exists".
Can you please review and let me know in case there is anything wrong with the code. Please note that the directory contains several files and I need to delete all of them together.

Regards
Sanket
Nov 16 '06 #3

P: 19
Please note that the code deletes in case there is only one file in the directory but does not deletes in case there are multiple files.
Nov 16 '06 #4

100+
P: 267
Please note that the code deletes in case there is only one file in the directory but does not deletes in case there are multiple files.
hi

strFldr = "C:\Results\Reports1" ' select the directory
strFile = Dir(strFldr & "\*.*") ' get list of files in folder

Kill strFldr & "\" & strFile

why not:

strFiles="c:\result\reports1\*.*"
Kill strFiles
Nov 17 '06 #5

100+
P: 267
or even

Private Function delfiles()
On Error Goto Fail
Kill "c:\result\reports1\*.*"
Fail:
Err.Clear
End Function

the errortrap just in case there is no file in your folder.
Nov 17 '06 #6

P: 5
Please note that the code deletes in case there is only one file in the directory but does not deletes in case there are multiple files.
Just as a matter of explanation...

the Dir() function returns the name (not the full path) of the _first_ file found for the specified string. So Dir("*.*") will return only one name and so the Kill statement only deletes that file.
Nov 17 '06 #7

P: 19
Hi,

Thanks to both of you.
The code worked perfectly

Regards
Sanket
Nov 17 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.