473,394 Members | 1,946 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Delete file or folder

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
20 11183
Geoff
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
kuzure
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
1,977 1GB
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
Killer42
8,435 Expert 8TB
Geoff was referring to the DriveListBox, DirListBox, and FileListBox controls.
Mar 2 '07 #5
kuzure
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
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
kuzure
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
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
kuzure
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
kuzure
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
did you add refference of microsoft Scripting runtime??
Mar 4 '07 #12
kuzure
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
The code is working perfectly in my computer.
which version of vb ur using??
Mar 4 '07 #14
kuzure
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
Killer42
8,435 Expert 8TB
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
kuzure
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
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
Killer42
8,435 Expert 8TB
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
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
jg007
283 100+
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

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

Similar topics

3
by: Alf Laue | last post by:
Hi, how do I delete a folder at my ftp-server, that was automatically created through a installation script? I dont have the permission to delete that folder :-( Thx for your help Alf
4
by: Mark Reed | last post by:
Hi all, I have the following code which imports the contents of all files within a set folder which works excellently. Once it has imported from each file, it deletes the file. Is there a way that...
3
by: News | last post by:
Is it possible to delete a file by copying it to the "bit bucket" or "null device"? Back in my youth when I live in VMS-land you could delete a file by copying it to NL: ========== I have...
9
by: Paul | last post by:
I'm trying to make get my app to delete all the files in a specified folder and all the files within the folders of the specified folder. e.g. Folder 1 contains three files (File1, File2, File3)...
2
by: createdbyx | last post by:
I am trying to make a file sync utillity to sync files between my laptop and my desktop pc. On my desktop machine (xp pro sp2) I have shared my "Visual Studio Projects" folder using windows simple...
3
by: ad | last post by:
Hi, How can I delete all files in a folder by C# for example, I want to delete all files under c:\Test
24
by: biganthony via AccessMonster.com | last post by:
Hi, I have the following code to select a folder and then delete it. I keep getting a Path/File error on the line that deletes the actual folder. The line before that line deletes the files in...
4
by: - HAL9000 | last post by:
When un-installing an application... Is it normal practice to write a special program that erases all the files and folders for all the users of an application that reads and writes to...
2
by: Thomas Bauer | last post by:
Hello, Call DeleteFiles bgW_DeleteFilesProcess = new DeleteFiles(); bgW_DeleteFilesProcess.RunAsync( folder, 5, "*.txt", new RunWorkerCompletedEventHandler( RunWorkerCompleted_DeleteFiles_TXT )...
0
by: wolfsbane | last post by:
Alright, here it is I am trying to write a win32 app in VB 2005 to clean up user's profiles. everything works correctly except for the Delete("directory", True) statement. I get a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.