Move files by list in array

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Re: Move files by list in array

Post by YasserKhalil »

Thanks a lot Mr. Hans for your great and helpful efforts. I appreciate your help a lot
Thank you very much

I have posted a thread at this link
https://www.excelforum.com/excel-progra ... names.html" onclick="window.open(this.href);return false;

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Re: Move files by list in array

Post by YasserKhalil »

At last I could figure it out using this code

Code: Select all

Sub Move_Files()
    Dim e           As Variant
    Dim x           As Variant
    Dim s           As String
    Dim t           As String
    Dim f           As String
    Dim c           As Long

    s = ThisWorkbook.Path & "\المستمسكات\"
    t = ThisWorkbook.Path & "\مستمسكات القائمة\"

    For Each e In Array("*.jpg")
        f = Dir(s & e)
        
        Do While f <> ""
            x = Application.Match(Replace(f, "." & Split(e, ".")(1), ""), Columns(1), 0)
            If Not IsError(x) Then
                FileCopy s & f, t & f
                Kill s & f
                c = c + 1
            End If

            f = Dir
        Loop
    Next e

    MsgBox "Total Number Of Moved Files : " & c, 64
End Sub
Thanks a lot Mr. Hans for your great efforts in this issue
Regards

User avatar
HansV
Administrator
Posts: 78615
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Move files by list in array

Post by HansV »

What is the purpose of

Code: Select all

     For Each e In Array("*.jpg")
        ...
    Next e
The only value in the array is "*.jpg" so there is no need for a loop...
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Re: Move files by list in array

Post by YasserKhalil »

First may be I can deal with other extensions in the future
Second is that this is the way I could escape from the file names themselves as I depended on the extension and see if the file name is existing in the column or not ..

User avatar
HansV
Administrator
Posts: 78615
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Move files by list in array

Post by HansV »

OK, I get the first point.
And instead of looping through the names in column A, you're looping through the files in the source folder and checking if they match column A.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Re: Move files by list in array

Post by YasserKhalil »

But this way works for me and I don't know why other ways doesn't work. That seems too weird for me

User avatar
HansV
Administrator
Posts: 78615
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Move files by list in array

Post by HansV »

I don't know either, but perhaps the way left-to-right text and right-to-left text are combined in the filename cause the problem.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Re: Move files by list in array

Post by YasserKhalil »

If so why the hard-coded code that I typed the names of the files work well ..!!?

User avatar
HansV
Administrator
Posts: 78615
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Move files by list in array

Post by HansV »

I really don't know.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4931
Joined: 31 Aug 2016, 09:02

Re: Move files by list in array

Post by YasserKhalil »

Thank you very very much Mr. Hans
I really appreciate your great efforts my tutor. You are awesome