Solved

Renaming Files

Posted on 1999-01-29
5
227 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now