rename files

J

Jason

Hello,

Can somebody help me to automatically rename files.

I've files with names like A1.txt in several folders which has a date in
their name, like 20041111-52365.

What i would like is to rename them like A<foldername (without
the -number)_1.txt

I would be very thankfull.

J
 
B

Bas Cost Budde

Jason said:
Hi Bas,

Can you tell me how?

Yes. But, 'how' is the third step only, where 'what is the desired
result' is the first step, and 'what are the starting conditions' is the
second. Can you provide this information?

Or, stated less abstractly:

* what files are to be manipulated? Do you (as a programmer) know some
location, or several locations, in advance? Does somebody have to
indicate a folder in the file system? Do all files in the tree have to
be renamed? If not, which ones do, and which don't?

* does every file receive its new name via the same 'formula'? I
understand you want to include the date part of the containing folder
name into the new file name.
 
J

Jason

Hi Bas,

This is a one-time action only.

The files are on a shared networkdrive and all stored in several
directories. The files have .out extensions.

What i would like is to point the directory and rename the files in the
subdirectories.
The result of each file whatever the name is, should be to begin with the
date in the folder, then a underscore with the original filename.

Greetings,

J
 
B

Bas Cost Budde

Jason said:
Hi Bas,

This is a one-time action only.

The files are on a shared networkdrive and all stored in several
directories. The files have .out extensions.

What i would like is to point the directory and rename the files in the
subdirectories.
The result of each file whatever the name is, should be to begin with the
date in the folder, then a underscore with the original filename.

I get the idea.

Maybe it is safest to write a routine that will do just one folder.
Directory.

You can then fire up this routine for as many directories as you need.

Inside the directory, you can scan for files using the Dir() function (I
do find the Help on this subject useful), and rename the file using the
Name statement. Retain the folder name before you start looping the files.

Are you right-handed in coding? Or should I maybe try to code this?
 
J

Jason

Hi Bas,

I'm learning, so if you could help me with this, i would be very greatful.

Greetings,

J
 
B

Bas Cost Budde

Jason said:
Hi Bas,

I'm learning, so if you could help me with this, i would be very greatful.

Sub NameFilesWithFolder(cPath As String)
'you must pass a valid path to this routine; it may end in a backslash
Dim cFile As String
Dim cPrefix As String
Dim nPos As Long
'find the last folder in cPath
If Right(cPath, 1) <> "\" Then cPath = cPath & "\"
nPos = Len(cPath) - 1
Do Until Mid(cPath, nPos, 1) = "\"
nPos = nPos - 1
If nPos = 1 Then Exit Do 'just in case there is no \ at all
Loop
cPrefix = Mid(cPath, nPos + 1, 8) & "_" 'taking the first 8
characters of this folder
'must be the date
cFile = Dir(cPath & "*.*")
Do Until cFile = ""
Name cPath & cFile As cPath & cPrefix & cFile
cFile = Dir() 'will return "" when no more files
Loop
End Sub
 
J

Jason

Hi Bas,

Thanks for your code example. It is exactly what i want, but i tried to
modify it to do it for also subdirectories but not successful. Could you
make it do that for me, so i can learn it to do it myself?

Greetings,

J
 
B

Bas Cost Budde

Jason said:
Hi Bas,

Thanks for your code example. It is exactly what i want, but i tried to
modify it to do it for also subdirectories but not successful. Could you
make it do that for me, so i can learn it to do it myself?

When you invoke Dir(), you can specify a second parameter. When that is
vbDirectory, you will get the names of all subdirectories (if any),
instead of file names.

The process must become recursive (calling itself) but I presume the
date of the 'root' directory is to be written before every filename? If
not, i.e. every subdirectory is named with a date too, the recursion can
be simple.
 
J

Jason

Hi Bas,

That is what i did, but the root isn't a date, but the subdirectories are.
For e.g. the rootfolder is c:\results\ and the subfolders are like
20041117_123456.
The code should rename the files in the subdirectories but not the directory
where they reside in.

Can this be done easily?

Greetings,
J
 
B

Bas Cost Budde

Jason said:
Hi Bas,

That is what i did, but the root isn't a date, but the subdirectories are.
For e.g. the rootfolder is c:\results\ and the subfolders are like
20041117_123456.
The code should rename the files in the subdirectories but not the directory
where they reside in.

