Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

How to batch remove carriage returns or line feed in file names

Is there any way to use the rename multiple items function in the finder to fix hundreds of file names that have a carriage return in them? They show up as a ? in the file name but if I tell it to replace ? with either a blank character or _ for example I get an error that the name can't be used. It looks like I can use A Better Finder Rename but I was hoping for a free alternative and now I'm curious how it would be done.


The reason I think it's a carriage return in the file name is that if I drag it to a web browser it displays the character as %0D which is either that or a line feed.

Posted on Aug 18, 2021 10:33 AM

Reply
Question marked as Best reply

Posted on Aug 19, 2021 4:29 AM

The following Automator solution will just prompt you for the folder containing the filenames with embedded carriage returns in them. It will only process those particular files, replacing the carriage return occurrences with an underline. Finally, it renames the original filename to the new name. This example does not drill down into sub-folders of the provided parent folder.


Launch Automator from your Applications folder.


It will open a dialog with New Document offered. Click that, and then from the array of Automator types, choose Application, and then click the Choose button.


You will now see a left panel with various Automator libraries containing specific actions, and a large workflow window to the right of that Library panel. Drag and drop the following Library actions onto the larger workflow window in top down order:

  • Library > Files & Folders > Ask for Finder Items
    • Start at: Desktop
    • Type: Folders
    • Allow Multiple Selection is not selected
  • Library > Utilities > Run AppleScript


Looks like this:


In the Run AppleScript action, select all of the default text in it and remove it. It will be replaced with the following code that you copy/paste into that action:


use framework "Foundation"
use AppleScript version "2.4" -- Yosemite or later
use scripting additions

property NSString : a reference to current application's NSString
property fixChar : "_"

on run {input, parameters}
	
	set theFolder to input as text
	with timeout of 1800 seconds
		tell application "Finder"
			set name_list to (every item in folder theFolder whose name contains return) as alias list
		end tell
	end timeout
	
	if name_list = {} then return
	
	tell application "Finder"
		repeat with afile in name_list
			set z to my replace_CR(afile, fixChar)
			set name of afile to my basename(z)
		end repeat
	end tell
	return
end run

