Delete file or folder

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kuzure
    New Member
    • Dec 2006
    • 48

    Delete file or folder

    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.
  • Geoff
    New Member
    • Feb 2007
    • 17

    #2
    Code:
    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.

    Comment

    • kuzure
      New Member
      • Dec 2006
      • 48

      #3
      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.

      Comment

      • hariharanmca
        Top Contributor
        • Dec 2006
        • 1977

        #4
        Originally posted by kuzure
        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.

        Code:
        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

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Geoff was referring to the DriveListBox, DirListBox, and FileListBox controls.

          Comment

          • kuzure
            New Member
            • Dec 2006
            • 48

            #6
            Originally posted by Killer42
            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

            Comment

            • sukeshchand
              New Member
              • Jan 2007
              • 88

              #7
              You must add Reference of Microsoft Scripting Runtime to run this program
              Project->Add Rererence and check microsoft scription runtime
              that all

              Comment

              • kuzure
                New Member
                • Dec 2006
                • 48

                #8
                Originally posted by sukeshchand
                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.tx t" is because there are unwanted data stored in it. I want to delete it and replace an empty data "testing.tx t" in the same place. How can I do it?

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

                Best Regards.

                Comment

                • sukeshchand
                  New Member
                  • Jan 2007
                  • 88

                  #9
                  You must replace the file with an empty text file

                  like

                  P
                  Code:
                  ublic Sub EmptyFile(FName As String)
                      Dim FileSystemObject As New FileSystemObject
                      FileSystemObject.CreateTextFile App.Path & "\" & FName, True
                  End Sub

                  Comment

                  • kuzure
                    New Member
                    • Dec 2006
                    • 48

                    #10
                    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

                    Comment

                    • kuzure
                      New Member
                      • Dec 2006
                      • 48

                      #11
                      Dear sukeshchand,

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

                      Public Sub Command1_Click( )
                      Dim FileSystemObjec t As New FileSystemObjec t
                      FileSystemObjec t.CreateTextFil e "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.

                      Comment

                      • sukeshchand
                        New Member
                        • Jan 2007
                        • 88

                        #12
                        did you add refference of microsoft Scripting runtime??

                        Comment

                        • kuzure
                          New Member
                          • Dec 2006
                          • 48

                          #13
                          Originally posted by sukeshchand
                          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.

                          Comment

                          • sukeshchand
                            New Member
                            • Jan 2007
                            • 88

                            #14
                            The code is working perfectly in my computer.
                            which version of vb ur using??

                            Comment

                            • kuzure
                              New Member
                              • Dec 2006
                              • 48

                              #15
                              Originally posted by sukeshchand
                              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 FileSystemObjec t As New FileSystemObject
                              --->why both name are same?

                              Thanks for the reply.

                              Best Regards.

                              Comment

                              Working...