get list from csv/txt file and batch rename.

I have a list in the form of Txt file with the current file name in Column A and the name to be renamed to in B
I need to rename the images in folder X with the list above. I also need to add a rule in that it reads only the first 13 digits of the files in folder X (some are labeled 123456789ABCD and some like this 123456789ABCD_2)

This would need to be renamed as SanFranciso and SanFranciso_2. basically everything after the 13 digit needs to be carried over from the original name.

Currently I have this system running as Macro in excel but its only slow as it has to open excel! I also want to turn it into a watch folder eventually so that it can ‘just happen in the background’ Also I am aware A better finder renamer 8 is an option, but this too is slow (needs to happen quietly in the background)

(The excel sheet looks like this)
A B C D
OriginalName NewName =IF(ISNA(VLOOKUP(C1,A2:B:B,2,FALSE)),“”,VLOOKUP(C1,A2:B:B,2,FALSE))

And the macro looks like this.

Sub snb()
Dim c00 As String, c01 As String
c00 = “Macintosh HD:Users:user:Desktop:” ‘here you can change the directory to suit your needs.
c01 = Dir(c00)
Do While c01 <> “”
Range(“C1”) = "’" & Left(c01, 13)
If Range(“D1”) <> “” Then
Name c00 & c01 As c00 & Range(“D1”) & Right(c01, Len(c01) - 13)
End If
c01 = Dir
Loop
End Sub

I hope this makes sense.

MATT

hi,

what about the suffix¿
is it part of the csv-List?

Hi

assuming that the name extension is not part of the name in the csv-list.

The *csv should look like this:

This is a folderAction-Script.
The csv is read every time a file is added to the folder.
If the Csv doesn’t change often or it is quite large we should think of storing its contents instead of reading it each time …

Hope it’ll work :slight_smile:

property csvAlias : alias ((path to desktop as text) & "my.csv") --path to your csv-File as alias
property csvDelimiter : ";" --delimiter used by your csv-File

on adding folder items to this_folder after receiving itemList
	try
		set TID to AppleScript's text item delimiters
		
		repeat with i from 1 to count of itemList
			set newFile to item i of itemList
			
			
			tell application "System Events" to set {fSuffix, fName} to {(name extension of disk item (newFile as text)), (displayed name of disk item (newFile as text))}
			
			if fSuffix is not "" then set fSuffix to "." & fSuffix
			set fName to my stripSuffix(fSuffix, fName)
						
			set csvText to read csvAlias
			
			if csvText contains fName then
				set AppleScript's text item delimiters to fName & csvDelimiter
				
				set newListName to (text 1 thru -1 of (paragraph 1 of (text item 2 of csvText)))
				
				if (count of fName) is greater than 13 then
					set newName to newListName & (text 14 thru -1 of fName) & fSuffix
				else
					set newName to newListName & fSuffix
				end if
				my renameFile(this_folder, fName & fSuffix, newName)
			end if
		end repeat
		
		set AppleScript's text item delimiters to TID
		
	on error e
		display dialog e
	end try
end adding folder items to

on stripSuffix(fSuffix, fName)
	if fName contains fSuffix then
		set AppleScript's text item delimiters to fSuffix
		return (text item 1 of fName)
	end if
end stripSuffix

on renameFile(this_folder, origName, newName)
	set sourcePath to quoted form of POSIX path of ((this_folder as text) & origName)
	set targetPath to quoted form of POSIX path of ((this_folder as text) & newName)
	do shell script "mv -f " & sourcePath & space & targetPath
end renameFile

Tested under Lion 10.7.3

Excellent i’ll give that a go tomorrow!

In terms of the csv file its as you mentioned it. its actually listing the barcode and the sku, the item sometimes has more than one image and so I add my own characters after the number that then need to be carried across and they also can be jpg’s or tif or PSD.

5012345678913.jpg should become MA66TT64528MU4MM.jog

or

5012345678913_2.jpg > MA66TT64528MU4MM_2.jpg
5012345678913_N.psd > MA66TT64528MU4MM_N.psd

Basically everything after the 13th number needs to be left alone.

In terms of the file size of the CSV it currently comes in at 2mb. it has about 65000 possible barcodes to ski’s

I’ll have a look to see how quick it takes place, but might sound a good idea to keep it loaded.

Thanks for your help i’ll check it out in the morning and let you know how it goes.

Matt

Struggling a little at the moment, currently it doesn’t appear to do anything.

How can I modify it so that I tell it the folder to use rather than setting it as a folder action as I will eventually use it to run in the launchD. That way it should still my front most action and run in the background.?

Partly working it names about 2 items then I get the prompt.

Can’t make «class docf≫ “5052411856597.psd” of «class cfol» “matthew” of «class cfol» “Users” of «class sdsk» of application “Finder” into type text.

property csvAlias : alias ((path to desktop as text) & "ean2sku.csv") --path to your csv-File as alias
property csvDelimiter : "," --delimiter used by your csv-File

tell application "Finder"
	set this_folder to folder "Hotfolder" as alias
	set itemList to every file in this_folder
end tell


