Renaming folders

R

Robin Clay

Greetings !

On the part of the Server that I have inherited,
there is a Tree consisting of
over 2,000 folders and sub-folders,
containing some 20,000 files in all.

Most of these folders have names such as

Transmittal Document379 040803 - Addendum 1 Rev01

where the 379 is the unique sequence number, and the
040803 is the date (3 Aug 2004)

What I want to do is "simply"
to run recursively through this entire batch,
deleting the words "Transmittal Document"

I would appreciate help on:
finding the sub-folder names
re-naming them
recursion in this context
finding the file names.

I DO know how to re-name THEM <grin>


Thanks in anticipation


RClay AT haswell DOT com
 
R

Rog

Hi Robin, I posted this code yesterday to help somebody
run recursively through files. It sould give you a very
good idea of how to get what you want. To use it, you
first need to set a reference to the Microsoft Scripting
Runtime object (from Tools|Refernces menu option)

Public Sub GetFiles()
'prints out file type and path for each file in PATH and
any subdirectories
Const PATH As String = "C:\Mcskew"
Dim objFSo As New FileSystemObject
Dim objFolder As Folder
Dim rngOut As Range

Set rngOut = Range("A1")
Set objFolder = objFSo.GetFolder(PATH)

Process rngOut, objFolder

End Sub

Public Sub Process(ByRef p_rngOut As Range, ByRef
p_objFolder As Folder)

Dim objFolder As Folder
Dim objFile As File

For Each objFile In p_objFolder.Files
'loop through all files
p_rngOut.Value = objFile.Name
p_rngOut.Offset(0, 1).Value = objFile.PATH
p_rngOut.Offset(0, 2).Value = objFile.Type
Set p_rngOut = p_rngOut.Offset(1)
Next

For Each objFolder In p_objFolder.SubFolders
'loop through all folders in this directory
Process p_rngOut, objFolder
Next

End Sub

Rgds

Rog
 
R

Robin Clay

Thank you so much -
that looks very much like what I need.

Erm... but I don't see the word "print" anywhere except in
the comment ?

i.e. where does it actually DO anything ? <grin>
i.e. where do I start my "operations"
to change the foldernames (how do I do that?)
and filenames (I can do that)?
 
R

Rog

As it stands, if you ran the Getfiles macro, it would dump
all the filenames/Path/type on the activesheet starting in
cell A1

The following will rename files and folders - you can
easily alter :

Option Explicit

Public Sub GetFiles()
'prints out file type and path for each file in PATH and
any subdirectories
Const PATH As String = "C:\Mcskew"
Dim objFSo As New FileSystemObject
Dim objFolder As Folder
Dim rngOut As Range

Set rngOut = Range("A1")
Set objFolder = objFSo.GetFolder(PATH)

Process rngOut, objFolder

End Sub

Public Sub Process(ByRef p_rngOut As Range, ByRef
p_objFolder As Folder)

Dim objFolder As Folder
Dim objFile As File

For Each objFile In p_objFolder.Files
'loop through all files
p_rngOut.Value = objFile.Name
p_rngOut.Offset(0, 1).Value = objFile.PATH
p_rngOut.Offset(0, 2).Value = objFile.Type

'rename file
If objFile.Name = "sausage.txt" Then
objFile.Name = "hello.txt"
End If
Set p_rngOut = p_rngOut.Offset(1)
Next

For Each objFolder In p_objFolder.SubFolders
'loop through all folders in this directory
'rename folder 'Danger' to 'crazy'
If objFolder.Name = "Danger" Then
objFolder.Name = "crazy"
End If
Process p_rngOut, objFolder
Next

End Sub
 
R

Robin Clay

The following will rename files and folders -

That seems to be what I wanted -
Thank you very much indeed!
 
R

Robin Clay

Erm... sorry about this, but....
Public Sub Process(ByRef p_rngOut As Range, _
ByRef p_objFolder As Folder)

....gives a
Compile error:
User-defined type not defined


I guess it don't care for your "Folder" type -
and perhaps your "File" type later on ?


RClay AT haswell DOT com
 

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