Replace first 8 letters of filename with Macro

C

cfatz1

Hi-

I have a bunch of files that all begin with a date - 20051231. After
the date is the rest of the filename and this part of the filename
varies. What I want to do is REPLACE the 20051231 portion of every
file in a specific folder with FINAL 2005. To be more specific....I
have over 1,000 files that each start with the date and then go on with
a different name for each file. Example:

20051231_FileA
20051231_FileB
20051231_FileC

I would like to keep the "FileA" portion of every file....but replace
the 20051231 with FINAL 2005. Any ideas?

Any help is greatly appreciated!!

TIA,
Chris
 
T

Tom Ogilvy

Sub RenameFiles()
Dim List() as String, cnt as Long
Dim fname as String

cnt = 0
fname = Dir("C:\MyFolder\20051231_*.xls")
do
cnt = cnt + 1
redim preserve list(1 to cnt)
list(cnt) = "C:\Myfolder\" & fname
fname = dir()
Loop while fname <> ""
for i = 1 to cnt
Name list(i) as Replace(list(i),"20051231","FINAL 2005")
Next
End Sub
 
G

Guest

*** CAUTION: PERFORM TEST RUNS FIRST! ***
Working from the DOS Command Prompt (Start|Programs|Accessories|Command
Prompt) navigate to the target directory.
Enter "RENAME ????????*.* FINAL2005*.*" at the prompt, then hit Enter.
Notice that there are 8 question marks, but FINAL2005 (without the space) is
9 characters. With your example filenames, the underscore will be replaced.
ALSO, any files that have the same names following the underscore will NOT
be renamed because there cannot be duplicate filenames.
Create a mock directory, add dummy text files with the old filenames, then
try this on them so as to get an understanding of how the DOS RENAME function
works *** BEFORE *** the actual execution.
 
C

cfatz1

Thanks Tom.

Don - Make sure there is a file>edit>replace option available before
replying to any posts...

Chris
 
G

Guest

Dim Filename As String
Dim Path As String

Path = "c:\MyFolder\"
Filename = Dir(Path & "2005*.txt")

Do While Filename <> ""
Name Path & Filename As Path & Replace(Filename, Left(Filename, 8), "FINAL
2005")
Filename = Dir
Loop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top