Solved

Renaming Files

Posted on 1999-01-29
5
229 Views
Last Modified: 2010-05-03
How would you open the files in a given directory and rename them one after another without knowing the exact name of each file in the directory, just the file extension?
0
Comment
Question by:timothy1
5 Comments
 
LVL 2

Accepted Solution

by:
sbmc earned 50 total points
ID: 1471239
a=dir("c:\*.*")
do until a=""
   name "c:\" & a as "c:\old" & a
   a=dir
loop

this will rename all files with a prefix of old
0
 
LVL 1

Expert Comment

by:rayford
ID: 1471240
assume a simple form with three textboxes named textbox1,2,3 and a single command button named command1.
given contents of the textboxes as follows
Text1.text="C:\test"      'path to folder to rename files in
Text2.text="txt"          'filespec extension to rename
Text3.text="bak"          'rename all TXT files to BAK's

click command button code to perform the rename would be:

Private Sub Command1_Click()
Myfile = Dir(Text1 & "\*." & Text2)
While Myfile <> ""
    Name Text1 & "\" & Myfile As Text1 & "\" & Left(Myfile, Len(Myfile) - Len(Text2)) & Text3
    Myfile = Dir
Wend
End Sub

This is assuming the only thing changing was the file extension on the rename.  You would alter the code slightly to get other results.
0
 

Author Comment

by:timothy1
ID: 1471241
I put the following code into a form and a bunch of files on my d: drive. It worked great! except that it just put a prefix on the existing file name ie. old.name.txt

I want to rename variousnames.txt to read newname.txt, not just put a prefix on the old name.

Private Sub cmdRename_Click()
Dim a
a = Dir("D:\*.*")
Do Until a = ""
   Name "D:\" & a As "D:\old" & a
   a = Dir
Loop

'this will rename all files with a prefix of old
MsgBox "Done!"
End Sub

0
 

Expert Comment

by:dudemann
ID: 1471242
You could also check out my shareware utility:  File Name Case Converter.  It can be found at:  http://www.primenet.com/~danield/netutilz/netutilz.htm.  

Dan.
0
 

Author Comment

by:timothy1
ID: 1471243
Well I looked up dir() and name in Online Boxs and then rewrote the above to fit my own needs.

Thanks anyway.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB6 ListBox Question 4 48
Problem to With line 4 63
How to read File Date Created using VB6 8 52
pop out of webbrowser1 control vba6 5 25
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question