Sign in to follow this  
Followers 0
AcidHeat79

Copy files to removable disk

27 posts in this topic

Hi all,

I'm pretty new at this...

I wanna copy 2 main files...My Documents and desktop into a removable disk.

  • Copy Desktop
  • Copy My Document
  • Save as a main folder title as date of the day
  • in the main folder titled date of the dat contains 2 folders; desktop and My document

How do i write this?

DirCopy(@MyDocumentsDir, "S:\")

DirCopy(@DesktopDir, "S:\")

Not like this rite? Coz this will create 2 folders instead of a main folder. How do i do a variable?

And how can i get a progress bar for each transfer?

_FileCopy("C:\Users\user\Documents\*.*","S:\Backups\MyDocs")

Func _FileCopy($fromFile,$todirectory)
Local $FOF_RESPOND_YES = 8
Local $FOF_SIMPLEPROGRESS = 256
$winShell = ObjCreate("shell.application")
$winShell.namespace($todirectory).CopyHere($fromFile)
EndFunc

Share this post


Link to post
Share on other sites



And how can i get a progress bar for each transfer?

http://www.autoitscript.com/wiki/FAQ#How_can_I_display_a_progress_bar_while_copying_files_or_directories.3F

I wanna copy 2 main files...My Documents and desktop into a removable disk.

You mean folders right? Also, will the removable drive ever change its drive letter? Will you want to limit the number of time stamped folders autoit creates?


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

The date format will look like: 20110111005631 as in 2011, 1, 11, 00:56:31

While 1
    DirCreate("S:\Test1") ;Creates a dummy temp file for the purpose of getting the time of creation
    $timeStamp = FileGetTime("S:\Test1",1,1) ; Sets $timestamp to the time of folder creation above
    DirRemove("S:\Test1") ;deletes the temp folder
        DirCreate("S:\"&$timeStamp) ;Creates the folder in the time/date format mentioned above
    $DestinationPath = ("S:\"&$timeStamp) ;names $Destination Path for use in next function
    DirCreate($DestinationPath&"\My Documents") ;Creates the my documents directory in the time stamp folder
        DirCopy(@MyDocumentsDir, $DestinationPath&"\My Documents", 1 ;Copies the data to timestamp\my documents
    DirCreate(($DestinationPath&"\Desktop");creates the Desktop directory in the time stamp folder
        DirCopy(@DesktopDir, $DestinationPath&"\Desktop", 1) ;Copies the data to timestamp\desktop
Exit
Wend

Ill keep on working on this but I thought you might want to see my progress.


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

I am not sure if I should have written the whole program for you but I did. Here it is. I tested it on Windows 7 Professional 64 bit and although a little slow when detecting the number of files to transfer, it worked well.

While 1
    DirCreate("S:\Test1")
    $timeStamp = FileGetTime("S:\Test1",1,1)
    DirRemove("S:\Test1")
    DirCreate("S:\"&$timeStamp)
    $DestinationPath = ("S:\"&$timeStamp)
    DirCreate($DestinationPath&"\My Documents")
    DirCreate($DestinationPath&"\Desktop")
    $DPMyDocs = ($DestinationPath&"\My Documents")
    $DPDesktop = ($DestinationPath&"\Desktop")
    _CopyDirWithProgress(@DesktopDir, $DPDesktop)
    _CopyDirWithProgress(@MyDocumentsDir, $DPMyDocs)
Exit
wend

Func _CopyDirWithProgress($sOriginalDir, $sDestDir)
  ;$sOriginalDir and $sDestDir are quite selfexplanatory...
  ;This func returns:
  ; -1 in case of critical error, bad original or destination dir
  ;  0 if everything went all right
  ; >0 is the number of file not copied and it makes a log file
  ;  if in the log appear as error message '0 file copied' it is a bug of some windows' copy command that does not redirect output...

   If StringRight($sOriginalDir, 1) <> '\' Then $sOriginalDir = $sOriginalDir & '\'
   If StringRight($sDestDir, 1) <> '\' Then $sDestDir = $sDestDir & '\'
   If $sOriginalDir = $sDestDir Then Return -1

   ProgressOn('Copying...', 'Making list of files...' & @LF & @LF, '', -1, -1, 18)
   Local $aFileList = _FileSearch($sOriginalDir)
   If $aFileList[0] = 0 Then
      ProgressOff()
      SetError(1)
      Return -1
   EndIf

   If FileExists($sDestDir) Then
      If Not StringInStr(FileGetAttrib($sDestDir), 'd') Then
         ProgressOff()
         SetError(2)
         Return -1
      EndIf
   Else
      DirCreate($sDestDir)
      If Not FileExists($sDestDir) Then
         ProgressOff()
         SetError(2)
         Return -1
      EndIf
   EndIf

   Local $iDirSize, $iCopiedSize = 0, $fProgress = 0
   Local $c, $FileName, $iOutPut = 0, $sLost = '', $sError
   Local $Sl = StringLen($sOriginalDir)

   _Quick_Sort($aFileList, 1, $aFileList[0])

   $iDirSize = Int(DirGetSize($sOriginalDir) / 1024)

   ProgressSet(Int($fProgress * 100), $aFileList[$c], 'Coping file:')
   For $c = 1 To $aFileList[0]
      $FileName = StringTrimLeft($aFileList[$c], $Sl)
      ProgressSet(Int($fProgress * 100), $aFileList[$c] & ' -> '& $sDestDir & $FileName & @LF & 'Total KiB: ' & $iDirSize & @LF & 'Done KiB: ' & $iCopiedSize, 'Coping file:  ' & Round($fProgress * 100, 2) & ' %   ' & $c & '/' &$aFileList[0])

      If StringInStr(FileGetAttrib($aFileList[$c]), 'd') Then
         DirCreate($sDestDir & $FileName)
      Else
         If Not FileCopy($aFileList[$c], $sDestDir & $FileName, 1) Then
            If Not FileCopy($aFileList[$c], $sDestDir & $FileName, 1) Then;Tries a second time
               If RunWait(@ComSpec & ' /c copy /y "' & $aFileList[$c] & '" "' & $sDestDir & $FileName & '">' & @TempDir & '\o.tmp', '', @SW_HIDE)=1 Then;and a third time, but this time it takes the error message
                  $sError = FileReadLine(@TempDir & '\o.tmp',1)
                  $iOutPut = $iOutPut + 1
                  $sLost = $sLost & $aFileList[$c] & '  ' & $sError & @CRLF
               EndIf
               FileDelete(@TempDir & '\o.tmp')
            EndIf
         EndIf

         FileSetAttrib($sDestDir & $FileName, "+A-RSH");<- Comment this line if you do not want attribs reset.

         $iCopiedSize = $iCopiedSize + Int(FileGetSize($aFileList[$c]) / 1024)
         $fProgress = $iCopiedSize / $iDirSize
      EndIf
   Next

   ProgressOff()

   If $sLost <> '' Then;tries to write the log somewhere.
      If FileWrite($sDestDir & 'notcopied.txt',$sLost) = 0 Then
         If FileWrite($sOriginalDir & 'notcopied.txt',$sLost) = 0 Then
            FileWrite(@WorkingDir & '\notcopied.txt',$sLost)
         EndIf
      EndIf
   EndIf

   Return $iOutPut
EndFunc  ;==>_CopyDirWithProgress

Func _FileSearch($sIstr, $bSF = 1)
  ; $bSF = 1 means looking in subfolders
  ; $sSF = 0 means looking only in the current folder.
  ; An array is returned with the full path of all files found. The pos [0] keeps the number of elements.
   Local $sCriteria, $sBuffer, $iH, $iH2, $sCS, $sCF, $sCF2, $sCP, $sFP, $sOutPut = '', $aNull[1]
   $sCP = StringLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sCP = '' Then $sCP = @WorkingDir & '\'
   $sCriteria = StringTrimLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sCriteria = '' Then $sCriteria = '*.*'

  ;To begin we seek in the starting path.
   $sCS = FileFindFirstFile($sCP & $sCriteria)
   If $sCS <> - 1 Then
      Do
         $sCF = FileFindNextFile($sCS)
         If @error Then
            FileClose($sCS)
            ExitLoop
         EndIf
         If $sCF = '.' Or $sCF = '..' Then ContinueLoop
         $sOutPut = $sOutPut & $sCP & $sCF & @LF
      Until 0
   EndIf

  ;And after, if needed, in the rest of the folders.
   If $bSF = 1 Then
      $sBuffer = @CR & $sCP & '*' & @LF;The buffer is set for keeping the given path plus a *.
      Do
         $sCS = StringTrimLeft(StringLeft($sBuffer, StringInStr($sBuffer, @LF, 0, 1) - 1), 1);current search.
         $sCP = StringLeft($sCS, StringInStr($sCS, '\', 0, -1));Current search path.
         $iH = FileFindFirstFile($sCS)
         If $iH <> - 1 Then
            Do
               $sCF = FileFindNextFile($iH)
               If @error Then
                  FileClose($iH)
                  ExitLoop
               EndIf
               If $sCF = '.' Or $sCF = '..' Then ContinueLoop
               If StringInStr(FileGetAttrib($sCP & $sCF), 'd') Then
                  $sBuffer = @CR & $sCP & $sCF & '\*' & @LF & $sBuffer;Every folder found is added in the begin of buffer
                  $sFP = $sCP & $sCF & '\';                               for future searches
                  $iH2 = FileFindFirstFile($sFP & $sCriteria);         and checked with the criteria.
                  If $iH2 <> - 1 Then
                     Do
                        $sCF2 = FileFindNextFile($iH2)
                        If @error Then
                           FileClose($iH2)
                           ExitLoop
                        EndIf
                        If $sCF2 = '.' Or $sCF2 = '..' Then ContinueLoop
                        $sOutPut = $sOutPut & $sFP & $sCF2 & @LF;Found items are put in the Output.
                     Until 0
                  EndIf
               EndIf
            Until 0
         EndIf
         $sBuffer = StringReplace($sBuffer, @CR & $sCS & @LF, '')
      Until $sBuffer = ''
   EndIf

   If $sOutPut = '' Then
      $aNull[0] = 0
      Return $aNull
   Else
      Return StringSplit(StringTrimRight($sOutPut, 1), @LF)
   EndIf
EndFunc  ;==>_FileSearch

Func _Quick_Sort(ByRef $SortArray, $First, $Last);Larry's code
   Dim $Low, $High
   Dim $Temp, $List_Separator

   $Low = $First
   $High = $Last
   $List_Separator = StringLen($SortArray[ ($First + $Last) / 2])
   Do
      While (StringLen($SortArray[$Low]) < $List_Separator)
         $Low = $Low + 1
      WEnd
      While (StringLen($SortArray[$High]) > $List_Separator)
         $High = $High - 1
      WEnd
      If ($Low <= $High) Then
         $Temp = $SortArray[$Low]
         $SortArray[$Low] = $SortArray[$High]
         $SortArray[$High] = $Temp
         $Low = $Low + 1
         $High = $High - 1
      EndIf
   Until $Low > $High
   If ($First < $High) Then _Quick_Sort($SortArray, $First, $High)
   If ($Low < $Last) Then _Quick_Sort($SortArray, $Low, $Last)
EndFunc  ;==>_Quick_Sort

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

I am not sure if I should have written the whole program for you but I did. Here it is. I tested it on Windows 7 Professional 64 bit and although a little slow when detecting the number of files to transfer, it worked well.

While 1
    DirCreate("S:\Test1")
    $timeStamp = FileGetTime("S:\Test1",1,1)
    DirRemove("S:\Test1")
    DirCreate("S:\"&$timeStamp)
    $DestinationPath = ("S:\"&$timeStamp)
    DirCreate($DestinationPath&"\My Documents")
    DirCreate($DestinationPath&"\Desktop")
    $DPMyDocs = ($DestinationPath&"\My Documents")
    $DPDesktop = ($DestinationPath&"\Desktop")
    _CopyDirWithProgress(@DesktopDir, $DPDesktop)
    _CopyDirWithProgress(@MyDocumentsDir, $DPMyDocs)
Exit
wend

Func _CopyDirWithProgress($sOriginalDir, $sDestDir)
  ;$sOriginalDir and $sDestDir are quite selfexplanatory...
  ;This func returns:
  ; -1 in case of critical error, bad original or destination dir
  ;  0 if everything went all right
  ; >0 is the number of file not copied and it makes a log file
  ;  if in the log appear as error message '0 file copied' it is a bug of some windows' copy command that does not redirect output...

   If StringRight($sOriginalDir, 1) <> '\' Then $sOriginalDir = $sOriginalDir & '\'
   If StringRight($sDestDir, 1) <> '\' Then $sDestDir = $sDestDir & '\'
   If $sOriginalDir = $sDestDir Then Return -1

   ProgressOn('Copying...', 'Making list of files...' & @LF & @LF, '', -1, -1, 18)
   Local $aFileList = _FileSearch($sOriginalDir)
   If $aFileList[0] = 0 Then
      ProgressOff()
      SetError(1)
      Return -1
   EndIf

   If FileExists($sDestDir) Then
      If Not StringInStr(FileGetAttrib($sDestDir), 'd') Then
         ProgressOff()
         SetError(2)
         Return -1
      EndIf
   Else
      DirCreate($sDestDir)
      If Not FileExists($sDestDir) Then
         ProgressOff()
         SetError(2)
         Return -1
      EndIf
   EndIf

   Local $iDirSize, $iCopiedSize = 0, $fProgress = 0
   Local $c, $FileName, $iOutPut = 0, $sLost = '', $sError
   Local $Sl = StringLen($sOriginalDir)

   _Quick_Sort($aFileList, 1, $aFileList[0])

   $iDirSize = Int(DirGetSize($sOriginalDir) / 1024)

   ProgressSet(Int($fProgress * 100), $aFileList[$c], 'Coping file:')
   For $c = 1 To $aFileList[0]
      $FileName = StringTrimLeft($aFileList[$c], $Sl)
      ProgressSet(Int($fProgress * 100), $aFileList[$c] & ' -> '& $sDestDir & $FileName & @LF & 'Total KiB: ' & $iDirSize & @LF & 'Done KiB: ' & $iCopiedSize, 'Coping file:  ' & Round($fProgress * 100, 2) & ' %   ' & $c & '/' &$aFileList[0])

      If StringInStr(FileGetAttrib($aFileList[$c]), 'd') Then
         DirCreate($sDestDir & $FileName)
      Else
         If Not FileCopy($aFileList[$c], $sDestDir & $FileName, 1) Then
            If Not FileCopy($aFileList[$c], $sDestDir & $FileName, 1) Then;Tries a second time
               If RunWait(@ComSpec & ' /c copy /y "' & $aFileList[$c] & '" "' & $sDestDir & $FileName & '">' & @TempDir & '\o.tmp', '', @SW_HIDE)=1 Then;and a third time, but this time it takes the error message
                  $sError = FileReadLine(@TempDir & '\o.tmp',1)
                  $iOutPut = $iOutPut + 1
                  $sLost = $sLost & $aFileList[$c] & '  ' & $sError & @CRLF
               EndIf
               FileDelete(@TempDir & '\o.tmp')
            EndIf
         EndIf

         FileSetAttrib($sDestDir & $FileName, "+A-RSH");<- Comment this line if you do not want attribs reset.

         $iCopiedSize = $iCopiedSize + Int(FileGetSize($aFileList[$c]) / 1024)
         $fProgress = $iCopiedSize / $iDirSize
      EndIf
   Next

   ProgressOff()

   If $sLost <> '' Then;tries to write the log somewhere.
      If FileWrite($sDestDir & 'notcopied.txt',$sLost) = 0 Then
         If FileWrite($sOriginalDir & 'notcopied.txt',$sLost) = 0 Then
            FileWrite(@WorkingDir & '\notcopied.txt',$sLost)
         EndIf
      EndIf
   EndIf

   Return $iOutPut
EndFunc  ;==>_CopyDirWithProgress

Func _FileSearch($sIstr, $bSF = 1)
  ; $bSF = 1 means looking in subfolders
  ; $sSF = 0 means looking only in the current folder.
  ; An array is returned with the full path of all files found. The pos [0] keeps the number of elements.
   Local $sCriteria, $sBuffer, $iH, $iH2, $sCS, $sCF, $sCF2, $sCP, $sFP, $sOutPut = '', $aNull[1]
   $sCP = StringLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sCP = '' Then $sCP = @WorkingDir & '\'
   $sCriteria = StringTrimLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sCriteria = '' Then $sCriteria = '*.*'

  ;To begin we seek in the starting path.
   $sCS = FileFindFirstFile($sCP & $sCriteria)
   If $sCS <> - 1 Then
      Do
         $sCF = FileFindNextFile($sCS)
         If @error Then
            FileClose($sCS)
            ExitLoop
         EndIf
         If $sCF = '.' Or $sCF = '..' Then ContinueLoop
         $sOutPut = $sOutPut & $sCP & $sCF & @LF
      Until 0
   EndIf

  ;And after, if needed, in the rest of the folders.
   If $bSF = 1 Then
      $sBuffer = @CR & $sCP & '*' & @LF;The buffer is set for keeping the given path plus a *.
      Do
         $sCS = StringTrimLeft(StringLeft($sBuffer, StringInStr($sBuffer, @LF, 0, 1) - 1), 1);current search.
         $sCP = StringLeft($sCS, StringInStr($sCS, '\', 0, -1));Current search path.
         $iH = FileFindFirstFile($sCS)
         If $iH <> - 1 Then
            Do
               $sCF = FileFindNextFile($iH)
               If @error Then
                  FileClose($iH)
                  ExitLoop
               EndIf
               If $sCF = '.' Or $sCF = '..' Then ContinueLoop
               If StringInStr(FileGetAttrib($sCP & $sCF), 'd') Then
                  $sBuffer = @CR & $sCP & $sCF & '\*' & @LF & $sBuffer;Every folder found is added in the begin of buffer
                  $sFP = $sCP & $sCF & '\';                               for future searches
                  $iH2 = FileFindFirstFile($sFP & $sCriteria);         and checked with the criteria.
                  If $iH2 <> - 1 Then
                     Do
                        $sCF2 = FileFindNextFile($iH2)
                        If @error Then
                           FileClose($iH2)
                           ExitLoop
                        EndIf
                        If $sCF2 = '.' Or $sCF2 = '..' Then ContinueLoop
                        $sOutPut = $sOutPut & $sFP & $sCF2 & @LF;Found items are put in the Output.
                     Until 0
                  EndIf
               EndIf
            Until 0
         EndIf
         $sBuffer = StringReplace($sBuffer, @CR & $sCS & @LF, '')
      Until $sBuffer = ''
   EndIf

   If $sOutPut = '' Then
      $aNull[0] = 0
      Return $aNull
   Else
      Return StringSplit(StringTrimRight($sOutPut, 1), @LF)
   EndIf
EndFunc  ;==>_FileSearch

Func _Quick_Sort(ByRef $SortArray, $First, $Last);Larry's code
   Dim $Low, $High
   Dim $Temp, $List_Separator

   $Low = $First
   $High = $Last
   $List_Separator = StringLen($SortArray[ ($First + $Last) / 2])
   Do
      While (StringLen($SortArray[$Low]) < $List_Separator)
         $Low = $Low + 1
      WEnd
      While (StringLen($SortArray[$High]) > $List_Separator)
         $High = $High - 1
      WEnd
      If ($Low <= $High) Then
         $Temp = $SortArray[$Low]
         $SortArray[$Low] = $SortArray[$High]
         $SortArray[$High] = $Temp
         $Low = $Low + 1
         $High = $High - 1
      EndIf
   Until $Low > $High
   If ($First < $High) Then _Quick_Sort($SortArray, $First, $High)
   If ($Low < $Last) Then _Quick_Sort($SortArray, $Low, $Last)
EndFunc  ;==>_Quick_Sort

OMG!!!So i just copy and paste this and change the directory to be copied? Or just leave it!?

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

computergroove,

very nice, or you can,

1 - enumerate the drives colleciton (drivegetdrive)

2 - check for your "labeled" removeable drive (drivegetlabel)

3 - generate a timestamped directory

4 - use dircopy to copy files

I do this with ALL of my source.

just my 1.5 cents

kylomas

NOTE : belay my last, don't have progress bars

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

computergroove,

very nice, or you can,

1 - enumerate the drives colleciton (drivegetdrive)

2 - check for your "labeled" removeable drive (drivegetlabel)

3 - generate a timestamped directory

4 - use dircopy to copy files

I do this with ALL of my source.

just my 1.5 cents

kylomas

NOTE : belay my last, don't have progress bars

Kylomas and computergroove,

its a good idea where all removable drive can be detected and the script doesnt have to be customise to different users.

How do i do that?

Share this post


Link to post
Share on other sites

To save customising the script for multiple users, you could use an ini file.

Look at the various ini commands in the helpfile.

They're located at the end of the File, Directory and Disk functions Reference section.

William

Share this post


Link to post
Share on other sites

yukinaga,

My origional thought was to label some removable media (disc or flash) and check for a "ready" removeable drive with this media, however, I think saywell's solution is better and adds the flexibility to detect wrong media mounted and prompt the user to mount the required media.

The following are all doc'ed in the help file:

drivegetdrive

drivegetlabel

ini doc

** note **

There is a caution that all drive "types" are not enumerated. I have been using this technique as a kind of source control/backup for about 1 year with NO problems.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

...

its a good idea where all removable drive can be detected and the script doesnt have to be customise to different users.

How do i do that?

This _SelectRemovableDrive() function was tested with none, one, and two USB removable drives connected, and appears to work correctly.

Local $RHD = _SelectRemovableDrive()
If @error = 0 Then MsgBox(4096, "Drive Selected:", "Selected removable drive is " & $RHD)

Func _SelectRemovableDrive()
    Local $var = DriveGetDrive("REMOVABLE")
    If @error Then Return SetError(1, 0, 0)
    If $var[0] = 1 Then Return SetError(0, 0, $var[1])
    If $var[0] > 1 Then
        Local $sText = "Select Drive letter of the required removable drive." & @CRLF & @CRLF
        For $i = 1 To $var[0]
            $sText &= "Enter " & StringLeft($var[$i], 1) & " for " & $var[$i] & " Removable Drive - " & @TAB & "Size: " & _
                    Int(DriveSpaceTotal($var[$i])) & " MB Label: " & DriveGetLabel($var[$i]) & @CRLF
        Next
        Do
            Local $sHD = InputBox("Select Removable Drive", $sText, StringLeft($var[1], 1), "", 400)
            If @error Then Return SetError(1, 0, 0)
            For $i = 1 To $var[0]
                If StringUpper(StringLeft($var[$i], 1)) == StringUpper(StringRegExpReplace($sHD, "(?i)[^a-z]", "")) Then Return SetError(0, 0, $var[$i])
            Next
        Until 0 ; loop InputBox() until existing removable drive letter is entered or Cancel button is pressed.
    EndIf
EndFunc   ;==>_SelectRemovableDrive

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Which part of the script should be inputed? or deleted.

I'm lost.

Is it the first part? The While 1...but thats the copy instructions...

While 1
    DirCreate("S:\Test1")
    $timeStamp = FileGetTime("S:\Test1",1,1)
    DirRemove("S:\Test1")
    DirCreate("S:\"&$timeStamp)
    $DestinationPath = ("S:\"&$timeStamp)
    DirCreate($DestinationPath&"\My Documents")
    DirCreate($DestinationPath&"\Desktop")
    $DPMyDocs = ($DestinationPath&"\My Documents")
    $DPDesktop = ($DestinationPath&"\Desktop")
    _CopyDirWithProgress(@DesktopDir, $DPDesktop)
    _CopyDirWithProgress(@MyDocumentsDir, $DPMyDocs)
Exit
wend
Edited by Yukinaga

Share this post


Link to post
Share on other sites

While 1 isn't for copying. It means that While True or in other words run this program until the program ends. I tried this and it was doing something however I dont have a removable drive and that was the only choice I was given by the new code. Try this exactly and when you get a popup box, select the correct removable drive letter.

;Copies the Desktop and My Documents Folders to a timestamped folder on the E drive and gives a progress bar of the files copying

Local $RHD = _SelectRemovableDrive()
If @error = 0 Then MsgBox(4096, "Drive Selected:", "Selected removable drive is " & $RHD)

While 1
    DirCreate($RHD&"Test1") ;Creates a dummy temp file for the purpose of getting the time of creation
    $timeStamp = FileGetTime($RHD&"Test1",1,1) ; Sets $timestamp to the time of folder creation above
    DirRemove($RHD&"Test1") ;deletes the temp folder
    DirCreate($RHD&$timeStamp) ;Creates the folder in the time/date format mentioned above
    $DestinationPath = ($RHD&$timeStamp) ;names $Destination Path for use in next function
    DirCreate($DestinationPath&"\My Documents") ;Creates the my documents directory in the time stamp folder
    DirCreate($DestinationPath&"\Desktop") ;Creates the Desktop directory in the time stamp folder
    $DPMyDocs = ($DestinationPath&"\My Documents") ;Creates a clean directory path for the CopyDirWithProgress Function below
    $DPDesktop = ($DestinationPath&"\Desktop") ;Creates a clean directory path for the CopyDirWithProgress Function below
    _CopyDirWithProgress(@DesktopDir, $DPDesktop) ;Copies the data to timestamp\Desktop
    _CopyDirWithProgress(@MyDocumentsDir, $DPMyDocs) ;Copies the data to timestamp\my documents
Exit
wend

Func _SelectRemovableDrive()
    Local $var = DriveGetDrive("REMOVABLE")
    If @error Then Return SetError(1, 0, 0)
    If $var[0] = 1 Then Return SetError(0, 0, $var[1])
    If $var[0] > 1 Then
        Local $sText = "Select Drive letter of the required removable drive." & @CRLF & @CRLF
        For $i = 1 To $var[0]
            $sText &= "Enter " & StringLeft($var[$i], 1) & " for " & $var[$i] & " Removable Drive - " & @TAB & "Size: " & _
                    Int(DriveSpaceTotal($var[$i])) & " MB Label: " & DriveGetLabel($var[$i]) & @CRLF
        Next
        Do
            Local $sHD = InputBox("Select Removable Drive", $sText, StringLeft($var[1], 1), "", 400)
            If @error Then Return SetError(1, 0, 0)
            For $i = 1 To $var[0]
                If StringUpper(StringLeft($var[$i], 1)) == StringUpper(StringRegExpReplace($sHD, "(?i)[^a-z]", "")) Then Return SetError(0, 0, $var[$i])
            Next
        Until 0 ; loop InputBox() until existing removable drive letter is entered or Cancel button is pressed.
    EndIf
EndFunc   ;==>_SelectRemovableDrive

Func _CopyDirWithProgress($sOriginalDir, $sDestDir)
  ;$sOriginalDir and $sDestDir are quite selfexplanatory...
  ;This func returns:
  ; -1 in case of critical error, bad original or destination dir
  ;  0 if everything went all right
  ; >0 is the number of file not copied and it makes a log file
  ;  if in the log appear as error message '0 file copied' it is a bug of some windows' copy command that does not redirect output...

   If StringRight($sOriginalDir, 1) <> '\' Then $sOriginalDir = $sOriginalDir & '\'
   If StringRight($sDestDir, 1) <> '\' Then $sDestDir = $sDestDir & '\'
   If $sOriginalDir = $sDestDir Then Return -1

   ProgressOn('Copying...', 'Making list of files...' & @LF & @LF, '', -1, -1, 18)
   Local $aFileList = _FileSearch($sOriginalDir)
   If $aFileList[0] = 0 Then
      ProgressOff()
      SetError(1)
      Return -1
   EndIf

   If FileExists($sDestDir) Then
      If Not StringInStr(FileGetAttrib($sDestDir), 'd') Then
         ProgressOff()
         SetError(2)
         Return -1
      EndIf
   Else
      DirCreate($sDestDir)
      If Not FileExists($sDestDir) Then
         ProgressOff()
         SetError(2)
         Return -1
      EndIf
   EndIf

   Local $iDirSize, $iCopiedSize = 0, $fProgress = 0
   Local $c, $FileName, $iOutPut = 0, $sLost = '', $sError
   Local $Sl = StringLen($sOriginalDir)

   _Quick_Sort($aFileList, 1, $aFileList[0])

   $iDirSize = Int(DirGetSize($sOriginalDir) / 1024)

   ProgressSet(Int($fProgress * 100), $aFileList[$c], 'Coping file:')
   For $c = 1 To $aFileList[0]
      $FileName = StringTrimLeft($aFileList[$c], $Sl)
      ProgressSet(Int($fProgress * 100), $aFileList[$c] & ' -> '& $sDestDir & $FileName & @LF & 'Total KiB: ' & $iDirSize & @LF & 'Done KiB: ' & $iCopiedSize, 'Coping file:  ' & Round($fProgress * 100, 2) & ' %   ' & $c & '/' &$aFileList[0])

      If StringInStr(FileGetAttrib($aFileList[$c]), 'd') Then
         DirCreate($sDestDir & $FileName)
      Else
         If Not FileCopy($aFileList[$c], $sDestDir & $FileName, 1) Then
            If Not FileCopy($aFileList[$c], $sDestDir & $FileName, 1) Then;Tries a second time
               If RunWait(@ComSpec & ' /c copy /y "' & $aFileList[$c] & '" "' & $sDestDir & $FileName & '">' & @TempDir & '\o.tmp', '', @SW_HIDE)=1 Then;and a third time, but this time it takes the error message
                  $sError = FileReadLine(@TempDir & '\o.tmp',1)
                  $iOutPut = $iOutPut + 1
                  $sLost = $sLost & $aFileList[$c] & '  ' & $sError & @CRLF
               EndIf
               FileDelete(@TempDir & '\o.tmp')
            EndIf
         EndIf

         FileSetAttrib($sDestDir & $FileName, "+A-RSH");<- Comment this line if you do not want attribs reset.

         $iCopiedSize = $iCopiedSize + Int(FileGetSize($aFileList[$c]) / 1024)
         $fProgress = $iCopiedSize / $iDirSize
      EndIf
   Next

   ProgressOff()

   If $sLost <> '' Then;tries to write the log somewhere.
      If FileWrite($sDestDir & 'notcopied.txt',$sLost) = 0 Then
         If FileWrite($sOriginalDir & 'notcopied.txt',$sLost) = 0 Then
            FileWrite(@WorkingDir & '\notcopied.txt',$sLost)
         EndIf
      EndIf
   EndIf

   Return $iOutPut
EndFunc  ;==>_CopyDirWithProgress

Func _FileSearch($sIstr, $bSF = 1)
  ; $bSF = 1 means looking in subfolders
  ; $sSF = 0 means looking only in the current folder.
  ; An array is returned with the full path of all files found. The pos [0] keeps the number of elements.
   Local $sCriteria, $sBuffer, $iH, $iH2, $sCS, $sCF, $sCF2, $sCP, $sFP, $sOutPut = '', $aNull[1]
   $sCP = StringLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sCP = '' Then $sCP = @WorkingDir & '\'
   $sCriteria = StringTrimLeft($sIstr, StringInStr($sIstr, '\', 0, -1))
   If $sCriteria = '' Then $sCriteria = '*.*'

  ;To begin we seek in the starting path.
   $sCS = FileFindFirstFile($sCP & $sCriteria)
   If $sCS <> - 1 Then
      Do
         $sCF = FileFindNextFile($sCS)
         If @error Then
            FileClose($sCS)
            ExitLoop
         EndIf
         If $sCF = '.' Or $sCF = '..' Then ContinueLoop
         $sOutPut = $sOutPut & $sCP & $sCF & @LF
      Until 0
   EndIf

  ;And after, if needed, in the rest of the folders.
   If $bSF = 1 Then
      $sBuffer = @CR & $sCP & '*' & @LF;The buffer is set for keeping the given path plus a *.
      Do
         $sCS = StringTrimLeft(StringLeft($sBuffer, StringInStr($sBuffer, @LF, 0, 1) - 1), 1);current search.
         $sCP = StringLeft($sCS, StringInStr($sCS, '\', 0, -1));Current search path.
         $iH = FileFindFirstFile($sCS)
         If $iH <> - 1 Then
            Do
               $sCF = FileFindNextFile($iH)
               If @error Then
                  FileClose($iH)
                  ExitLoop
               EndIf
               If $sCF = '.' Or $sCF = '..' Then ContinueLoop
               If StringInStr(FileGetAttrib($sCP & $sCF), 'd') Then
                  $sBuffer = @CR & $sCP & $sCF & '\*' & @LF & $sBuffer;Every folder found is added in the begin of buffer
                  $sFP = $sCP & $sCF & '\';                               for future searches
                  $iH2 = FileFindFirstFile($sFP & $sCriteria);         and checked with the criteria.
                  If $iH2 <> - 1 Then
                     Do
                        $sCF2 = FileFindNextFile($iH2)
                        If @error Then
                           FileClose($iH2)
                           ExitLoop
                        EndIf
                        If $sCF2 = '.' Or $sCF2 = '..' Then ContinueLoop
                        $sOutPut = $sOutPut & $sFP & $sCF2 & @LF;Found items are put in the Output.
                     Until 0
                  EndIf
               EndIf
            Until 0
         EndIf
         $sBuffer = StringReplace($sBuffer, @CR & $sCS & @LF, '')
      Until $sBuffer = ''
   EndIf

   If $sOutPut = '' Then
      $aNull[0] = 0
      Return $aNull
   Else
      Return StringSplit(StringTrimRight($sOutPut, 1), @LF)
   EndIf
EndFunc  ;==>_FileSearch

Func _Quick_Sort(ByRef $SortArray, $First, $Last);Larry's code
   Dim $Low, $High
   Dim $Temp, $List_Separator

   $Low = $First
   $High = $Last
   $List_Separator = StringLen($SortArray[ ($First + $Last) / 2])
   Do
      While (StringLen($SortArray[$Low]) < $List_Separator)
         $Low = $Low + 1
      WEnd
      While (StringLen($SortArray[$High]) > $List_Separator)
         $High = $High - 1
      WEnd
      If ($Low <= $High) Then
         $Temp = $SortArray[$Low]
         $SortArray[$Low] = $SortArray[$High]
         $SortArray[$High] = $Temp
         $Low = $Low + 1
         $High = $High - 1
      EndIf
   Until $Low > $High
   If ($First < $High) Then _Quick_Sort($SortArray, $First, $High)
   If ($Low < $Last) Then _Quick_Sort($SortArray, $Low, $Last)
EndFunc  ;==>_Quick_Sort

Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

okay, i tried every where to put a msgbox for a 'completion' msg...but i cant get it...

Share this post


Link to post
Share on other sites

You want to add a MsgBox that says copy complete after the file copying is done?

While 1
    DirCreate($RHD&"Test1") ;Creates a dummy temp file for the purpose of getting the time of creation
    $timeStamp = FileGetTime($RHD&"Test1",1,1) ; Sets $timestamp to the time of folder creation above
    DirRemove($RHD&"Test1") ;deletes the temp folder
    DirCreate($RHD&$timeStamp) ;Creates the folder in the time/date format mentioned above
    $DestinationPath = ($RHD&$timeStamp) ;names $Destination Path for use in next function
    DirCreate($DestinationPath&"\My Documents") ;Creates the my documents directory in the time stamp folder
    DirCreate($DestinationPath&"\Desktop") ;Creates the Desktop directory in the time stamp folder
    $DPMyDocs = ($DestinationPath&"\My Documents") ;Creates a clean directory path for the CopyDirWithProgress Function below
    $DPDesktop = ($DestinationPath&"\Desktop") ;Creates a clean directory path for the CopyDirWithProgress Function below
    _CopyDirWithProgress(@DesktopDir, $DPDesktop) ;Copies the data to timestamp\Desktop
    _CopyDirWithProgress(@MyDocumentsDir, $DPMyDocs) ;Copies the data to timestamp\my documents
Exit
wend

The above is the whole program really. The other is from 1 other user that developed the copy progress function (mad props to this guy/girl). What you would need to do is add a line before Exit to the above code:

MsgBox(0,"File Copy Progress", "The files completed copying to"&$DestinationPath)

The above should work. Please try it. Also Note that this line wont do any kind of confirmation check. It will only leave a message box on the screen until you hit ok.


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

You want to add a MsgBox that says copy complete after the file copying is done?

While 1
    DirCreate($RHD&"Test1") ;Creates a dummy temp file for the purpose of getting the time of creation
    $timeStamp = FileGetTime($RHD&"Test1",1,1) ; Sets $timestamp to the time of folder creation above
    DirRemove($RHD&"Test1") ;deletes the temp folder
    DirCreate($RHD&$timeStamp) ;Creates the folder in the time/date format mentioned above
    $DestinationPath = ($RHD&$timeStamp) ;names $Destination Path for use in next function
    DirCreate($DestinationPath&"\My Documents") ;Creates the my documents directory in the time stamp folder
    DirCreate($DestinationPath&"\Desktop") ;Creates the Desktop directory in the time stamp folder
    $DPMyDocs = ($DestinationPath&"\My Documents") ;Creates a clean directory path for the CopyDirWithProgress Function below
    $DPDesktop = ($DestinationPath&"\Desktop") ;Creates a clean directory path for the CopyDirWithProgress Function below
    _CopyDirWithProgress(@DesktopDir, $DPDesktop) ;Copies the data to timestamp\Desktop
    _CopyDirWithProgress(@MyDocumentsDir, $DPMyDocs) ;Copies the data to timestamp\my documents
Exit
wend

The above is the whole program really. The other is from 1 other user that developed the copy progress function (mad props to this guy/girl). What you would need to do is add a line before Exit to the above code:

MsgBox(0,"File Copy Progress", "The files completed copying to"&$DestinationPath)

The above should work. Please try it. Also Note that this line wont do any kind of confirmation check. It will only leave a message box on the screen until you hit ok.

Yup, tried that and saw it before you replied. So the top is like the summary of the whole thing. And to add anything it must come before the EXIT.

Share this post


Link to post
Share on other sites

Basically yes. You can add code to the bottom or middle or where ever you want. The important thing is to understand what all of it does. Was the message box what you were hoping for? Are you happy with the end result?


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

Basically yes. You can add code to the bottom or middle or where ever you want. The important thing is to understand what all of it does. Was the message box what you were hoping for? Are you happy with the end result?

OMG! You really made my day. Aside from making this work, i learn many things from zero. Thanks man. IF i'm around your town i'll pay a gloryhole for you...

thanks alot man

Share this post


Link to post
Share on other sites

OMG! You really made my day. Aside from making this work, i learn many things from zero. Thanks man. IF i'm around your town i'll pay a gloryhole for you...

thanks alot man

WTF??????

Share this post


Link to post
Share on other sites

WTF??????

Hahahahahaha...i was kidding. The happiness is beyond description.

Share this post


Link to post
Share on other sites

Hahahahahaha...i was kidding. The happiness is beyond description.

Thank you for not describing more :x

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0