Page 1 of 1

Copy All File In Path with Specific Filename

Posted: Fri Jun 05, 2020 4:19 pm
by parakorn
เรียนพี่ๆ และ อาจารย์ครับ

จากโค้ดนี้ผมต้องการปรับปรุงให้สามารถกำหนด ชื่อไฟล์ขึ้นต้นตามที่เราต้องการครับ

เช่น ผมต้องการ Copy File เฉพาะที่ขึ้นต้นว่า CRR โค้ดก็จะ Copy ไฟล์ที่ขึ้นต้นด้วยคำนี้เท่านั้นครับ

ขอรบกวนชี้แนะด้วยครับ

Code: Select all

Sub CopyAllFileInfolder()
    Dim FileSystem As Object
    Dim HostFolder As String

    HostFolder = Sheets("Cdir").Range("B3").Value
    
    Set FileSystem = CreateObject("Scripting.FileSystemObject")
    DoFolder FileSystem.GetFolder(HostFolder)
End Sub

Sub DoFolder(Folder)
    Dim SubFolder
    For Each SubFolder In Folder.SubFolders
        DoFolder SubFolder
    Next
    Dim File
    For Each File In Folder.Files
        'MsgBox File
        FileCopy File, "C:\Users\Pitheerap\Desktop\Upload SQL\Folder1\" & VBA.Split(File, "\")(UBound(VBA.Split(File, "\")))
    Next
End Sub

Re: Copy All File In Path with Specific Filename

Posted: Sat Jun 06, 2020 9:30 am
by snasui
:D ตัวอย่าง Code ครับ

Code: Select all

'Other code
For Each File In Folder.Files
    'MsgBox File
    If InStr(File, "\CRR") Then
        FileCopy File, "C:\Users\Pitheerap\Desktop\Upload SQL\Folder1\" & VBA.Split(File, "\")(UBound(VBA.Split(File, "\")))
    End If
Next
'Other code

Re: Copy All File In Path with Specific Filename

Posted: Wed Jun 10, 2020 12:07 am
by parakorn
snasui wrote: Sat Jun 06, 2020 9:30 am :D ตัวอย่าง Code ครับ

Code: Select all

'Other code
For Each File In Folder.Files
    'MsgBox File
    If InStr(File, "\CRR") Then
        FileCopy File, "C:\Users\Pitheerap\Desktop\Upload SQL\Folder1\" & VBA.Split(File, "\")(UBound(VBA.Split(File, "\")))
    End If
Next
'Other code
ขอบพระคุณอาจารย์มากครับ :cp: :cp: