How to delete specific characters in the file names in a folder

How to write a script that deletes a series of characters in all the file names found in a specific folder? Is it easy? Does one exist?

Here is an example of a file name: 206 HackerN_Star Power.jpg I want to be able to delete starting with the 5th character and ending with the _ character. It would be fine to replace the old file name with the newly created one which would be 206 Star Power.jpg I of course know how to do this by selecting and deleting the selected characters for each file in the folder; however, there are so many I wanted to automate the process.

Model: MacBook Pro
AppleScript: 2.2.3
Browser: Safari
Operating System: Mac OS X (10.8)

Hi,

first of all: Welcome to MacScripter

second of all: Please post questions and requests in the appropriate forum, Code Exchange is for sharing code

simple solution, it asks for a folder and renames all files in this folder which contains at least one underscore character.


set baseFolder to choose folder
tell application "Finder" to set theFiles to files of baseFolder
repeat with aFile in theFiles
	set fileName to name of aFile
	set underscoreOffset to offset of "_" in fileName
	if underscoreOffset > 0 then
		tell fileName to set newFileName to text 1 thru 4 & text (underscoreOffset + 1) thru -1
		tell application "Finder" to set name of contents of aFile to newFileName
	end if
end repeat


Sorry for the wrong post location. As you can see I am a newbie. Thanks for the script! Regards