View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Harald Staff Harald Staff is offline
external usenet poster
 
Posts: 1,327
Default Rename Batch of Files in VBA

Hi Darren

Wow. Well, Google would actually help a lot if you broke down the big task
into small definite problems. Ok, first, you rename a file like this:

Sub test()
Name "C:\Temp\MyApp.ini" As "C:\Temp\WifesApp.ini"
End Sub

And here's one among several ways to scan folders and files from a root.
Display the immediate window in the VBE while running it, it shows all
Debug.Print actions. Note also the "*.*" in the file code, change to "*.xl*"
to limit to Excel files. Or whatever.

Sub test()
Call LookForDirectories("C:\Documents and Settings\All Users")
End Sub
Sub LookForDirectories(ByVal DirToSearch As String)
Dim counter As Integer
Dim i As Integer
Dim Directories() As String
Dim Contents As String

counter = 0
DirToSearch = DirToSearch & "\"
Contents = Dir(DirToSearch, vbDirectory)
Do While Contents < ""
If Contents < "." And Contents < ".." Then
If (GetAttr(DirToSearch & Contents) And _
vbDirectory) = vbDirectory Then
counter% = counter% + 1
ReDim Preserve Directories(counter)
Directories(counter) = DirToSearch & Contents
End If
End If
Contents = Dir
Loop
If counter = 0 Then Exit Sub
For i = 1 To counter
Debug.Print
Debug.Print "*********************"
Debug.Print "Folder " & Directories(i)
Debug.Print "*********************"
GetFilesInDirectory Directories(i)
LookForDirectories Directories(i)
Next i
End Sub

Sub GetFilesInDirectory(ByVal DirToSearch As String)
Dim NextFile As String
On Error Resume Next
With ActiveSheet
NextFile = Dir(DirToSearch & "\" & "*.*")
Do Until NextFile = ""
Debug.Print "Folder " & DirToSearch, NextFile
NextFile = Dir()
Loop
End With
End Sub

--
HTH. Best wishes Harald
Followup to newsgroup only please

"Darren Hill" skrev i melding
...
I'm using Excel 2000.
I have a big macro to create - I don't expect the group to do all my work,
information on how to get started would be good for now.

My goal:
I have a directory ("C:\My Documents\Archive") which contains multiple
subdirectories, each of which contains files to be renamed. Some
subdirectories also have their own subfolders.
Those files whose name starts with the string "File" needs that string
replaced with the folder name. If the file does not start with the string
"File", the immediate folder name gets added to the start of the folder
name.
The filelist will include non-Excel files.

I can do the string replacing bit, it's the creating a list of folders and
their content files, and then getting the immediate folder and using that

to
rename the files that I have no idea about, and Google isn't helping much

:)

Thank in advance.

Darren