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.