on replace_CR(afile, repChar)
	# replace all carriage returns in the filename with a designated repChar character
	return ((NSString's stringWithString:(POSIX path of afile))'s stringByReplacingOccurrencesOfString:return withString:repChar) as text
end replace_CR

on basename(afile)
	# just return the filename without any path
	return ((NSString's stringWithString:(POSIX path of afile))'s lastPathComponent()) as text
end basename


Looks like this:


Once you have done that, click the hammer icon in the Run AppleScript action to compile the AppleScript. At this point, you save the Automator application to your Desktop as (e.g. no_CR.app). and then quit Automator. You double-click this just saved application to run it for the prompt to your first folder. Verify that your filenames are fixed, and then run the app again providing your other folder with files to be fixed.


I just tested this on Mojave 10.14.6 as that was what was in my bedroom before daylight. I will test it on macOS 11.5.2 in a few minutes. I will be traveling mid-morning to mid-afternoon today, so won't be responding to any posts until later.

Similar questions

10 replies
Question marked as Best reply

Aug 19, 2021 4:29 AM in response to AJ 2010

The following Automator solution will just prompt you for the folder containing the filenames with embedded carriage returns in them. It will only process those particular files, replacing the carriage return occurrences with an underline. Finally, it renames the original filename to the new name. This example does not drill down into sub-folders of the provided parent folder.


Launch Automator from your Applications folder.


It will open a dialog with New Document offered. Click that, and then from the array of Automator types, choose Application, and then click the Choose button.


You will now see a left panel with various Automator libraries containing specific actions, and a large workflow window to the right of that Library panel. Drag and drop the following Library actions onto the larger workflow window in top down order:

  • Library > Files & Folders > Ask for Finder Items
    • Start at: Desktop
    • Type: Folders
    • Allow Multiple Selection is not selected
  • Library > Utilities > Run AppleScript


Looks like this:


In the Run AppleScript action, select all of the default text in it and remove it. It will be replaced with the following code that you copy/paste into that action:


use framework "Foundation"
use AppleScript version "2.4" -- Yosemite or later
use scripting additions

property NSString : a reference to current application's NSString
property fixChar : "_"

on run {input, parameters}
	
	set theFolder to input as text
	with timeout of 1800 seconds
		tell application "Finder"
			set name_list to (every item in folder theFolder whose name contains return) as alias list
		end tell
	end timeout
	
	if name_list = {} then return
	
	tell application "Finder"
		repeat with afile in name_list
			set z to my replace_CR(afile, fixChar)
			set name of afile to my basename(z)
		end repeat
	end tell
	return
end run

on replace_CR(afile, repChar)
	# replace all carriage returns in the filename with a designated repChar character
	return ((NSString's stringWithString:(POSIX path of afile))'s stringByReplacingOccurrencesOfString:return withString:repChar) as text
end replace_CR

on basename(afile)
	# just return the filename without any path
	return ((NSString's stringWithString:(POSIX path of afile))'s lastPathComponent()) as text
end basename


Looks like this:


Once you have done that, click the hammer icon in the Run AppleScript action to compile the AppleScript. At this point, you save the Automator application to your Desktop as (e.g. no_CR.app). and then quit Automator. You double-click this just saved application to run it for the prompt to your first folder. Verify that your filenames are fixed, and then run the app again providing your other folder with files to be fixed.


I just tested this on Mojave 10.14.6 as that was what was in my bedroom before daylight. I will test it on macOS 11.5.2 in a few minutes. I will be traveling mid-morning to mid-afternoon today, so won't be responding to any posts until later.

Aug 18, 2021 1:40 PM in response to AJ 2010

The hex '0D' is the carriage return character, and no, the Finder Rename cannot fix these trailing characters in the filename. What types of files are these? Are the files all in the same folder, or in a hierarchy of sub-folders?


I can probably write a short Automator tool (script) that can detect which filenames end with that character and fix them. I just tested this on a file that I created here and was able to remove the trailing carriage return from the filename. Shouldn't be much code to resolve this.

Aug 18, 2021 1:57 PM in response to VikingOSX

The file names are like this- B002_CLIENTA_JACKSON_WARM?B002_CLIENTA_JACKSON_WARM?B002_CLIENTA_JACKSON_COOL__0004.jpg and the ? is where the carriage return is in the name so from what I've seen, each file name has two of them.


There are two different folders that have the file name issue but all files are located in those two folders.

Aug 18, 2021 4:25 PM in response to AJ 2010

I have never encountered filenames with carriage return characters embedded in the name, only at the end of the filename. Are you suggesting that the single filename pattern is like the following:


B002_CLIENTA_JACKSON_WARM?B002_CLIENTA_JACKSON_WARM?B002_CLIENTA_JACKSON_COOL__0004.jpg


The only means to repair these filenames will be using AppleScript, as the UNIX shell abhors carriage returns in filenames and cannot rename them. What character is to replace the carriage return in the filename?

Aug 18, 2021 5:16 PM in response to AJ 2010

I have working code right now that renames that behemoth filename with carriage returns in it to one without them. Not much effort to whip together an Automator solution where you can select the containing folder and the script does the renaming of the problematic filenames within it. Unless you really want to pay money for A Better Finder Rename, then the Automator solution is free.

Aug 19, 2021 7:50 AM in response to AJ 2010

You are welcome.


I also tested it on macOS 11.5.2 before I left the house this morning it worked there too. What I did notice and will fix later today is if there is a filename that ends with a carriage return, as currently written, the Automator workflow will add an underscore in that last position (e.g. foo.txt? ➝ foo.txt_) when it should just strip the carriage return from the end of the filename.

Aug 19, 2021 3:27 PM in response to VikingOSX

In the code that I posted earlier, there was an issue with the on replace_CR handler where it would dutifully replace carriage returns in the filename with an underscore, yet if the filename ended with a carriage return, it would result in file.ext? becoming file.ext_. That would be undesirable.


One should add the following property statement to the previous script:

property NSCharacterSet : a reference to current application's NSCharacterSet


and replace the original replace_CR handler with the following:

on replace_CR(afile, repChar)
	set str to NSString's stringWithString:(POSIX path of afile as text)
	# remove any leading/trailing newlines or carriage returns from the filename
	set newStr to (str's stringByTrimmingCharactersInSet:(NSCharacterSet's newlineCharacterSet))
	# replace all remaining carriage returns in the filename with a designated repChar character
	return (newStr's stringByReplacingOccurrencesOfString:return withString:repChar) as text
end replace_CR



How to batch remove carriage returns or line feed in file names

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.