Renaming Folders and files after folder's name

Hi everyone,
Sorry if my question seems simple but I’m new to scripting.
The question is at the end of this post.
Here is my current workflow:
I have folders with this kind of name:
00002032
00002033
00002034
etc etc,
I remove the 4 leading zero using batch rename
then it looks like this:
2032
2033
2034

The files contains in the folders look like this:
000020340001
000020340002
000020340003

I then run this script to rename the files so now they look like this:
2034_01
2034_02
2034_03

NOTE: I actually select the folder that contains the subfolders and the script rename all files from all subfolders

tell application “Finder”
set a to every folder of (choose folder)
repeat with aa in a
set Base_Name to my MakeBase(aa as string)
set count_er to 1
set all_files to (every file in aa)
repeat with ff in all_files
set ff’s name to (Base_Name & “.” & (text -2 thru -1 of (“00” & (count_er as string))) & “.” & (ff’s name extension))
set count_er to count_er + 1
end repeat

end repeat

end tell

So my question is how to add something to this script to remove the 4 leading zeros to the folders name (rather than using batch rename?). The idea is to have only 1 script to do everything.

Thank you in advance!

I would try something like this:

tell application "Finder"
	set a to every folder of (choose folder)
	repeat with aa in a
		set aa to aa as alias
		set aa to[b] my [/b]RenameFolder(aa)
		set Base_Name to my MakeBase(aa as string)
		set count_er to 1
		set all_files to (every file in aa)
		repeat with ff in all_files
			set ff's name to (Base_Name & "." & (text -2 thru -1 of ("00" & (count_er as string))) & "." & (ff's name extension))
			set count_er to count_er + 1
		end repeat
		
	end repeat
end tell
on RenameFolder(aFolder)
	tell application "Finder"
		set folderName to name of aFolder
		set AppleScript's text item delimiters to {"0000"}
		set newName to text item 2 of folderName
		set the name of newFolder to newName
	end tell
	return aFolder
end RenameFolder

Thank you estockly,
I just ran it multiple times and it doesn’t work.
It says: Finder got an error: Can’t continue RenameFolder.

As the folders need to be renamed before the files I tried to put your script before the one that I already have (that rename the files after folders name). It doesn’t work either.

Hi. You should get accustomed to using descriptive variables from the start, as the alternative complicates matters. As long as the file names won’t be significantly longer than what you’ve exemplified, a simple way to do your rename task would be to coerce the names to a number, which would drop the leading zeroes.


tell application "Finder"
	repeat with aFolder in (get my (choose folder)'s folders)
		set aFolder's name to aFolder's name as number
		repeat with aFile in (get aFolder's files)
			set dispNom to aFile's displayed name
			set aFile's name to ((get dispNom as number) as text)'s text 1 thru 4 & "_" & dispNom's text -2 thru -1 & "." & aFile's name extension
		end repeat
	end repeat
end tell

–Edited to correct reference to alias

That’s because the call to that handler is in a Finder tell block. And Finder has no idea of what a handler is.
You need to say my RenameFolder(), just as in the next line.
Um… you do have a MakeBase handler?

estockly’s RenameFolder() has another problem: it does not store/restore text item delimiters, which could cause unexpected results later. Improved version below:

on RenameFolder(aFolder)
	tell application "Finder"
		set folderName to name of aFolder
		set {TIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "0000"}
		set newName to text item 2 of folderName
		set AppleScript's text item delimiters to TIDs
		set the name of newFolder to newName
	end tell
	return aFolder
end RenameFolder

You should get into the habit of always storing/restoring text item delimiters when using them.

My mistake. Since the handler call was inside a Finder tell block it needed the "my " before it.

I fixed above.

Thank you, I’m going to try it!

Thanks for your response!

Thank you! I’m going to try it!

I just tried it and had this error message:
error “Finder got an error: Expected a reference.” number -1727

I just tried it and have this error message when I try to compile it:
Expected end of line, etc. but found “my”.

I just wrote a note in bold to my initial post. I have the feeling the element I forgot to mention might have confused the persons who tried to help me.

This is doable via Finder, but if I were doing this I would use Shane Stanley’s FilemanagerLib library.

I can post a comparison if you like.

Oops. That’s what happens when you don’t test things. Replace
[format]repeat with aFolder in my (choose folder) [/format]–an alias
with:
[format]repeat with aFolder in (get my (choose folder)'s folders) [/format]–a Finder reference list