Problems changing filenames in folder

I need some help. I have a folder with eps file in it. What I need to do is change the filename. All my files in the folder start with a B. Lets say there are 5 files in this folder they would be named as so.
Before
B2243WB324
B54325w895
B93893BH383
B74837mj236
B89423nm837

After
2243WB324
54325w895
93893BH383
74837mj236
89423nm837

I would like to rename all these files by removing the B at the beginning of the filename. And just resave over the old file in the same folder


Set SourceFolder to (Choose folder with Prompt)
tell application "Finder" to set filelist to (get the name of every file of folder SourceFolder whose file type = "EPSF") as list
try
 repeat with thisFileName in filelist
  set thisPath to (thisSubFolder as string) & ":" & thisFileName as string

So are you saying that there are already files present with the names that will be generated by removing the “b”? If so, the script will probably need to delete the original before the name change. This script (untested after a long, busy day) might get you started.

set SourceFolder to (choose folder with prompt "Choose a folder")
tell application "Finder"
	set files_ to (files of SourceFolder whose file type is "EPSF")
	repeat with file_ in files_
		set name_ to name of file_
		if name_ begins with "b" then
			set new_name to text 2 thru end of name_
			if exists file new_name of SourceFolder then
				delete file new_name of SourceFolder
			end if
			set name of file_ to new_name
		end if
	end repeat
end tell

– Rob

I didn’t explain my self very very well the first time. But the info you gave me was correct if I was going to use that method. What I have is a
Folder
Subfolder
Eps file
Subfolder
Eps File
Subfolder
Eps File
So what I have is a bunch of subfolders and inside them is a file. I need to rename the subfolder also without the “B” and the file inside the subfolder without the “B”.
2 thr end of name


set SourceFolder to (choose folder with prompt "Choose a folder")

tell application "Finder" to set FolderList to get the name of every folder of SourceFolder
repeat with thisFolder in FolderList
	
	set name_ to name of thisFolder
	if name_ begins with "b" then
		set new_name to text 2 thru end of name_
		set name of thisFolder to new_name
	end if
end repeat



repeat with thisFolder in FolderList
	set thisSubFolder to (SourceFolder as string) & thisFolder as string
	
	
	tell application "Finder" to set filelist to (get the name of every file of folder thisSubFolder whose file type = "EPSF") as list
	
	repeat with thisFileName in filelist
		--path of file in the subfolder
		
		
		set name_ to name of thisFileName
		if name_ begins with "b" then
			set new_name to text 2 thru end of name_
			if exists file new_name of SourceFolder then
				delete file new_name of SourceFolder
			end if
			set name of thisFileName to new_name
		end if
	end repeat
	
	
end repeat

Does your script work?

– Rob

No the subfolders don’t get renamed but the files do.

Does this work? It should obtain a list of all files whose name begins with “b” and whose type is EPSF. It should then rename each file and its containing folder. I have not tested the code so you might want to use duplicates before trying it on important files.

set source_ to choose folder

tell application "Finder"
	set epsf_files to files of entire contents of source_ whose file type is "EPSF" and name begins with "b"
	repeat with file_ in epsf_files
		set name_ to name of file_
		set newname_ to text 2 thru end of name_
		set container_ to container of file_
		if exists file newname_ of container_ then
			delete file newname_ of container_
			set name of file_ to newname_
		end if
		if name of container_ begins with "b" then
			set name of container_ to (text 2 thru end of (name of container_))
		end if
	end repeat
end tell

– Rob

It gets an execution errors at this part of the code.
Finder got an error: can’t set name of folder “b10652” of folder “Test” to text 2 thru end of name of folder “b10607” of folder “test”
No special OSAX are needed for this code right.


		if name of container_ begins with "b" then
			set name of container_ to (text 2 thru end of (name of container_))
		end if

The script should not require additional software to work. I’m writing the code in OS X so it’s likely a minor issue that I’ve overlooked. Does this line overcome the issue?

set name of container_ to ((characters 2 thru end of (get name of container_)) as string)

– Rob

Ok that works in changing the folder names but the file inside that folder doesn’t change.
So If we change all folder names first then change the filenames inside the subfolders.

Maybe this will work.

set source_ to choose folder

tell application "Finder"
	set epsf_files to files of entire contents of source_ whose file type is "EPSF" and name begins with "b"
	repeat with file_ in epsf_files
		set name_ to name of file_
		set newname_ to (characters 2 thru end of name_) as string
		set container_ to container of file_
		if exists file newname_ of container_ then
			delete file newname_ of container_
			set name of file_ to newname_
		end if
		if name of container_ begins with "b" then
			set name of container_ to ((characters 2 thru end of (get name of container_)) as string)
		end if
	end repeat
end tell

– Rob

No this did not work it only changed names of the folder.
Remeber that the files are inside a subfolder of the sourcefolder


   repeat with file_ in epsf_files 
      set name_ to name of file_ 
      set newname_ to (characters 2 thru end of name_) as string 
      set container_ to container of file_

Light testing indicates that this will work. The previous attempt failed because I made a stupid mistake which went undetected (by me) because I didn’t test the script.

set source_ to choose folder

tell application "Finder"
	set epsf_files to (files of entire contents of source_ whose file type is "EPSF" and name begins with "b") as alias list
	repeat with file_ in epsf_files
		set name_ to (get name of file_)
		set newname_ to (characters 2 thru end of name_) as string
		set container_ to (container of file_) as alias
		if exists file newname_ of container_ then
			delete file newname_ of container_
		end if
		set name of file_ to newname_
		if name of container_ begins with "b" then
			set name of container_ to ((characters 2 thru end of (get name of container_)) as string)
		end if
	end repeat
end tell

– Rob