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.
20 11183 - Private Sub File1_Click()
-
-
Kill (File1.Path & "\" & File1.FileName)
-
-
End Sub
-
-
Private Sub Dir1_Change()
-
-
File1.Path = Dir1.Path
-
-
End Sub
-
-
Private Sub Drive1_Change()
-
-
Dir1.Path = Drive1.Drive
-
-
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.
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.
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.
- Private Sub DeleteFile()
-
Dim FileSystemObject As Object
-
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
-
If FileSystemObject.FileExists(Dir1.Path & "\" & Trim(txtFileName.Text)) Then
-
FileSystemObject.DeleteFile Dir1.Path & "\" & Trim(txtFileName.Text)
-
End If
-
End Sub
Try This Code
Geoff was referring to the DriveListBox, DirListBox, and FileListBox controls.
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
You must add Reference of Microsoft Scripting Runtime to run this program
Project->Add Rererence and check microsoft scription runtime
that all
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.
You must replace the file with an empty text file
like
P - ublic Sub EmptyFile(FName As String)
-
Dim FileSystemObject As New FileSystemObject
-
FileSystemObject.CreateTextFile App.Path & "\" & FName, True
-
End Sub
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
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.
did you add refference of microsoft Scripting runtime??
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.
The code is working perfectly in my computer.
which version of vb ur using??
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.
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. - Open "YourFile.Txt" For Output Access Write Lock Write As #1
-
Close #1
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. - Open "YourFile.Txt" For Output Access Write Lock Write As #1
-
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.
The file system object is a class with vb. lots of file handling methods are avilable in it.
Try the following code... - Dim FObj As New FileSystemObject
-
FObj.CreateTextFile "C:\test345.txt", True
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.
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.
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 -
-
-
Public Class Form1
-
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
example(TextBox1.Text)
-
End Sub
-
Private Sub example(ByVal Message_To_Show As String)
-
-
msgbox(Message_To_Show)
-
-
End Sub
-
End Class
-
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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
|
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...
|
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...
|
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)...
|
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...
|
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
|
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...
|
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...
|
by: Thomas Bauer |
last post by:
Hello,
Call
DeleteFiles bgW_DeleteFilesProcess = new DeleteFiles();
bgW_DeleteFilesProcess.RunAsync( folder, 5, "*.txt", new
RunWorkerCompletedEventHandler( RunWorkerCompleted_DeleteFiles_TXT )...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
| |