By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,663 Members | 1,867 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,663 IT Pros & Developers. It's quick & easy.

Rename File Do Loop Error

P: 13
Hi,

I'm trying to create a do loop that renames files in a folder.

My current code looks like this:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Rename_Files_Click()
  2. Dim VPNPath As String
  3. VPNPath = Dir("C:\Temp\*.txt")
  4.  
  5. Do While Len(VPNPath) > 0
  6.  
  7. Name "C:\Temp\" & VPNPath As "C:\Temp\" & Left(VPNPath, 4) & ".tx1"
  8. Loop
  9. End Sub
  10.  
The code works but only for the first file in the folder after which an error message is returned Run-time error '53': file not found

Any thoughts on where I'm going wrong???
Jan 23 '08 #1
Share this Question
Share on Google+
2 Replies


JKing
Expert 100+
P: 1,206
Hi,

I'm trying to create a do loop that renames files in a folder.

My current code looks like this:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Rename_Files_Click()
  2. Dim VPNPath As String
  3. VPNPath = Dir("C:\Temp\*.txt")
  4.  
  5. Do While Len(VPNPath) > 0
  6.  
  7. Name "C:\Temp\" & VPNPath As "C:\Temp\" & Left(VPNPath, 4) & ".tx1"
  8. Loop
  9. End Sub
  10.  
The code works but only for the first file in the folder after which an error message is returned Run-time error '53': file not found

Any thoughts on where I'm going wrong???
You aren't reseting VPNPath in the loop. So it keeps looking for the first found file which is no longer there because you renamed it.

Add the line as shown at the end of the loop. This should move to the next file and complete your process.

Expand|Select|Wrap|Line Numbers
  1. Private Sub Rename_Files_Click()
  2. Dim VPNPath As String
  3. VPNPath = Dir("C:\Temp\*.txt")
  4.  
  5. Do While Len(VPNPath) > 0
  6.  
  7. Name "C:\Temp\" & VPNPath As "C:\Temp\" & Left(VPNPath, 4) & ".tx1"
  8. VPNPath = Dir
  9. Loop
  10. End Sub
  11.  
Jan 23 '08 #2

P: 13
Cheers worked a charm
Jan 23 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.