' 3 July 2011 Simple Backup number x (i have stopped counting) Copyrighted by J. van Zijl aka Slowdown ' If you like/use the code or part of the code please send me an e-mail ' Q7Basic for Windows? 7/Vista/XP ALPHA 22-June-2011 (32 bit) ' Purpose, to copy more than one file/directory in one single action. ' In the first example (Simple Backup) the files where copied by directory ' instead file by file. ' This example will copy the files file by file ' Advantage is that you can see wich file is copied and you can abort the copy. ' The abort function is not here and you have to write it yourself :-) ' You can find me on, ' http://www.q7basic.org/forum/index.php and ' http://www.kbasic.com/forum/index.php ' j.vanzijl@quicknet.nl ' this is the mainwindow scope Outlet CopiedTolabel As QLabel Outlet RemoveEntryButton As QPushButton Outlet FilesCopiedlabel As QLabel Outlet DirectoryBox As QComboBox Outlet progressBar As QProgressBar Outlet menuDirectory_s As QMenu Outlet plainTextEdit As QPlainTextEdit Outlet StartCopyButt As QPushButton Outlet EndProgButt As QPushButton Outlet FilesToCopyLabel As QLabel Outlet DidCopyLabel As QLabel Outlet BytesToCopyLabel As QLabel Public Dim CurrentDirectoryIndex As Integer Public Dim DirectoryIndex As List Public Dim DestDirectory As String Public Dim SourcePathFile As String = Application.Path & "\\FromDirectory.xml" Public Dim DestenationPathFile As String = Application.Path & "\\ToDirectory.xml" Public Dim CurrentComboIndex As Int64 Global Dim ToTalFilesCopied As Int64 Global Dim TotalBytesCopied As Int64 Event Init() CheckAndLoad(SourcePathFile, True) CheckAndLoad(DestenationPathFile, False) RemoveEntryButton!enabled = False progressBar!minimum = 0 End Event Signal on_ProjectOpen() ' MainWindow!windowIcon = QIcon(":/Resources/FormIcon.png") End Signal Signal on_EndProgButt_clicked(Checked As Boolean) Application.Quit() End Signal Signal on_actionNo_more_close_me_please_triggered(Checked As Boolean) Application.Quit() End Signal Signal on_actionDirectory_s_to_be_copied_triggered(Checked As Boolean) Dim SelectedDir As String CheckAndLoad(SourcePathFile, True) SelectedDir = OpenFileDialog.GetDirectory() If Len(SelectedDir) > 3 Then DirectoryIndex.Append(SelectedDir) WriteList(DirectoryIndex, SourcePathFile) CheckAndLoad(SourcePathFile, True) Else MsgBox("Warning !!", "Root of drive : " & Left(SelectedDir, 2) & " is not allowed") End If End Signal Signal on_actionCopy_to_directory_triggered(Checked As Boolean) DestDirectory = OpenFileDialog.GetDirectory() If WriteString(DestDirectory, DestenationPathFile)= True Then CheckAndLoad(DestenationPathFile, False) End If End Signal Signal on_actionDelete_log_file_triggered(Checked As Boolean) Dim IsOk As Boolean If CB_.FilePressent (Application.Path & "\\SimpleBackupLog.txt")= True Then IsOk = Delete(Application.Path & "\\SimpleBackupLog.txt") End If End Signal Signal on_RemoveEntryButton_clicked(Checked As Boolean) Dim KeyAction As Integer RemoveEntryButton!enabled = False KeyAction = OpenDialog("RemoveDialog") If KeyAction = QDialog.Accepted Then DirectoryIndex.Remove(CurrentComboIndex) WriteList(DirectoryIndex, SourcePathFile) CheckAndLoad(SourcePathFile, True) End If End Signal Signal on_DirectoryBox_activated(Text As String) CurrentComboIndex = DirectoryBox.CurrentIndex If RemoveEntryButton!enabled = True Then RemoveEntryButton!enabled = False Else RemoveEntryButton!enabled = True End If End Signal Sub CheckAndLoad(CFile As String, CSource As Boolean) Dim Lus As Int64 If CSource = True Then If CB_.FilePressent(CFile)= True Then DirectoryIndex = ReadList(CFile) DirectoryBox.RemoveAll() plainTextEdit!plainText = "Directory's to be copied;" & "\n\n" For Lus = 0 To DirectoryIndex.Length() - 1 If Right(FilePath(DirectoryIndex.Object(Lus)), 1) <> "/" Then DirectoryBox.Append(FilePath(DirectoryIndex.Object(Lus)) & "/" & FileName(DirectoryIndex.Object(Lus))) plainTextEdit!plainText = plainTextEdit!plainText & FilePath(DirectoryIndex.Object(Lus)) & "/" & FileName(DirectoryIndex.Object(Lus)) & "\n" Else DirectoryBox.Append(FilePath(DirectoryIndex.Object(Lus)) & FileName(DirectoryIndex.Object(Lus))) plainTextEdit!plainText = plainTextEdit!plainText & FilePath(DirectoryIndex.Object(Lus)) & FileName(DirectoryIndex.Object(Lus)) & "\n" End If DoEvents() Next End If Else If CB_.FilePressent(CFile)= True Then DestDirectory = ReadString(CFile) If Right(DestDirectory, 1) <> "/" Then DestDirectory = DestDirectory & "/" End If CopiedTolabel.Text = DestDirectory End If End If End Sub ' =================== Code adaption date 26 june 2011 ========================= Signal on_StartCopyButt_clicked(Checked As Boolean) StartCopyButt!enabled = False StartToCopyThem() End Signal Signal on_actionStart_to_Copy_triggered(Checked As Boolean) StartCopyButt!enabled = False StartToCopyThem() End Signal Sub StartToCopyThem() Dim Lus As Int64 Dim TmpList As List ToTalFilesCopied = 0 TotalBytesCopied = 0 For Lus = 0 To DirectoryIndex.Length() - 1 FilesCopiedlabel.Text = " Simple Backup is reading the Directory-tree" DoEvents(10) TmpList = CB_.FindDirectorys(DirectoryIndex.Object(Lus), Lus) CB_.SaveLogFile(CopyDirFiles(TmpList, DestDirectory, Lus)) Next CopiedTolabel.Text = "" FilesCopiedlabel.Text = " Simple Backup is ready" progressBar!value = 0 plainTextEdit!enabled = True plainTextEdit!plainText = ReadString(Application.Path & "\\SimpleBackupLog.txt") StartCopyButt!enabled = True End Sub Function CopyDirFiles(DirectoryList As List, DestPath As String, PressentDir As Integer) As List Dim DirName As String Dim Lus As Int64 Dim TmpLus As Int64 Dim IsCopy As Boolean Dim IsDirectory As Boolean Dim TempList As List Dim NotCopied As List Dim NameFilter As List Dim FileList As List Dim BytesToCopy As Int64 Dim FilesToCopy As Int64 Dim BytesCopied As Int64 Dim FilesCopied As Int64 ' first we have to create the desination directory's FilesCopiedlabel.Text = " Simple Backup is writing the Directory-tree of " & DirectoryIndex.Object(PressentDir) & " on destination" DoEvents(10) For Lus = 0 To DirectoryList.Length() - 1 DirName = FilePath(DirectoryList.Object(Lus)) & "/" & FileName(DirectoryList.Object(Lus)) DirName = Right(DirName, Len(DirName) - 3) IsDirectory = Dir(DestPath & DirName, True) Next ' find the filenames and put them in a new List FilesCopiedlabel.Text = " Simple Backup is reading-in files of " & DirectoryIndex.Object(PressentDir) DoEvents(10) For Lus = 0 To DirectoryList.Length() - 1 TempList = Dir.FindFile(Trim(DirectoryList.Object(Lus)), NameFilter, Dir.Files Or Dir.Hidden Or Dir.System ) ' Trim is needed in this case for removing the non-printable characters For TmpLus = 0 To TempList.Length() - 1 If IsDir(TempList.Object(TmpLus))= False Then DirName = FilePath(TempList.Object(TmpLus)) & "/" & FileName(TempList.Object(TmpLus)) FileList.Append(TempList.Object(TmpLus)) FilesToCopy = FilesToCopy + 1 BytesToCopy = BytesToCopy + File.Size(DirName) FilesToCopyLabel.Text = "Files: " & String(FilesToCopy) BytesToCopyLabel.Text = CB_.MakeItReadAble(BytesToCopy) DoEvents() End If Next Next ' at this point we don't need the array's TempList and DirectoryList anymore so empty them. TempList.RemoveAll() DirectoryList.RemoveAll() ' the actual file copy code. progressBar!maximum = FileList.Length() -1 For Lus = 0 To FileList.Length() - 1 DirName = FilePath(FileList.Object(Lus)) & "/" & FileName(FileList.Object(Lus)) FilesCopiedlabel.Text = DirName progressBar!value = Lus DoEvents() IsCopy = Copy(DirName, DestDirectory & Right(DirName, Len(DirName) - 3)) If IsCopy = False Then NotCopied.Append(DirName) Else FilesCopied = FilesCopied + 1 ToTalFilesCopied = ToTalFilesCopied + 1 TotalBytesCopied = TotalBytesCopied + File.Size(DirName) DidCopyLabel.Text = "Copied: " & String(FilesCopied) End If Next progressBar!value = FileList.Length() -1 ' Make sure that the array 'FileList' is empty FileList.RemoveAll() Return NotCopied End Function