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

Delete file or folder

P: 48
Dear friends,

Is there anyway to write a program in vb in order to delete a file in my computer?For instance, I have a .txt file in C Drive, which is "C:\testing.txt". So, now I want to click a button in vb and delete this file. Of course, I would like to delete any file from the computer, so I hope can have a text box or some drop down box that can choose any files from my computer.

Is it possible?

Appreciate for any reply. Thank you.

Best Regards.
Mar 1 '07 #1
Share this Question
Share on Google+
20 Replies


P: 17
Expand|Select|Wrap|Line Numbers
  1. Private Sub File1_Click()
  2.  
  3.     Kill (File1.Path & "\" & File1.FileName)
  4.  
  5. End Sub
  6.  
  7. Private Sub Dir1_Change()
  8.  
  9.    File1.Path = Dir1.Path
  10.  
  11. End Sub
  12.  
  13. Private Sub Drive1_Change()
  14.  
  15.    Dir1.Path = Drive1.Drive
  16.  
  17. End Sub
With that code you'll need a Drive Dir and File tools so you can view any file / folder on the system. Or you can just have a text box for the user to type the path into. What ever floats your boat.
This works with a single click on the file in a ListView to delete it.

Geoff.
Mar 1 '07 #2

P: 48
Dear Geoff,

I had tried the code, but there is nothing appear. I mean I had put a combo box for Drive1, text boxes for File1 and Dir1. When I run the program, it did not appear anything. The Combo box and text boxes are empty. What happen?

By the way, do you mean that I can choose the file I want because of this "browser"?(It is called a browser right? ^^ ) But I need to press "delete" at my keyboard to delete the file. Actually, I would like to delete a file in VB directly, but not pressing the keyboard.

Thanks for the reply.

Best Regards.
Mar 2 '07 #3

hariharanmca
100+
P: 1,977
Dear Geoff,

I had tried the code, but there is nothing appear. I mean I had put a combo box for Drive1, text boxes for File1 and Dir1. When I run the program, it did not appear anything. The Combo box and text boxes are empty. What happen?

By the way, do you mean that I can choose the file I want because of this "browser"?(It is called a browser right? ^^ ) But I need to press "delete" at my keyboard to delete the file. Actually, I would like to delete a file in VB directly, but not pressing the keyboard.

Thanks for the reply.

Best Regards.

Expand|Select|Wrap|Line Numbers
  1. Private Sub DeleteFile()
  2.     Dim FileSystemObject As Object
  3.     Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
  4.     If FileSystemObject.FileExists(Dir1.Path & "\" & Trim(txtFileName.Text)) Then
  5.         FileSystemObject.DeleteFile Dir1.Path & "\" & Trim(txtFileName.Text)
  6.     End If
  7. End Sub
Try This Code
Mar 2 '07 #4

Expert 5K+
P: 8,434
Geoff was referring to the DriveListBox, DirListBox, and FileListBox controls.
Mar 2 '07 #5

P: 48
Geoff was referring to the DriveListBox, DirListBox, and FileListBox controls.

Do I need to enable any component to use this? Actually, I had seen this code b4, but I had the same result, it was blank in the Box, nothing appear at all.

Thanks for the reply.

Best Regards
Mar 3 '07 #6

P: 88
You must add Reference of Microsoft Scripting Runtime to run this program
Project->Add Rererence and check microsoft scription runtime
that all
Mar 3 '07 #7

P: 48
You must add Reference of Microsoft Scripting Runtime to run this program
Project->Add Rererence and check microsoft scription runtime
that all
Dear friends,

Thanks for the reply, it works!!!! Sorry for previous reply, I was the one who make big mistake.

By the way, since I can delete the file now, I would like to know how to replace a same file back to the same place, with empty data of course.

I was trying to delete the file, for instance, "testing.txt" is because there are unwanted data stored in it. I want to delete it and replace an empty data "testing.txt" in the same place. How can I do it?

Again, thanks for the reply. You guys are the best. haha^^

Best Regards.
Mar 3 '07 #8

P: 88
You must replace the file with an empty text file

like

P
Expand|Select|Wrap|Line Numbers
  1. ublic Sub EmptyFile(FName As String)
  2.     Dim FileSystemObject As New FileSystemObject
  3.     FileSystemObject.CreateTextFile App.Path & "\" & FName, True
  4. End Sub
Mar 3 '07 #9

P: 48
Dear friends,

Hrm, actually, instead of delete and replace a same file, is there any other way such like delete the data ONLY?

Wow, VB is tough.

Best Regards
Mar 4 '07 #10

P: 48
Dear sukeshchand,

The code I had change a bit but it is not working.

Public Sub Command1_Click()
Dim FileSystemObject As New FileSystemObject
FileSystemObject.CreateTextFile "C:\test345.txt", True
End Sub

Well, I had tried your code, but not working as well. By the way, I need a button to activate it, so I change something. Of course, it is not working then.haha^^

Best Regards.
Mar 4 '07 #11

P: 88
did you add refference of microsoft Scripting runtime??
Mar 4 '07 #12

P: 48
did you add refference of microsoft Scripting runtime??
Yes I did. I even try the original code you gave. But both of my code and your code are not working. Hrm...still trying to solve.

Best Regards.
Mar 4 '07 #13

P: 88
The code is working perfectly in my computer.
which version of vb ur using??
Mar 4 '07 #14

P: 48
The code is working perfectly in my computer.
which version of vb ur using??

VB 6.0. By the way, you mean my code or your code? Cuz I need a button to activate it, so if your code working but not mine, then I still need to work it out. hehe^^.

And one more thing, I dont really understand your code. Especially the

1) Public Sub EmptyFile(FName As String)
--->why there are declaration in the bracket?

2)Dim FileSystemObject As New FileSystemObject
--->why both name are same?

