Copy only the first file of a folder VBA

2019-07-29 18:47发布

I have found out how to copy all the files from a folder to another folder using VBA.

The only thing I can't find is how that I can only move the first three items of a folder.

I'm taking three pictures per item and for every item I create another folder using an excel sheet. So it should move (or copy) three pictures, and then move to the next three and do the same (but with another folder).

the only thing I can't find is how to select only the three first items from a folder.

I use the .copyfile method at the moment.

Dim ESN, FromPath, ToPath, LastRow, PN, FileToCopy, MyFile, i, k,     NumberOfFiles As Integer, Module
LastRow = ActiveWorkbook.ActiveSheet.Range("E" & Rows.Count).End(xlUp).Row + 1
 NumberOfFiles = InputBox("How many parts do you want to enter?", "# of parts?")

Dim FSO As Object
Set FSO = CreateObject("scripting.filesystemobject")
'Select all files
FileToCopy = "*.*"


ESN = ActiveWorkbook.ActiveSheet.Range("A1").Value
ESN = Right(ESN, 6)
For k = NumberOfFiles To 1 Step -1
'path toekennen
Module = ActiveWorkbook.ActiveSheet.Range("B" & LastRow - k).Value
PN = ActiveWorkbook.ActiveSheet.Range("G" & LastRow - k).Value
FromPath = "D:\DCIM\100OLYMP\"
ToPath = "U:\tmo\Checklist Engine Records\LEAP 1A\PHOTOS\" & Module & _
"\" & ESN & "_" & PN & "\"
MyFile = Dir(FromPath & "\*.JPG")

'folder creeeren als deze niet bestaat
If FSO.folderexists(ToPath) = False Then
FSO.createfolder ToPath
End If
    For i = 0 To 2
    FSO.movefile Source:=FromPath & FileToCopy, Destination:=ToPath
    MyFile = Dir
    Next i
Next k`

Thanks! KawaRu.

1条回答
Deceive 欺骗
2楼-- · 2019-07-29 19:39
myfile=dir("c:\yourpath" & "\*.jpeg")
i=1
for i = 1 to 3
 ' your commands
myfile=Dir
next i

this might help you...add this code in your function or sub...

查看更多
登录 后发表回答