473,398 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Rename File Do Loop Error

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
2 1922
JKing
1,206 Expert 1GB
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
Cheers worked a charm
Jan 23 '08 #3

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

Similar topics

7
by: Tom | last post by:
I'm having a problem using a path with spaces as a parameter to os.rename() in a program on WinXP. This works fine at the command line (where the folder "c:\aa bb" exists) > os.rename( "c\aa...
4
by: Larry R Harrison Jr | last post by:
I have Access XP. I know Visual Basic quite well. One thing I know Access can do--and I can't quite figure out how--is to rename the files in a directory. If the file is named "aug01_003.jpg" I...
2
by: Bruce Russell | last post by:
This may sound stupid but I can't rename the WebForm1.aspx in the solution explorer. The file is located in my local web server at C:\Inetpub\wwwroot\Lab3-VB-Starter\WebForm1.aspx Is there...
3
by: Erik Foreman | last post by:
I am using a folderbrowserdialog object to prowse to a folder then once in that folder I am going to have my program rename all of the files in that folder. I know the rename funchtion but I am not...
5
by: Tony Meyer | last post by:
On Windows, if I do os.rename(old, new) where old is a file that is in-use (e.g. python itself, or a dll that is loaded), I would expect that an error would be raised (e.g. as when os.remove is...
2
by: AA Arens | last post by:
During my database construction, I left the names of the buttons, comboboxes named by default like Combo77, Command43. I can rename them with Properties Other Name, but them I start getting...
12
by: mantrid | last post by:
Im trying to move a file but am having luck my code is below. The temp and target paths are valid as they echo correctly. but I cant get the copy() function to work, or the rename() function ...
1
by: hardik | last post by:
can anyone tell me how i can change the filename which is going to uploaded here is a code for uploading a file <!-- #include file="clsUpload.asp" --> Set objUpload = New clsUpload If...
1
by: jonathan184 | last post by:
trying to rename filenames and extensions then add a header in line1 of each file if the header existed in line 1 to go to the next file. but i am getting error explciti errors Here is my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.