Sunday, October 17, 2021

#1 2021-10-12 11:34:11 pm

KyleC
Member
Registered: 2021-10-08
Posts: 1

Delete duplicate text/words from file names

Hi all,

I hope someone can help me with this. I would really, really appreciate it.

I have 1000’s of files that have duplicate text in the file name and I’m trying to figure out a script to delete any duplicate word or text (please see below)

Before:

TOR_0023369_Tec - SPC_Tec - SPC.mov 
TOR_0023369_IIC - ROK_IIC - ROK.mov
TOR_0023369_D&M - OPT_D&M - OPT.mov
TOR_0023369_SET - LNG_SET - LNG.mov

After

TOR_0023369_Tec - SPC.mov 
TOR_0023369_IIC - ROK.mov
TOR_0023369_D&M - OPT.mov
TOR_0023369_SET - LNG.mov

Any help will be greatly appreciated.

Thank you!

Offline

 

#2 2021-10-13 12:55:42 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2079

Re: Delete duplicate text/words from file names

Hi,

Create on your desktop new empty folder, duplicate to it 2-3 of your files. Test following script:

Applescript:

set folderHFS to (choose folder) as text
set ATID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"_", "-", "."}

tell application "System Events"
   
   repeat with aFile in (get files of folder folderHFS)
       set fileName to name of aFile
       -- try block required to change only the names which confirm to your template
       try
           set {A, B, C, D, E, F, G} to text items of fileName
           set newName to A & "_" & B & "_" & C & "-" & D
           if newName does not contain E then set newName to newName & "_" & E
           if newName does not contain F then set newName to newName & "-" & F
           set newName to newName & "." & G
           set name of aFile to newName
       end try
   end repeat
   
end tell

set AppleScript's text item delimiters to ATID

Last edited by KniazidisR (2021-10-14 05:29:49 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#3 2021-10-13 11:13:52 am

wch1zpink
Member
Registered: 2011-08-20
Posts: 61

Re: Delete duplicate text/words from file names

Applescript:

activate
set theFiles to (choose file with prompt "Choose Files" with multiple selections allowed)

tell application "System Events"
   repeat with i in theFiles
       set oldName to name of i
       tell current application to set newName to ¬
           (text 1 thru (offset of "- " in oldName) of oldName) & ¬
           text -1 thru -8 of oldName
       set name of i to newName
   end repeat
end tell

Offline

 

#4 2021-10-13 07:38:43 pm

wch1zpink
Member
Registered: 2011-08-20
Posts: 61

Re: Delete duplicate text/words from file names

KniazidisR wrote:

repeat with aFile in (files of folder folderHFS)


@KniazidisR Using macOS Big Sur, the above line of your code throws an error for me.  This minor edit solves the problem…

Applescript:

repeat with aFile in (get files of folder folderHFS)

Offline

 

#5 2021-10-14 04:22:26 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2079

Re: Delete duplicate text/words from file names

I will not be able to install Big Sur on my Macbook Pro. Actually, the parenthesis immediately following the "in" keyword should be interpreted by AppleScript as "(get" implicitly. The fact that Big Sur doesn't do this means a change to this old rule. But thanks anyway for the info. I updated the script.


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#6 2021-10-14 05:25:24 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5427

Re: Delete duplicate text/words from file names

KniazidisR wrote:

Actually, the parenthesis immediately following the "in" keyword should be interpreted by AppleScript as "(get" implicitly.


Hi.

I get an error in Mojave too: error "System Events got an error: AppleEvent handler failed." number -10000.

The parenthesis doesn't make any difference in this sort of repeat. The repeat is still through items in an application. It seems that System Events doesn't like being treated like this. The Finder doesn't seem to mind, but it's giving me some strange results this morning.  hmm

When repeating with objects in an application, it's usually safer and more efficient to fetch the objects from the application explicitly and then work through the resulting list, rather than index-referencing the objects in the application itself.


NG

Offline

 

#7 2021-10-14 06:40:05 pm

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1081

Re: Delete duplicate text/words from file names

Just for learning purposes, I wrote an ASObjC suggestion. It wasn't clear to me if all of the files are in one folder or not, so my script works recursively. I understand that many will find the script to be unacceptably long, and I post it FWIW. Using an AppleScript to rename thousands of files can be risky; it's important that the OP have a completely reliable backup of all files.

Applescript:

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

on main()
   set theFolder to POSIX path of (choose folder)
   set theFiles to getFiles(theFolder, {"mov"})
   
   repeat with aFile in theFiles
       set aFile to (current application's NSString's stringWithString:(POSIX path of aFile))
       set filePath to aFile's stringByDeletingLastPathComponent()
       set fileName to (aFile's lastPathComponent())'s stringByDeletingPathExtension()
       set fileNameList to (fileName's componentsSeparatedByString:"_") as list
       if (count fileNameList) = 4 then
           if item 3 of fileNameList = item 4 of fileNameList then
               set newFileName to current application's NSString's stringWithFormat_("%@_%@_%@.mov", item 1 of fileNameList, item 2 of fileNameList, item 3 of fileNameList)
               renameFile(aFile, newFileName)
           end if
       end if
   end repeat
end main

on getFiles(theFolder, theFileExtensions)
   set fileManager to current application's NSFileManager's defaultManager()
   set theFolder to current application's |NSURL|'s fileURLWithPath:theFolder
   set searchOptions to (current application's NSDirectoryEnumerationSkipsPackageDescendants as integer) + (current application's NSDirectoryEnumerationSkipsHiddenFiles as integer)
   set theFiles to (fileManager's enumeratorAtURL:theFolder includingPropertiesForKeys:{} options:searchOptions errorHandler:(missing value))'s allObjects()
   set searchPredicate to current application's NSPredicate's predicateWithFormat_("pathExtension.lowercaseString IN %@", theFileExtensions)
   return ((theFiles's filteredArrayUsingPredicate:searchPredicate) as list)
end getFiles

on renameFile(theFile, newName)
   set fileManager to current application's NSFileManager's defaultManager()
   set theNewPath to theFile's stringByDeletingLastPathComponent()'s stringByAppendingPathComponent:newName
   if fileManager's fileExistsAtPath:theNewPath then
       display dialog quote & (newName as text) & quote & " already exists and will be skipped" with icon stop
   else
       fileManager's moveItemAtPath:theFile toPath:theNewPath |error|:(missing value)
   end if
end renameFile

main()

Last edited by peavine (2021-10-14 06:44:16 pm)


2018 Mac mini - macOS Catalina - Script Debugger 8

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)