Insert hyphens in Mac Word document filenames

Hi. I've got about 700 Word documents on my Mac where the filename starts with a date (eg: 20260701). Is there a way to insert a hyphen into all of them to separate it into year, month and date pls (eg: 2026-07-01). Thanks.

Posted on Jul 27, 2026 2:30 AM

Reply
Question marked as Top-ranking reply

Posted on Aug 9, 2026 5:52 AM

Since several hundred users have clicked on this topic, I will post the AppleScript/Objective-C solution that one can run from Apple's Script Editor, or save that script as a clickable application. The script will prompt for a folder containing the Word documents, where it just looks for an 8-digit string of numbers in the file path and reformats it as yyyy-mm-dd. There is no verification code to confirm that this is a sane date, or perhaps a numeric folder name of 8 digits.


Copy/paste the following into Apple's Script Editor, then click the compile button, and then run.


(*

Word_Rename_wo_Finder.applescript

Prompts for folder containing Word documents needing to have their yyyymmdd or
yyyyddmm integer string in the filename changed to yyyy-mm-dd or yyyy-dd-mm
format. This is a recursive script that will drill into sub-folders looking
only for Word documents. Running it more than once will not change previously
renamed Word documents.

When done, script will display a dialog with renamed document tally.

Tested: macOS 26.6.1
VikingOSX, 2026-08-09, Apple Support Communities, no warranties expressed or implied.

*)

use framework "Foundation"
use AppleScript version "2.5"
use scripting additions

property ca : current application
property docIcon : "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MultipleItemsIcon.icns"
property allowedExts : {"doc", "docx"}
property found : 0 as integer
property renamed : 0 as integer

set nsm to ca's NSMutableString's new()
set fm to ca's NSFileManager's defaultManager()
set pred to ca's NSPredicate's predicateWithFormat_("self.pathExtension IN[c] %@", allowedExts)
-- set enumOptions to (current application's NSDirectoryEnumerationSkipsPackageDescendants as integer) + (current application's NSDirectoryEnumerationSkipsHiddenFiles as integer)
set enumOptions to 6
set theDir to POSIX path of (choose folder default location (path to desktop)) as «class furl»

