title: Using excel vba to copy specific file extensions to another folder using the FSO method
body: <p>Trying to use fso technique to copy from source folder C:\ ( V) to target folder C:(All) but running code <strong>give message runtime error 53. file not found</strong> </p>
  
  <p>What I am trying to achieve is to copy all xlsx file from source folder C:\ V which contains also other file extension pdf, csv, txt, word..</p>
  
  <p>All xlsx will be copied to folder C:\ALL,</p>
  
  <p>Getting runtime error on this line below</p>
  
  <p>****FSO.CopyFile Source:=sourcePath &amp; fileExtn, Destination:=destinationPath****</p>
  
  <pre><code>Sub copy_specific_files_in_folder()
  
  
  
  
  Dim FSO As Object
  Dim sourcePath As String
  Dim destinationPath As String
  Dim fileExtn As String
  
  sourcePath = "c:\V"
  
  destinationPath = "c:\all\"
  
  
  fileExtn = " * .xlsx"
  
  
  If Right(sourcePath, 1) &lt;&gt; "\" Then
  sourcePath = sourcePath &amp; "\"
  End If
  
  Set FSO = CreateObject("scripting.filesystemobject")
  
  If FSO.FolderExists(sourcePath) = False Then
  
  MsgBox sourcePath &amp; " does not exit"
  
  Exit Sub
  
  End If
  
  If FSO.FolderExists(destinationPath) = False Then
  MsgBox destinationPath &amp; " does not exit"
  
  Exit Sub
  End If
  
  FSO.CopyFile Source:=sourcePath &amp; fileExtn, Destination:=destinationPath
  
  copy_files_from_subfolders
  
  MsgBox "your files have been copied from subfolders of " &amp; sourcePath &amp; "to" &amp; destinationPath
  
  
  
  End Sub
  
  
  
  
  Sub copy_files_from_subfolders()
  
  Dim FSO As Object, fld As Object
  Dim fsoFile As Object
  Dim fsoFol As Object
  
  sourcePath = "c:\V"
  
  targetpath = "c:\all\"
  
  
  If Right(sourcePath, 1) &lt;&gt; “ \ ” Then sourcePath = sourcePath &amp; “ \ ”
  
  Set FSO = CreateObject(“scripting.filesystemobject”)
  Set fld = FSO.GetFolder(sourcePath)
  If FSO.FolderExists(fld) Then
  For Each fsoFol In FSO.GetFolder(sourcePath).SubFolders
  For Each fsoFile In fsoFol.Files
  If Right(fsoFile, 4) = “xlsx” Then
  fsoFile.Copy targetpath
  End If
  Next
  Next
  End If
  
  End Sub
  </code></pre>