title: Loop through folder, renaming files that meet specific criteria using VBA?
body: <p>I am new to VBA (and have only a bit of training in java), but assembled this bit of code with the help of other posts here and have hit a wall.</p>
  
  <p>I am trying to write code that will cycle through each file in a folder, testing if each file meets certain criteria.  If criteria are met, the file names should be edited, overwriting (or deleting prior) any existing files with the same name.  Copies of these newly renamed files should then be copied to a different folder.  I believe I'm very close, but my code refuses to cycle through all files and/or crashes Excel when it is run.  Help please?  :-)</p>
  
  <pre><code>Sub RenameImages()
  
  Const FILEPATH As String = _
  "C:\\CurrentPath"
  Const NEWPATH As String = _
  "C:\\AditionalPath"
  
  
  Dim strfile As String
  Dim freplace As String
  Dim fprefix As String
  Dim fsuffix As String
  Dim propfname As String
  
  Dim FileExistsbol As Boolean
  
  Dim fso As Object
  Set fso = VBA.CreateObject("Scripting.FileSystemObject")
  
  strfile = Dir(FILEPATH)
  
  Do While (strfile &lt;&gt; "")
    Debug.Print strfile
    If Mid$(strfile, 4, 1) = "_" Then
      fprefix = Left$(strfile, 3)
      fsuffix = Right$(strfile, 5)
      freplace = "Page"
      propfname = FILEPATH &amp; fprefix &amp; freplace &amp; fsuffix
      FileExistsbol = FileExists(propfname)
        If FileExistsbol Then
        Kill propfname
        End If
      Name FILEPATH &amp; strfile As propfname
      'fso.CopyFile(FILEPATH &amp; propfname, NEWPATH &amp; propfname, True)
    End If
  
    strfile = Dir(FILEPATH)
  
  Loop
  
  End Sub
  </code></pre>
  
  <p>If it's helpful, the file names start as ABC_mm_dd_hh_Page_#.jpg and the goal is to cut them down to ABCPage#.jpg</p>
  
  <p>Thanks SO much!</p>