Link to home
Start Free TrialLog in
Avatar of timothy1
timothy1

asked on

Renaming Files

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?
ASKER CERTIFIED SOLUTION
Avatar of sbmc
sbmc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rayford
rayford

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.
Avatar of timothy1

ASKER

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

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.
Well I looked up dir() and name in Online Boxs and then rewrote the above to fit my own needs.

Thanks anyway.