Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find & Rename 1

Status
Not open for further replies.

sborny

Technical User
Jun 29, 2001
157
0
0
GB
Hi all,

I need to very quickly put together an app that will search all directories and subdirectories on a drive and look for files call folder.jpg. When it finds one i need it to change the name to AlbumArt.jpg without any user intervention and then move onto the next one automatically.

Unfortunately I have not had any experience in searching for files etc so was wondering if someone could help.

Thanks

Symon.

Everything has an answer, it's just knowing the right question to ask. !!!!
 
Search this forum for:

recursive
dir



Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
You can do this with the FileSystemObject and a little recursion.

First, reference "Microsoft Scripting Runtime" under Project->References.

Then, do this:

Private Sub RenameJPGs(ByVal Path As String)
Dim fs As New Scripting.FileSystemObject
Dim f As Folder
Dim sf, fil
Dim f1 As Folder

Set fld = fs.GetFolder(Path)

Set sf = fld.SubFolders

For Each f1 In sf
If f1.SubFolders.Count > 0 Then
RenameJPGs(f1.Path)
End If

For Each fil In f1.Files
Is fil.Name = "folder.jpg" Then
fs.CopyFile f1.Path & "\" & fil.Name, f1.Path & "\AlbumArt.jpg"
fs.DeleteFile f1.Path & "\" & fil.Name
End If
Next
Next
End Sub

Just call this sub and pass it the path you want to search (e.g., "C:\") and it should work.

I recommend testing it first on a sub directory before unleashing it on an entire hard drive. :D

Hope this helps.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Thanks jebenson,

It works ok if there is only one subdirectory but I have say the drive with a directory and then inside that directory could be 2 or 3 directories that need to be checked.

It works fine on the first directory that I finds but does nothing with the others.

Any ideas.

Cheers Symon.

Everything has an answer, it's just knowing the right question to ask. !!!!
 
Lot's of examples if you search the forum for:

recursive
dir



Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
DrJavaJoe,

I have done a search, but as stated I unfortunately have no experience in this area and therefore don't understand what I am looking at.

Again unfortunately I need to get this running tonight and do not have the time on this occasion to study the ins and outs of recursion.

Thanks for your help anyway.

Cheers
Symon.

Everything has an answer, it's just knowing the right question to ask. !!!!
 
DJJ,
Is there an echo in here? [smile]

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first
'If we're supposed to work in Hex, why have we only got A fingers?'
Essex Steam UK for steam enthusiasts
 
Take a look at Hypetia's solution in this thread.

thread222-609352



Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
I tested this on my computer, and starting from the root of the drive (C:\), the routine found and renamed files as many as 5 directories deep. There is no reason why it should not work for any number of subdirectories, except for memory and OS limitations.

Are you sure all of the files are named identically?



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Hi Jebenson,

Yeah all the names are identical. I am running XP Pro with 2GB ram so I don't think that memory is an issue.

Just to clarify the directory structure is not that it goes five levels deep. Here is what I mean:

c:\music\athlete\tourist\folder.jpg

c:\music\athlete\vehicles & animals\folder.jpg

As you can see inside the music directory is a directory called athelete and inside here are 2 directories.

It only seems to find the first one. So as said it does not look like a problem with how deep it goes, just if there is more than 1 directory at a particular level.

Hope this makes sense.

Cheers

Symon.

Everything has an answer, it's just knowing the right question to ask. !!!!
 
Well, I still don't understand why its not working, because in my testing I did similar directory structures, and it got all of the files at the same level (e.g., C:\Temp\Dir1\folder.jpg and C:\Temp\Dir2\folder.jpg, etc.). I just now did a test to make sure that it can handle spaces in the folder names or other characters (i.e., "&"), and it still works.

The only other thing I can see that might be causing this is that I am on Win 2K Pro, not XP Pro. I've never tried using the FileSystemObject on an XP machine, but it is my understanding that Microsoft recommends its usage in .NET, which to me means that it should work fine with XP.

Have you tried a small dir structure for testing, and put a breakpoint in the code to see what is happening? You should be able to see the dir names and paths as the code iterates.

If that doesn't work you may need to try using the Dir function, as others have suggested.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Hi jebenson,

I was being really stupid. Unfortunately i did not realise that the serach was case sensitive. Some of the files were names folder.jpg and some were Folder.jpg.

I have added an "Or" in the if statement so that it looks for both and it know works perfectly.

Thanks for all your help.

Cheers

Symon.

P.S. I have given you a star for your help.

Everything has an answer, it's just knowing the right question to ask. !!!!
 
here's the first hint of why it doesn't work:

If f1.SubFolders.Count > 0 Then

 
If f1.SubFolders.Count > 0 Then

strongm - what's wrong with that?


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
You'll only recurse into that folder if it has at least one subfolder. If it only has files it won't enter it ...
 
Actually, it will only recurse the subfolders if there is one or more subfolders. If there are no subfolders, it skips over the code inside the if, then loops through the files in that folder. Say there is a dir structure like this:

C:\Temp\Subdir1\Subdir2
C:\Temp\Subdir3\SubDir4

and you call the function with a parameter of "C:\Temp". The function will:

1) Count 2 subdirs under "C:\Temp", and recursively call itself with a parameter of "C:\Temp\Subdir1"

2) Count 1 subdir under "C:\Temp\Subdir1"; calls itself with "C:\Temp\Subdir1\Subdir2"

3) Count 0 subdirs under "C:\Temp\Subdir1\Subdir2"; does not call itself recursively and then loops through any files in "C:\Temp\Subdir1\Subdir2"; exits the function call from 2)

4) Call from 1) has looped through all subdirs of "C:\Temp\Subdir1"; loops through all files in "C:\Temp\Subdir1"; exits call from 1)

5) Initial function call calls function again with "C:\Temp\Subdir3"

6) Counts 1 subdir under "C:\Temp\Subdir3"; calls itself, passing "C:\Temp\Subdir3\Subdir4"

7) Counts 0 subdirs under "C:\Temp\Subdir3\Subdir4"; does not call itself recursively and then loops through any files in "C:\Temp\Subdir3\Subdir4"; exits function call from 6)

8) Function call to "C:\Temp\Subdir3" from 5) has looped through all subdirs of "C:\Temp\Subdir3"; loops through all files in "C:\Temp\Subdir3"; exits function call from 5)

9) Finally, the function has looped through all subdirs of C:\Temp"; loops through files in "C:\Temp"; exits initial function call; process ends.


I have tested this, and it does indeed find and rename files in directories that have no subdirectories.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Quite right, that's me not reading the code properly. Apologies. It's been a long week ...
 
Hey no problem, strongm. We all have "one of those days" every now and again!

Cheers!


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top