Can this be done easily?

So only one level of subdirectories? That means you can call my routine
from a simple structure that loops through the 'root'

untested code:

sub NameAll(cPath as string)
dim cDir as string
cdir=dir(cpath,vbdirectory)
do until cdir=""
namefileswithfolder cpath & "\" & cdir
loop
end sub
 
J

Jason

Hi Bas,

Thanks for helping me. I tried your code, but it doesn't work or i did
something wrong.

Here's your code from the beginning, but where should i put the second one?

Public Sub NameFilesWithFolder(cPath As String)
'you must pass a valid path to this routine; it may end in a backslash
Dim cFile As String
Dim cPrefix As String
Dim nPos As Long
'find the last folder in cPath
If Right(cPath, 1) <> "\" Then cPath = cPath & "\"
nPos = Len(cPath) - 1
Do Until Mid(cPath, nPos, 1) = "\"
nPos = nPos - 1
If nPos = 1 Then Exit Do 'just in case there is no \ at all
Loop
cPrefix = Mid(cPath, nPos + 1, 8) & "_" 'taking the first 8 characters
of this folder
'must be the date
cFile = Dir(cPath & "*.*")
Do Until cFile = ""
Name cPath & cFile As cPath & cPrefix & cFile
cFile = Dir() 'will return "" when no more files
Loop
End Sub
 
B

Bas Cost Budde

Jason said:
Hi Bas,

Thanks for helping me. I tried your code, but it doesn't work or i did
something wrong.

Here's your code from the beginning, but where should i put the second one?

Do you have the first part in a module? You should. And paste the second
part in the same module (I suggest below the first but that doesn't make
differences. As long as you keep them apart :) )

Make a habit of telling me (or whoever) *what* you try and *what* goes
wrong (no effect, wrong effect, messages)
 
J

Jason

Hi Bas,

I pasted it in the same module, but nothing happens with the files. It seems
that the patch has a "." and then skips the name part.

How can i fix this?

J
 
B

Bas Cost Budde

Jason said:
Hi Bas,

I pasted it in the same module, but nothing happens with the files. It seems
that the patch has a "." and then skips the name part.

How can i fix this?

As I said, untested... there is no bounds checking statement. To say the
least. This should be better:

Sub NameAll(cPath As String)
Dim cDir As String
cDir = Dir(cPath, vbDirectory)
Do Until cDir = ""
If Left(cDir, 1) <> "." Then
NameFilesWithFolder cPath & "\" & cDir
End If
cDir = Dir()
Loop
End Sub
 
J

Jason

Hi Bas,

I tried it out, but it keeps failing on the following lines:

Do Until cDir = ""
If Left(cDir, 1) <> "." Then
NameFilesWithFolder cPath & "\" & cDir
End If
cDir = Dir()
Loop
It stops after the end if.

Do you know what the cause is? It try to check it but it seems ok.

J
 
B

Bas Cost Budde

Jason said:
Hi Bas,

I tried it out, but it keeps failing on the following lines:

Do Until cDir = ""
If Left(cDir, 1) <> "." Then
NameFilesWithFolder cPath & "\" & cDir
End If
cDir = Dir()
Loop
It stops after the end if.

mmm. The Help says that Dir is not re-entrant :-( so this approach
doesn't work.

Since we have a database, I could divert to storing all file names in a
table, and doing the Name statements "all at once". Name can work with
absolute paths so that's what is to store.

If you had done the renaming by hand, you would be finished by now :)
You can adapt the strategy by having a form that displays all your
folders in the root; then by clicking on one folder, the
NameFilesWithFolder will be called for that folder. Does that sound like
something?
 
T

Tim Ferguson

mmm. The Help says that Dir is not re-entrant :-( so this approach
doesn't work.

The idea is to do a pre-ordering iteration: use dir() to get all the
directory names into an array, then recurse through each member of the
array:

public sub GetSubDirs(strRoot)
do while len(dir())>0
AddItToArray
loop

for each strDirName in MyArray
GetSubDirs strDirName
next strDirName

End Sub


.... well that kind of thing. There is an example in help, or the MKB
somewhere. All the best


Tim F
 
J

Jason

Hi bas,

Thnx for the effort, i could be finished by now :). But that's not the
point. I still want to learn how it can be done. Tim also replied to this
subject and has an example code. Could you take a look at it?

J
 

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