How to drag drop file/s into a textbox from winexplorer?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #46
    Q1 = If there is only 1 textbox with filenames (the original with index=0 ) then there are no copies: so no files dropped.
    Place this code at the beginning of the command code:

    Code:
    '§ check if Files to copy <> 0
       If TextFiles.Count = 1 Then
          MsgBox "There are no files to copy !" & vbNewLine & _
                   "Please, drop files into the Dropzone !"
          Exit Sub
       End If
    Q2 = I have also added the possibility if the user just not want to uverwrite this one file.
    The program continues with the next file.
    Code:
             '§ Make copies
             For FILEidx = 1 To Val(CopyTimes(STARTFILEidx).Text)
                FILETO = NAME & "(" & FILEidx & ")" & "." & EXTENTION
                If Dir(SaveTo.Text & "\" & FILETO) <> "" Then _
                   MESSAGE = MsgBox("The file " & FILETO & " exist !" & vbNewLine & _
                            "Replace ? [Yes]" & vbNewLine & _
                            "No for only this one [No]" & vbNewLine & _
                            "Cancel All [Cancel]", vbYesNoCancel)
                If MESSAGE = vbCancel Then Exit Sub
                If MESSAGE = vbNo Then
                   NMBRCOPIES = NMBRCOPIES - 1
                   GoTo NextFile
                End If
                FileCopy TextFiles(STARTFILEidx), SaveTo.Text & "\" & FILETO
                COPIESMADE = COPIESMADE + 1
                LabelProgress.Caption = "Progress : " & COPIESMADE & "/" & NMBRCOPIES & " copy-ed"
                ShapeProgress.Width = (COPIESMADE / NMBRCOPIES) * ShapeProgressBack.Width
                LOGTEXT = LOGTEXT & FILETO & vbNewLine '§ add filename for printing log
    NextFile:
             Next
       Next
    Q3 = In the copy code is also added a var LOGTEXT(string)
    which keep track of the copied files.
    Just dump this data in a textfile with:
    Code:
    Private Sub ComSaveLog_Click()
    Dim MESSAGE As Variant
    Dim OUTPUTFN As Integer
       If Dir(SaveTo.Text & "\Log.txt") <> "" Then _
          MESSAGE = MsgBox("The file Log.txt exist !" & vbNewLine & "Overwrite ?", vbOKCancel)
       If MESSAGE = vbCancel Then Exit Sub
       OUTPUTFN = FreeFile
       On Error GoTo ErrWriting
       Open SaveTo.Text & "\Log.txt" For Output As #OUTPUTFN
          Print #OUTPUTFN, LOGTEXT
       Close #OUTPUTFN
    Exit Sub
    ErrWriting:
       Close #OUTPUTFN
       MsgBox ("There is an error writing the settings file !")
    End Sub
    Attached Files

    Comment

    • blurryanthem
      New Member
      • Dec 2010
      • 43

      #47
      Sir Geur,

      I have added these codes to handle the ff error trapping..let me know if the codes are fine:

      a. '§ Opens Saved Log file (Replicate button )
      Code:
      Private Sub LogBtn_Click()
          Dim LogFile As String
          LogFile = "notepad c:\Log.txt"
          Shell LogFile
      End Sub
      b. If file exists ( replace Ok/Cancel)
      Code:
      '§ Loop through files to copy
         COPIESMADE = 0
         'LabelProgress.Caption = "Copy Progress"
         
      For STARTFILEidx = 1 To Text1.Count - 1
            '§ find FILENAME (NAME + EXTENTION)
            FILENAME = Mid(Text1(STARTFILEidx), InStrRev(Text1(STARTFILEidx), "\") + 1)
            '§ find NAME
            NAME = Left(FILENAME, InStrRev(FILENAME, ".") - 1)
            '§ find EXTENTION
            EXTENTION = Mid(FILENAME, InStrRev(FILENAME, ".") + 1)
               '§ Make copies
               For FILEidx = 1 To Val(CopyTimes(STARTFILEidx).Text)
                  FILETO = NAME & "(" & FILEidx & ")" & "." & EXTENTION
                  If Dir(SaveTo.Text & "\" & FILETO) <> "" Then
                     MESSAGE = MsgBox(FILETO & " already exist." & _
                           vbNewLine & "Replace existing file ?", vbOKCancel)
                     'LabelProgress.Caption = "Progress"
                     
                      If MESSAGE = vbOK Then
                      FileCopy Text1(STARTFILEidx), SaveTo.Text & "\" & FILETO
                      COPIESMADE = COPIESMADE + 1
                      LabelProgress.Caption = "Copy Progress:" & COPIESMADE & " / " & NMBRCOPIES & ""
                      ShapeProgress.Width = (COPIESMADE / NMBRCOPIES) * ShapeProgressBack.Width
                      LOGTEXT = LOGTEXT & FILETO & vbNewLine '§ add filename for printing log
                      Else
                      Exit Sub
                      End If
                  
                  End If
                      FileCopy Text1(STARTFILEidx), SaveTo.Text & "\" & FILETO
                      COPIESMADE = COPIESMADE + 1
                      LabelProgress.Caption = "Copy Progress:" & COPIESMADE & " / " & NMBRCOPIES & ""
                      ShapeProgress.Width = (COPIESMADE / NMBRCOPIES) * ShapeProgressBack.Width
                      LOGTEXT = LOGTEXT & FILETO & vbNewLine '§ add filename for printing log
                Next
                  LabelProgress.Caption = "Files successfully replicated."
                  ShapeProgress.Width = 0
                  
                  '§ print Log
                  sFilename = "c:\Log.txt"
         
                  'obtain the next free file handle from the
                  'system and and save the text box contents
                  hFile = FreeFile
                  Open sFilename For Output As #hFile
                  Print #hFile, LOGTEXT
                  Close #hFile
      c. No copy destination ( if SaveTo.Text has no value )
      Code:
       , if replicate button is pressed
      If SaveTo.Text = "" Then
            MsgBox ("Please specify a copy destination.")
      End If
      d. I don't have error handling yet to check if there are no chosen/dragged files when replicate button is pressed.

      Comment

      • Guido Geurs
        Recognized Expert Contributor
        • Oct 2009
        • 767

        #48
        Seems to be OK.
        You can define how the Notepad window must be opened by:

        Code:
        Private Sub ComShowLog_Click()
        Dim NOTEPADSHELL As Double
           NOTEPADSHELL = Shell("Notepad " & SaveTo.Text & "\Log.txt", vbNormalFocus)
        End Sub
        No need to proceed with the code if there is no destination folder (exit sub) so the code can be:

        Code:
        ....
        '§ check if Files to copy <> 0
           If TextFiles.Count = 1 Then '§ only the original textbox with index=0
              MsgBox "There are no files to copy !" & vbNewLine & _
                       "Please, drop files into the Dropzone !"
              Exit Sub
           End If
        ....
        Attached Files

        Comment

        • blurryanthem
          New Member
          • Dec 2010
          • 43

          #49
          a. I think I have to consider your suggestions with the conditions if a file already exist.

          "Replace ? [Yes]" & vbNewLine & _
          "No for only this one [No]" & vbNewLine & _
          "Cancel All [Cancel]", vbYesNoCancel)

          Thanks for the idea.

          b. I have already added the checking for "No file to replicate." and it worked already.

          Comment

          • blurryanthem
            New Member
            • Dec 2010
            • 43

            #50
            Hello Sir Geur,

            I modified your code a bit in the situation wherein no destination drive is specified, since program was still able to continue with the code instead of exiting the sub and copies the files at root of c:\

            Code:
            '§ check if Files to copy <> 0
                If TextFiles.Count = 1 Then '§ only the original textbox with index=0
                   MsgBox "There are no files to copy !" & vbNewLine & _
                            "Please, drop files into the Dropzone !"
                   Exit Sub
                End If
            Modified code. ( This already works, and exits the sub.no copy process is performed )

            Code:
            If Text1.Count = 1 Then
                   MsgBox "There are no files to copy." & vbNewLine & _
                            "Please, drop files into the drop zone."
                Else
                Exit Sub
            End If
            Thanks!

            Comment

            • blurryanthem
              New Member
              • Dec 2010
              • 43

              #51
              executable file

              Hello Sir Geur,

              I think I've made it work as to what I need with the tool. Please take a check with its functionality and see if there are still areas that are buggy or needs improvement.

              Thank you so much for your unselfish help. I was able to learn alot while studying your codes at the same time and was able to refresh my vb knowledge that has been stucked for 5 years now.

              Attached is the tool.

              Thanks alot!
              Emily
              Attached Files

              Comment

              • blurryanthem
                New Member
                • Dec 2010
                • 43

                #52
                I just found a bug wherein if a copy process is ongoing and a file exist halfway the process and then I press cancel... the copy process was stopped but the progress bar does not go back to zero.

                I tried to insert the lines below but program does not seem to read it..I don't know why.

                Code:
                         If MESSAGE = vbCancel Then Exit Sub
                          ShapeProgress.Width = 0
                          LabelProgress.Caption = "Copy Cancelled."
                         If MESSAGE = vbNo Then GoTo NextFile
                         FileCopy Text1(STARTFILEidx), SaveTo.Text & "\" & FILETO

                Comment

                • Guido Geurs
                  Recognized Expert Contributor
                  • Oct 2009
                  • 767

                  #53
                  Resetting the progressbar must be done before You Exit the Sub:

                  Code:
                           If MESSAGE = vbCancel Then
                              LabelProgress.Caption = "Copy Canceled."
                              ShapeProgress.Width = 0
                              Exit Sub
                           End If
                  When I click Log then Notepad opens but Minimized.
                  I have changed the code to :
                  Code:
                  Private Sub ComShowLog_Click()
                  Dim NOTEPADSHELL As Double
                     NOTEPADSHELL = Shell("Notepad " & SaveTo.Text & "\Log.txt", vbNormalFocus)
                  End Sub
                  In my code is the progressbar working (see attachment).
                  Is in Your code the indication Shape on top of the background of the progressbar?

                  I have also added in the log the type of copy: "replaced" or "New" and "Canceled" if so.

                  I don't understand Your previous mail in which You say: "the situation wherein no destination drive is specified, since ..."
                  and You modify the code for no files dropped = "If Text1.Count = 1 Then
                  MsgBox "There ..." ???

                  and I think there is an error in it because You "Exit it" when there are files dropped: (if Count = 1 => Msg
                  ELSE => Exit sub ...)

                  " If Text1.Count = 1 Then
                  MsgBox "There ...
                  Else
                  Exit Sub
                  End If

                  PS:
                  It was a pleasure helping You because I learn more by working and searching for solutions on an existing problem than reading 10 books of programming.
                  If You have still questions, please don't hesitate and place them in a call on Bytes.
                  Attached Files

                  Comment

                  • blurryanthem
                    New Member
                    • Dec 2010
                    • 43

                    #54
                    Sir Geur,

                    The progress bar is goes back to 0 when copy is canceled. So you just have to put the conditions before exiting sub. Tnx!

                    As for the code below..I am quite hesitant to use it as after I replicate files..and click log file button using your code below since it only opens an empty file..but does not write the copied files on the Log.text
                    Code:
                       1. Private Sub ComShowLog_Click()
                       2. Dim NOTEPADSHELL As Double
                       3.    NOTEPADSHELL = Shell("Notepad " & SaveTo.Text & "\Log.txt", vbNormalFocus)
                       4. End Sub
                    At the end of my replicate button code I have already indicated that a log file should be printed. I put this after files are successfully replicated. I will by default print and save the Log file at c:\Log.txt
                    Code:
                                '§ print Log
                                sFilename = "c:\Log.txt"
                       
                                'obtain the next free file handle from the
                                'system and and save the text box contents
                                hFile = FreeFile
                                Open sFilename For Output As #hFile
                                Print #hFile, LOGTEXT
                                Close #hFile
                    I would only need the filenames ( a new or replaced file does not matter ). Is there also a way to indicate the total number of files being copied? I assume by couting the lines in the saved file? How?

                    Comment

                    • Guido Geurs
                      Recognized Expert Contributor
                      • Oct 2009
                      • 767

                      #55
                      The total of different files to copy= TextFiles.Count-1 (-1 for the original textbox with index= 0)
                      The total of copyed files is already calculated for the progress bar= COPYTOTAL
                      In:
                      Code:
                         For COPIESidx = 1 To TextFiles.Count - 1
                            COPYTOTAL = COPYTOTAL + Val(CopyTimes(COPIESidx))
                         Next

                      Comment

                      • blurryanthem
                        New Member
                        • Dec 2010
                        • 43

                        #56
                        Hello Sir Geur,

                        It's just now that I have noticed that hung up. There seems to be a problem when copying large number of copies per chosen file. The tool seems to hung up and does not anymore show all the progress and then after a while immediately goes to 100%.

                        I'm unsure if this can handle thousand of copies?
                        Any help?

                        Thank You!

                        Comment

                        • blurryanthem
                          New Member
                          • Dec 2010
                          • 43

                          #57
                          Sir Geur,

                          I also want to understand your code below as when clicked, it seems to ask the user to create Log file. And then when confirmed, it creates an empty Log file. It doesn't write the filenames to the Log.txt. What does it really do? Just open and create an empty Log.txt?

                          Code:
                          Private Sub ComShowLog_Click()
                          Dim NOTEPADSHELL As Double
                          NOTEPADSHELL = Shell("Notepad " & SaveTo.Text & "\Log.txt", vbNormalFocus)
                          End Sub
                          I have indicated in the last lines of my code at Replicate button this:
                          Code:
                                      '§ print Log
                                      sFilename = "c:\Log.txt"
                             
                                      'obtain the next free file handle from the
                                      'system and and save the text box contents
                                      hFile = FreeFile
                                      Open sFilename For Output As #hFile
                                      Print #hFile, LOGTEXT
                                      Close #hFile

                          Comment

                          • Guido Geurs
                            Recognized Expert Contributor
                            • Oct 2009
                            • 767

                            #58
                            Q1 : maybe there are to much copies at the same time.
                            Is the problem solved if You enter the "Doevents" after the copy command like this:
                            Code:
                            .....
                                     If MESSAGE = vbNo Then GoTo NextFile
                                     FileCopy TextFiles(STARTFILEidx), SaveTo.Text & "\" & FILETO
                                     DoEvents
                                     '§ add filename for printing log
                                     If FILEEXIST Then
                            .....
                            Q2: My code opens notepad to a logfile I make in the destination folder (SaveTo.Text).

                            Code:
                            Private Sub ComShowLog_Click() 
                            Dim NOTEPADSHELL As Double 
                            NOTEPADSHELL = Shell("Notepad " & SaveTo.Text & "\Log.txt", vbNormalFocus) 
                            End Sub
                            If Your Logfile is = "c:\Log.txt " (there is no logfile created by Your code in the copy-to folder) then the code must be:
                            Code:
                            Private Sub ComShowLog_Click() 
                            Dim NOTEPADSHELL As Double 
                            NOTEPADSHELL = Shell("Notepad c:\Log.txt", vbNormalFocus) 
                            End Sub

                            Comment

                            • blurryanthem
                              New Member
                              • Dec 2010
                              • 43

                              #59
                              Hello Sir Geur,

                              I just noticed that a Run time error '75', Path/File access error is being thrown when I am trying to replicate/copy files into an empty/formatted external drive( ex. usb flash drive)

                              But when the same flash drive has at least a file in it, then replicate/copy files to the drive using the tool...no error is thrown and normal copy process is performed.

                              When the error "75" occured, it seemed to point at this line:

                              If Dir$(PATHpart, vbDirectory) = "" Then MkDir (PATHpart) '§ create folders

                              Any idea why?
                              Thanks
                              Emily

                              Comment

                              • Guido Geurs
                                Recognized Expert Contributor
                                • Oct 2009
                                • 767

                                #60
                                The previous code checks only on Folders.
                                If you want to copy to the root, the code "Dir$(PATHp art, vbDirectory)" gives an error because it's a root and not a vbDirectory !
                                If it's an existing root, go directly to the copy code.
                                I have changed it to check drive, root and folders (see attachment).
                                Attached Files

                                Comment

                                Working...