Renaming all files in a directory

J

Jan Kronsell

Hi NG

Im trying to rename/move all files in a directory using

Sub nytfors()
Set fs = Application.FileSearch
With fs
.LookIn = "C:\tb"
.Filename = "*.jpg"
End With
For i = 1 To fs.FoundFiles.Count
OldName = "C:\tb\" & fs.FoundFiles(i)
NewName = "C:\tb2\u" & i
'Debug.Print OldName, NewName
Name OldName As NewName
Next i
End Sub

but the code stops at
Name OldName As NewName

telling me its an invalid procedure call or argument.

Any ideas?

Jan
 
J

Jan Kronsell

Sorry. My mistake

OldName = "C:\tb\" & fs.FoundFiles(i)

should be

OldName = fs.FoundFiles(i)

And then it worked.

Jan
 
B

Bob Phillips

Jan,

Haven't tested it, but I would guess that you are not renaming, but moving
to a new directory as well.

You might need to create the directory

MkDir "C:\tb2\u"

at thestart.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
J

Jan Kronsell

Thnaks all, but i solved problem myself. I forgot that the fsfs.founfiles
already included the path, so when I removed the "c:\tb\" from the Oldname
variable it worked.

Jan
 

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