try
	set TID to AppleScript's text item delimiters
	
	repeat with i from 1 to count of itemList
		set newFile to item i of itemList
		
		
		tell application "System Events" to set {fSuffix, fName} to {(name extension of disk item (newFile as text)), (displayed name of disk item (newFile as text))}
		
		if fSuffix is not "" then set fSuffix to "." & fSuffix
		set fName to my stripSuffix(fSuffix, fName)
		
		set csvText to read csvAlias
		
		if csvText contains fName then
			set AppleScript's text item delimiters to fName & csvDelimiter
			
			set newListName to (text 1 thru -1 of (paragraph 1 of (text item 2 of csvText)))
			
			if (count of fName) is greater than 13 then
				set newName to newListName & (text 14 thru -1 of fName) & fSuffix
			else
				set newName to newListName & fSuffix
			end if
			my renameFile(this_folder, fName & fSuffix, newName)
		end if
	end repeat
	
	set AppleScript's text item delimiters to TID
	
on error e
	display dialog e
end try


on stripSuffix(fSuffix, fName)
	if fName contains fSuffix then
		set AppleScript's text item delimiters to fSuffix
		return (text item 1 of fName)
	end if
end stripSuffix

on renameFile(this_folder, origName, newName)
	set sourcePath to quoted form of POSIX path of ((this_folder as text) & origName)
	set targetPath to quoted form of POSIX path of ((this_folder as text) & newName)
	do shell script "mv -f " & sourcePath & space & targetPath
end renameFile

I’ve uploaded a sample of what i’m working with

https://www.wetransfer.com/dl/9CW06JI6/b7dff6e1769b878d67582ca1772af27644e7dc62c2694eb72b64706a9878c7f9ea5a3518282ed12

Matt

Hi,

didn’t mention that fileNames of originals in the csv are cut of after the 13th character …

Well, after changing this in my folderActionScript it works like charm with your data on my system!

property csvAlias : alias ((path to desktop as text) & "SourceFiles:ean2sku.csv") --path to your csv-File as alias
property csvDelimiter : "," --delimiter used by your csv-File

on adding folder items to this_folder after receiving itemList
	try
		set TID to AppleScript's text item delimiters
		
		repeat with i from 1 to count of itemList
			set newFile to item i of itemList
			
			
			tell application "System Events" to set {fSuffix, fName} to {(name extension of disk item (newFile as text)), (displayed name of disk item (newFile as text))}
			
			if fSuffix is not "" then set fSuffix to "." & fSuffix
			set fName to my stripSuffix(fSuffix, fName)
			set tmpName to (text 1 thru 13 of fName)
			
			set csvText to read csvAlias
			
			if csvText contains tmpName then
				set AppleScript's text item delimiters to tmpName & csvDelimiter
				
				set newListName to (text 1 thru -1 of (paragraph 1 of (text item 2 of csvText)))
				
				if (count of fName) is greater than 13 then
					set newName to newListName & (text 14 thru -1 of fName) & fSuffix
				else
					set newName to newListName & fSuffix
				end if
				my renameFile(this_folder, fName & fSuffix, newName)
			end if
		end repeat
		
		set AppleScript's text item delimiters to TID
		
	on error
		display dialog "Could not rename: " & fName giving up after 3
	end try
end adding folder items to

on stripSuffix(fSuffix, fName)
	if fName contains fSuffix then
		set AppleScript's text item delimiters to fSuffix
		return (text item 1 of fName)
	end if
end stripSuffix

on renameFile(this_folder, origName, newName)
	set sourcePath to quoted form of POSIX path of ((this_folder as text) & origName)
	set targetPath to quoted form of POSIX path of ((this_folder as text) & newName)
	do shell script "mv -f " & sourcePath & space & targetPath
end renameFile

If you want to launch the script regulary by system you may use this one.
The renamed files are stored in a destFolder.

property csvAlias : alias ((path to desktop as text) & "SourceFiles:ean2sku.csv")
property hotFolder : alias ((path to desktop as text) & "SourceFiles:HotFolder:")
property destFolder : alias ((path to desktop as text) & "SourceFiles:destFolder:")

property csvDelimiter : "," --delimiter used by your csv-File

set TID to AppleScript's text item delimiters

set fileNames to list folder hotFolder without invisibles

repeat with i from 1 to count of fileNames
	try
		set fName to item i of fileNames
		
		set filePath to (hotFolder as text) & fName
		
		tell application "System Events" to set fSuffix to name extension of disk item filePath
		
		if fSuffix is not "" then set fSuffix to "." & fSuffix
		set fName to my stripSuffix(fSuffix, fName)
		set tmpName to (text 1 thru 13 of fName)
		
		set csvText to read csvAlias
		
		if csvText contains tmpName then
			set AppleScript's text item delimiters to tmpName & csvDelimiter
			
			set newListName to (text 1 thru -1 of (paragraph 1 of (text item 2 of csvText)))
			
			if (count of fName) is greater than 13 then
				set newName to newListName & (text 14 thru -1 of fName) & fSuffix
			else
				set newName to newListName & fSuffix
			end if
			set destFilePath to ((destFolder as text) & newName)
			my renameFile(filePath, destFilePath)
		end if
	on error
		display dialog "Could not rename: " & fName giving up after 3
	end try
end repeat

set AppleScript's text item delimiters to TID


on stripSuffix(fSuffix, fName)
	if fName contains fSuffix then
		set AppleScript's text item delimiters to fSuffix
		return (text item 1 of fName)
	end if
end stripSuffix

on renameFile(filePath, destFilePath)
	set sourcePath to quoted form of POSIX path of filePath
	set targetPath to quoted form of POSIX path of destFilePath
	do shell script "mv -f " & sourcePath & space & targetPath
end renameFile

Excellent it all seams to be running ok.

I’ll let you know if I get any more unusual variables, but otherwise looks like its working!!

Thank you very much!

Matt