Jump to content

[UDF] Dir copy with progress


SumTingWong
 Share

Recommended Posts

This code is based off Larry's brilliant _FileSearch UDF. Hope you don't mind Larry :ph34r: .

It's slow so should only be used on fairly small directory structures.

AutoItSetOption("MustDeclareVars", 1)

Dim $n

ProgressOn("Dir Copy With Progress", "", "Scanning . . .")
$n = _DirCopyWithProgress("D:\Test1\*.au3", "D:\Test2", 1)
Sleep(5000)
ProgressOff()

Func _DirCopyWithProgress($sFileSpec, $sDestDir, $nOverwrite)
   Local $sSourceDir
   Local $nSourceLen
   Local $sRootDir   
   Local $hSearch
   Local $sFile   
   Local $sPathList = "*"
   Local $sFileList
   Local $aCopyList
   Local $nTotalSize
   Local $nSize
   Local $i
   
   $i = StringInStr($sFileSpec, "\", 0, -1)
   $sSourceDir = StringLeft($sFileSpec, $i)
   $nSourceLen = StringLen($sSourceDir)
   
   $sFileSpec = StringRight($sFileSpec, StringLen($sFileSpec)-$i)
   
   If Not FileExists($sDestDir) Then      
      If Not DirCreate($sDestDir) Then Return 0
   EndIf
   If StringRight($sDestDir, 1) <> "\" Then $sDestDir = $sDestDir & "\"
   
   $sRootDir = $sSourceDir
   While 1
      $hSearch = FileFindFirstFile($sRootDir & "*.*")
      If $hSearch > -1 Then
         $sFile = FileFindNextFile($hSearch)
         While Not @error
            If $sFile <> "." And $sFile <> ".." Then
               If StringInStr(FileGetAttrib($sRootDir & $sFile), "D") Then
                  $sPathList = $sPathList & $sRootDir & $sFile & "*"                  
               Else
                  If _MatchFilePattern($sFile, $sFileSpec) Then
                     $sFileList = $sFileList & StringTrimLeft($sRootDir, $nSourceLen) & "*" & $sFile & "*"
                     $i = FileGetSize($sRootDir & $sFile)
                     $nTotalSize = $nTotalSize + $i
                     $sFileList = $sFileList & $i & "*"
                  EndIf
               EndIf
            EndIf
            $sFile = FileFindNextFile($hSearch)
         Wend
         FileClose($hSearch)
      EndIf
      If $sPathList = "*" Then ExitLoop
      $sPathList = StringTrimLeft($sPathList, 1)
      $sRootDir = StringLeft($sPathList, StringInStr($sPathList, "*")-1) & "\"
      $sPathList = StringTrimLeft($sPathList, StringInStr($sPathList, "*")-1)
   Wend

   If $sFileList <> "" Then      
      $aCopyList = StringSplit($sFileList, "*")
      For $i = 1 To $aCopyList[0]-2 Step 3
         If Not FileExists($sDestDir & $aCopyList[$i]) Then DirCreate($sDestDir & $aCopyList[$i])
         If FileCopy($sSourceDir & $aCopyList[$i] & $aCopyList[$i+1], _
                  $sDestDir & $aCopyList[$i] & $aCopyList[$i+1], $nOverwrite) Then
            $nSize = $nSize+$aCopyList[$i+2]
            ProgressSet(Int(($nSize/$nTotalSize)*100), $aCopyList[$i+1] & @CRLF & "From: " & $aCopyList[$i])
         EndIf
      Next
   EndIf
   If $nSize = $nTotalSize Then
      Return 1
   Else
      Return 0
   EndIf
EndFunc

Func _MatchFilePattern($sFileName, $sPattern)
   Local $i
   Local $c
   Local $n
   Local $nMatch = 1
   
   For $i = 1 To StringLen($sPattern)
      $c = StringMid($sPattern, $i, 1)
      If $c <> "*" Then
         $n = StringInStr($sFileName, $c)
         If $n = 0 Then
            $nMatch = 0
            ExitLoop
         Else
            $sFileName = StringTrimLeft($sFileName, $n)
         EndIf
      EndIf
   Next
   Return $nMatch
EndFunc
Link to comment
Share on other sites

  • 4 weeks later...

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...