Thanks for the reply.

Best Regards.
Mar 4 '07 #15

Expert 5K+
P: 8,434
Personally, I think that the FileSystemObject is overkill for such a simple operation. To empty out the contents of a file, you can can use two simple statements.
Expand|Select|Wrap|Line Numbers
  1. Open "YourFile.Txt" For Output Access Write Lock Write As #1
  2. Close #1
Mar 4 '07 #16

P: 48
Personally, I think that the FileSystemObject is overkill for such a simple operation. To empty out the contents of a file, you can can use two simple statements.
Expand|Select|Wrap|Line Numbers
  1. Open "YourFile.Txt" For Output Access Write Lock Write As #1
  2. Close #1

Thanks for the reply, it works!!! This code is easier than delete and replace another file. haha^^

By the way, the code that used FileSystemObject still not working. I hope I can work it out later, I need to learn more than one way.

Thanks again everyone.

Best Regards.
Mar 5 '07 #17

P: 88
The file system object is a class with vb. lots of file handling methods are avilable in it.

Try the following code...
Expand|Select|Wrap|Line Numbers
  1. Dim FObj As New FileSystemObject
  2. FObj.CreateTextFile "C:\test345.txt", True
Mar 5 '07 #18

Expert 5K+
P: 8,434
Thanks for the reply, it works!!! This code is easier than delete and replace another file. haha^^
Glad to hear it worked out. :)

By the way, the code that used FileSystemObject still not working. I hope I can work it out later, I need to learn more than one way.
Yes, you should. The FileSystemObject provides lots of options - this was a very simple case. And it's good to know more than one way to do things.
Mar 5 '07 #19

P: 1
Hello everyone---

kuzure, if it's alright, I'm going to steal your spotlight for a moment. If I could trouble someone really quickly, that would be great. =P

I am trying to delete a file as well (well, really, it's a program I am building for my friend): he happens to play World of Warcraft and there is a Cache folder within WoW's (World of Warcraft) respective directory -- usually based in Program Files. Everytime WoW is run, it adds on to the Cache folder with every new "encounter" with an item, creature, etc... In order to play different private servers, my friend, as does everyone else, is encouraged to delete their Cache folder in order for a server's settings to work out properly. Every time you delete it, then run WoW, it re-creates itself to store the new settings.

I am working on having a button be set to delete the Cache folder and another to run the game. If I could possibly be informed of a code using: Const DelCache As String = "C:\Program Files\World of Warcraft\Cache" as the constant used in the click event for the button deleting the Cache folder.

What I am hoping to do is have it check if the folder exists, then to delete it if so. So obviously I need an If-Then-Else statement containing the folder existence-check and the deletion.

As for the code pertaining to running WoW -- a simple code would do just fine, although there really isn't a complex one at that.
Oct 14 '08 #20

100+
P: 283
some of the questions here are very basic and although it is great to help please do start with a few basic tutorials before posting , there are many great links and tutorials available via google or your prefered search engine

there are also many filesystem object tutorials available


for the code 'Dim FileSystemObject As New FileSystemObject '

you cannot use the filesystem object directly so you need to create a new instance of it,

click here for some more info on FSO

(apologies if I am miss understanding the question )

below is an example of a very simple sub called ' example ' when you click button 1 it calls the sub and passes it the value of textbox1 which the sub then shows in a message box

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. Public Class Form1
  4.  
  5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  6.         example(TextBox1.Text)
  7.     End Sub
  8.     Private Sub example(ByVal Message_To_Show As String)
  9.  
  10.         msgbox(Message_To_Show)
  11.  
  12.     End Sub
  13. End Class
  14.  
  15.  
Oct 15 '08 #21

Post your reply

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