Results 1 to 23 of 23

Thread: Removing files

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    277

    Removing files

    I have code for copying files to a folder using commondialog.

    would it be much different to implement a similar feature to allow users to remove files. Heres the code used for adding.

    VB Code:
    1. Private Sub Command2_Click()
    2.  
    3.  
    4.     Dim f1 As Integer, songpath As String, xmlpath As String, songname As String, mp3path As String, f2 As Integer, i As Integer
    5.    
    6.     xmlpath = "C:\Program Files\mgamerz\"
    7.  
    8.     mp3path = "C:\Program Files\mgamerz\my_mp3s\"
    9.    
    10.      f2 = FreeFile
    11.     Open mp3path & "mp3.txt" For Output As #f2       ' if you don't need mp3.txt take out
    12.      f1 = FreeFile
    13.      
    14.     Open xmlpath & "audiolist.xml" For Output As #f1
    15.    
    16.     Print #f1, "<?xml version=""1.0""?>"
    17.     Print #f1, "<songs>"
    18.     songname = Dir(mp3path & "\*.mp3")   ' get first track
    19.     i = 1
    20.     While Not Len(songname) = 0
    21.         songpath = "<song path=""" & mp3path & songname & """ title=""" & songname & """/> "
    22.         Print #f1, songpath
    23.         Print #f2, songname                  ' for mp3.txt
    24.         Sleep 100                                ' slight pause, needed
    25.         songname = Dir                         ' get next track
    26.    
    27.     Wend
    28.     Print #f1, "</songs>"
    29.  
    30.  
    31. Close
    32.  
    33.  
    34. End Sub

    Thanks Rob

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Removing files

    do you want to delete the song from the drive, or just delete the song from your list?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    277

    Re: Removing files

    well I would like the user to be able to click a command button (remove files) and then the folder where they are stored will open and they can select either one track or multiple - click ok and they will be gone.

    Cheers for replying
    Rob

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    277

    Re: Removing files

    I posted the wrong code this is what I'm using to open files

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim strFile() As String, strPath As String
    3.     Dim n As Long, nCount As Long
    4.     On Error Resume Next
    5.     With CommonDialog1
    6.         .Flags = cdlOFNAllowMultiselect + cdlOFNExplorer
    7.         .CancelError = True
    8.         .InitDir = "C:\"
    9.         .DialogTitle = "Select File"
    10.        
    11.         .ShowOpen
    12.         If Err.Number <> cdlCancel Then
    13.             strFile = Split(.FileName, vbNullChar)
    14.             nCount = UBound(strFile)
    15.             If nCount = 0 Then
    16.                 'Only one file is selected so split up the path and the filename
    17.                 ReDim strFile(1)
    18.                 strFile(0) = Left$(.FileName, InStrRev(.FileName, "\"))
    19.                 strFile(1) = Mid$(.FileName, InStrRev(.FileName, "\") + 1)
    20.                 nCount = 1
    21.             End If
    22.             strPath = strFile(0)
    23.             If Right$(strPath, 1) <> "\" Then
    24.                 strPath = strPath & "\"
    25.             End If
    26.             For n = 1 To nCount
    27.                 If Len(Dir(FAVORITES_FOLDER & strFile(n))) = 0 Then
    28.                     FileCopy strPath & strFile(n), FAVORITES_FOLDER & strFile(n)
    29.                 End If
    30.             Next
    31.         End If
    32.     End With
    33. End Sub

    Can I adjust this to work in reverse so to speak?

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Removing files

    where is it going to be deleted from, the list, the folder, or both?

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    277

    Re: Removing files

    I just want the user to be able to click a command button and then be taken to the my_mp3s folder where the mp3 files are stored. Then when they are there I want them to be able to either select a single file or select multiple files by holding shift etc and that will delete the file. I thought this might have been quite easy as I have it set up so they can add files.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    277

    Re: Removing files

    As for the list this will be adjusted via a command button that I have on the form that creates/updates an xml file by what is in the my_mp3s folder.

    Sorry if I didn't make myself clear before.

    Rob

  8. #8
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Removing files

    so the list will get rebuilt based on the files that are present. I am not sure about the common dialog. I'm sure that there is a multiselect to select more than one file, but not sure how to reference what is returned. I would load the filenames into a listbox, and then delete them that way, but there is probably a better way to use the common dialog.

    If you used the listbox, you could delete the ones that you don't want, which would move them into an array to delete. if you can get that same list from the cd, then it would be easy.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    277

    Re: Removing files

    I just thought that I would be able to reverse the code that adds the files if you know what I mean but I'm just a beginner lol

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Removing files

    try changing this:
    Code:
               For n = 1 To nCount
                    If Len(Dir(FAVORITES_FOLDER & strFile(n))) = 0 Then
                        FileCopy strPath & strFile(n), FAVORITES_FOLDER & strFile(n)
                    End If
                Next
    to this:

    Code:
              For n = 1 To nCount
                        Kill strPath & strFile(n)
                Next
    You would have to get the right directory.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    277

    Re: Removing files

    Hey - Brilliant that deleted the files. It allowed me to either delete a single file or delete multiple
    Great thanks

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    277

    Re: Removing files

    just one thing when I select the file the command button is labeled open - can I change this to delete?

  13. #13
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Removing files

    Sure. Just declare a flag in the general section, so that it retains its value while the program is running. Dim ActionFlag as Boolean
    Then set it to one or the other. ActionFlag=True ' Can be for OPEN

    Then in the click event for the button, if actionflag=true then 'do the open routine, else, do the delete routine.

    You'd also want to change the caption based on the flag value, so I'd use
    if ActionFlag=true then
    btnAction.Caption = "Open"
    else
    btnAction.Caption="Delete"
    endif

    You may want to put that in the form activate event or form load.

    You could have it automatically switch to delete when the folder is read.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    277

    Re: Removing files

    Not sure what you mean would I set the action flag to true in my button code for example would I put it in this code.

    VB Code:
    1. Private Sub Command5_Click()
    2. Dim strFile() As String, strPath As String
    3.     Dim n As Long, nCount As Long
    4.     On Error Resume Next
    5.     With CommonDialog1
    6.         .Flags = cdlOFNAllowMultiselect + cdlOFNExplorer
    7.         .CancelError = True
    8.         .InitDir = "C:\program files\mgamerz\my_mp3s"
    9.         .DialogTitle = "Select File To Delete"
    10.        
    11.         .ShowOpen
    12.         If Err.Number <> cdlCancel Then
    13.             strFile = Split(.FileName, vbNullChar)
    14.             nCount = UBound(strFile)
    15.             If nCount = 0 Then
    16.                 'Only one file is selected so split up the path and the filename
    17.                 ReDim strFile(1)
    18.                 strFile(0) = Left$(.FileName, InStrRev(.FileName, "\"))
    19.                 strFile(1) = Mid$(.FileName, InStrRev(.FileName, "\") + 1)
    20.                 nCount = 1
    21.             End If
    22.             strPath = strFile(0)
    23.             If Right$(strPath, 1) <> "\" Then
    24.                 strPath = strPath & "\"
    25.             End If
    26.             For n = 1 To nCount
    27.                     Kill strPath & strFile(n)
    28.            
    29.             Next
    30.         End If
    31.     End With
    32. End Sub

  15. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Removing files

    Code:
    Private Sub Command5_Click()
      If ActionFlag = "True" Then
        call OpenFiles
      Else
       call DeleteFiles
      endif
    and make one routine Open, and the other Delete. at the bottom of the Open, you could set the ActionFlag to False, and the caption, so that the button becomes "Delete."

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    277

    Re: Removing files

    Still a bit lost with this?? not sure what you mena by openFiles and delete files?

  17. #17
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Removing files

    I thought that you wanted to have the same button be Open and Delete.
    That's why you need the flag, to tell the program which routine to use.

    If all you want to do is change the button caption, use

    btnName.Caption= "Delete"

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    277

    Re: Removing files

    sorry what I meant when the window opens to choose the files to open the caption on the button next to the file name is labeled open and when I delete a file it is also set to open. I wondered if I could change this to delete. You know the one above cancel? Hope I've explained properly

  19. #19
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Removing files

    Oh, I know what you mean. You want to change the caption in the Common Dialog control? I don't think that is possible. You would have to call two different versions of the CD control. What I have done is create a CD control on a form by it self, and call it frmCommonDialog. Here is some of my code:

    Code:
      Dim oCDBForm As New frmCommonDialog
      x = lblSetup(1).Left + lblSetup(1).Width
      y = lblSetup(1).Top
      oCDBForm.Move x, y
      With oCDBForm.CommonDialog1
        .DialogTitle = "Select BackUp File To use"
        .InitDir = App.Path
        .Filter = "BackUp Files (*.old)|BU*.old|All " & _
            "Files (*.*)|*.*"
        .Flags = _
            cdlOFNFileMustExist + _
            cdlOFNHideReadOnly + _
            cdlOFNLongNames + _
            cdlOFNExplorer
        .CancelError = False  ' Change To trap cancel True
        .ShowOpen
        txtBackUp.Text = .FileName
      End With
      Unload oCDBForm
      Set oCDBForm = Nothing
    I can then display it wherever I want, and use any action that I want to use. I use the same one for Printer dialog. it just has different parameters.

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2005
    Posts
    277

    Re: Removing files

    I dont understnad how I would do that it sounds a bit complicated for me that lol

  21. #21
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Removing files

    Much better than using multiple forms that each have a CD control. Plus, I re-use the form in other projects.

  22. #22
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Removing files

    the commondialog control has its own caption
    VB Code:
    1. Cd1.DialogTitle = "test"
    2. Cd1.ShowOpen

    regards pete

  23. #23
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Removing files

    it s not easy to change the button caption in the common dialog, it may be possible to do it using sendmessage to the commondialog, but you can change the dialog caption as per my last post,

    as an extra precaution you could set
    VB Code:
    1. Cd1.FileName = "Select files for delete"
    which will change as soon as they select a file, but just gives an extra warning message

    p.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width