set theFiles to ((fm's enumeratorAtURL:theDir includingPropertiesForKeys:{} options:enumOptions errorHandler:(missing value))'s allObjects())'s valueForKey:"path"
set wordDocs to theFiles's filteredArrayUsingPredicate:pred
set found to count of wordDocs

repeat with adoc in wordDocs
	(nsm's setString:adoc)
	set docRange to {0, nsm's |length|()}
	-- renamed date string is in ISO8601 format (yyyy-mm-dd). if you need
	-- yyyy-dd-mm just rearrange the $2 and $3 in the withString below
	(nsm's replaceOccurrencesOfString:"(\\d{4})(\\d{2})(\\d{2})" withString:"$1-$2-$3" options:(ca's NSRegularExpressionSearch) range:docRange)
	(fm's moveItemAtPath:adoc toPath:(nsm as text) |error|:(reference))
	set renamed to renamed + 1
end repeat
display dialog "Documents Found: " & found & return & "Renamed: " & renamed with title "Word Document Renaming" with icon POSIX file docIcon as alias
return

4 replies
Question marked as Top-ranking reply

Aug 9, 2026 5:52 AM in response to Lance-Chinnian

Since several hundred users have clicked on this topic, I will post the AppleScript/Objective-C solution that one can run from Apple's Script Editor, or save that script as a clickable application. The script will prompt for a folder containing the Word documents, where it just looks for an 8-digit string of numbers in the file path and reformats it as yyyy-mm-dd. There is no verification code to confirm that this is a sane date, or perhaps a numeric folder name of 8 digits.


Copy/paste the following into Apple's Script Editor, then click the compile button, and then run.


(*

Word_Rename_wo_Finder.applescript

Prompts for folder containing Word documents needing to have their yyyymmdd or
yyyyddmm integer string in the filename changed to yyyy-mm-dd or yyyy-dd-mm
format. This is a recursive script that will drill into sub-folders looking
only for Word documents. Running it more than once will not change previously
renamed Word documents.

When done, script will display a dialog with renamed document tally.

Tested: macOS 26.6.1
VikingOSX, 2026-08-09, Apple Support Communities, no warranties expressed or implied.

*)

use framework "Foundation"
use AppleScript version "2.5"
use scripting additions

property ca : current application
property docIcon : "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MultipleItemsIcon.icns"
property allowedExts : {"doc", "docx"}
property found : 0 as integer
property renamed : 0 as integer

set nsm to ca's NSMutableString's new()
set fm to ca's NSFileManager's defaultManager()
set pred to ca's NSPredicate's predicateWithFormat_("self.pathExtension IN[c] %@", allowedExts)
-- set enumOptions to (current application's NSDirectoryEnumerationSkipsPackageDescendants as integer) + (current application's NSDirectoryEnumerationSkipsHiddenFiles as integer)
set enumOptions to 6
set theDir to POSIX path of (choose folder default location (path to desktop)) as «class furl»

set theFiles to ((fm's enumeratorAtURL:theDir includingPropertiesForKeys:{} options:enumOptions errorHandler:(missing value))'s allObjects())'s valueForKey:"path"
set wordDocs to theFiles's filteredArrayUsingPredicate:pred
set found to count of wordDocs

repeat with adoc in wordDocs
	(nsm's setString:adoc)
	set docRange to {0, nsm's |length|()}
	-- renamed date string is in ISO8601 format (yyyy-mm-dd). if you need
	-- yyyy-dd-mm just rearrange the $2 and $3 in the withString below
	(nsm's replaceOccurrencesOfString:"(\\d{4})(\\d{2})(\\d{2})" withString:"$1-$2-$3" options:(ca's NSRegularExpressionSearch) range:docRange)
	(fm's moveItemAtPath:adoc toPath:(nsm as text) |error|:(reference))
	set renamed to renamed + 1
end repeat
display dialog "Documents Found: " & found & return & "Renamed: " & renamed with title "Word Document Renaming" with icon POSIX file docIcon as alias
return

Aug 10, 2026 11:34 AM in response to VikingOSX

My conscience got the better of me and I decided to correct the previous code post with an updated solution that only works on the specific filename and ignores its path components. This exonerates those that may use 8-digit folder names.

(*

Word_Rename_wo_Finder.applescript

Prompts for folder containing Word documents needing to have their yyyymmdd or
yyyyddmm integer string in the filename changed to yyyy-mm-dd or yyyy-dd-mm
format. This is a recursive script that will drill into sub-folders looking
only for Word documents. Running it more than once will not change previously
renamed Word documents.

When done, script will display a dialog with renamed document tally.

Version: 2
Tested: macOS 26.6.1
VikingOSX, 2026-08-10, Apple Support Communities, no warranties expressed or implied.

*)

use framework "Foundation"
use AppleScript version "2.5"
use scripting additions

property ca : current application
property docIcon : "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/MultipleItemsIcon.icns"
property allowedExts : {"doc", "docx"}
property found : 0 as integer
property renamed : 0 as integer

set nsm to ca's NSMutableString's new()
set fm to ca's NSFileManager's defaultManager()
set pred to ca's NSPredicate's predicateWithFormat_("self.pathExtension IN[c] %@", allowedExts)
-- set enumOptions to (current application's NSDirectoryEnumerationSkipsPackageDescendants as integer) + (current application's NSDirectoryEnumerationSkipsHiddenFiles as integer)
set enumOptions to 6
set theDir to POSIX path of (choose folder default location (path to desktop)) as «class furl»

set theFiles to ((fm's enumeratorAtURL:theDir includingPropertiesForKeys:{} options:enumOptions errorHandler:(missing value))'s allObjects())'s valueForKey:"path"
set wordDocs to theFiles's filteredArrayUsingPredicate:pred
set found to count of wordDocs

repeat with adoc in wordDocs
	set fpath to adoc's stringByDeletingLastPathComponent()
	set fname to adoc's lastPathComponent()
	(nsm's setString:fname)
	set docRange to {0, fname's |length|()}
	-- renamed date string is in ISO8601 format (yyyy-mm-dd). if you need
	-- yyyy-dd-mm just rearrange the $2 and $3 in the withString below
	(nsm's replaceOccurrencesOfString:"(\\d{4})(\\d{2})(\\d{2})" withString:"$1-$2-$3" options:(ca's NSRegularExpressionSearch) range:docRange)
	set fixedName to (fpath's stringByAppendingPathComponent:nsm)
	(fm's moveItemAtPath:adoc toPath:(fixedName) |error|:(reference))
	set renamed to renamed + 1
end repeat
display dialog "Documents Found: " & found & return & "Renamed: " & renamed with title "Word Document Renaming" with icon POSIX file docIcon as alias
return


Insert hyphens in Mac Word document filenames

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