Quantcast
Channel: VBForums - Visual Basic 6 and Earlier
Viewing all articles
Browse latest Browse all 22038

Take very long time to Search, Copy and Paste

$
0
0
Hello,
What's wrong on my code, although it get what I need, but take about 30mins to complete.
I would like to back up some important data File from C:\Check Result\
Inside the C:\Check Result\, there has few thousand of SubFolder Name
and Inside the Subfolder, I need to backup some data from it,
For Example:
C:\Check Result\00012345A\
C:\Check Result\00012346A\
C:\Check Result\00012347A\
C:\Check Result\00012348A\
"
"
"
Inside the Subfolder, there has many of csv extension file

I used Excel to Sorting which Data I need to back Up,
Thus, in Excel file A1 to A250 with some data Which I need to back up the file with filename last word as "n.csv", csv is extension name,
inside the subfolders, there are many csv files, I just need the csv file with name "??????????n.csv", ? can be any number or word
Let said the above subfolder name xxx12345A, 12345 is Serial number,
12345, 12346, 12347, 12348, 12349.............................
from my Excel file name "backupnumber.xls", the A1 cells with the serial number which I need to back up the "n.csv" file to backup folder
A1= 12345
A2= 12348
A3= 12350......................................................
I write a code, to open the backupnumber.xls excel file,
get the A1 data, and compare the Subfolder partial name whether match with the A1 data,
if Match, then will go into the Subfolder and get the filename "???????n.csv" file,
then copy it and paste into backup folder,
Next until A1 till A250 data was completed get and past into backup folder.
My code is working, only it get longer times,
I am appreciate if you can give me some advise,
what's wrong on my code take time.
Thanks a lot.
Code:

        Dim fso As New FileSystemObject
        Dim oXL As Object
        Dim oBook As Object
        Dim oSheet As Object
        Dim k
        Dim ObjFolder
        Dim ObjSubFolders
        Dim ObjSubFolder
        Dim ObjFiles
        Dim ObjFile
        Dim Foldername
        Dim Fname
   
   

        On Error Resume Next
               
        Set oXL = CreateObject("Excel.Application")
        Set oBook = oXL.Workbooks.Open("c:\Backupnumber.xls")
        Set oSheet = oBook.Worksheets(1)

        Set fso = CreateObject("Scripting.FileSystemObject")
        Set ObjFolder = fso.GetFolder("C:\Check Result\)
        Set ObjSubFolders = ObjFolder.SubFolders
        Set ObjFiles = ObjSubFolder.Files
            fso.CreateFolder "c:\Backup Data\"
 
            For Each ObjSubFolder In ObjSubFolders
                    Foldername = ObjSubFolder.Name
                      For k = 1 To 250
                      Label2.Visible = True
                          If oSheet.range("A" & k) <> 0 Then
                                If InStr(1, Foldername, oSheet.range("A" & k).Text) > 0 Then
                                      For Each ObjFile In ObjSubFolder.Files
                                          Fname = ObjFile.Name
                                              If InStr(LCase$(Fname), LCase$("n.csv")) Then
                                              fso.CopyFile ObjFile, "c:\Backup Data\", False
                                              Else
                                          End If
                                      Next
                                Else
                                End If
                          Else
                          End If
                      Next k
            Next


Viewing all articles
Browse latest Browse all 22038

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>