Batch rename files replacing periods with hyphens excluding the period in the file extension

Hi,


I digitally draw and have assigned version numbers to the many different versions of a design (e.g. 'Bob 5.4.2.1.ai'). Over my various compositions, there are hundreds of versions.


To reduce compatibility complications, I want to batch rename the files, replacing periods with hyphens but finder keeps changing the period before the extension to a hyphen even if extensions are hidden.


Is there a way of excluding the period before the extension or would I have to get third party software. If the latter, do you have any recommendations?


I am currently on macOS Sequoia 15.7.1.


Thanks for any suggestions!

Mac mini, macOS 15.7

Posted on Nov 11, 2025 10:24 PM

Reply
Question marked as Top-ranking reply

Posted on Nov 12, 2025 3:27 AM

Here is a quick and dirty Python script that does what you want.

See instructions on how to use below. The code is pretty straightfor


import os
from glob import glob

os.chdir("/Users/user/test")  #change this to the path to the directory with files to rename
for file in glob("*.ai"): #change ai to another extension, or use * to apply to all extensions
    separator = file.rfind(".")
    name, extension = file[:separator], file[separator:]
    name = name.replace(".","-")
    os.rename(file,name+extension)


To use, first replace "/Users/user/test" with the correct path for your files.

The above example applies to files with extension .ai in the selected folder, but you can change it to another extension, say, .pdf, or use "*.*" for files with any extension.


Save the contents to a file, like "replacedots.py".

To run the code,


1) open a Terminal window and type


"python "


WITH a SPACE at the end; DO NOT press enter


2) Drag the replacedots.py file over the Terminal window


3) Press enter


Done.

14 replies
Question marked as Top-ranking reply

Nov 12, 2025 3:27 AM in response to AglaVerin

Here is a quick and dirty Python script that does what you want.

See instructions on how to use below. The code is pretty straightfor


import os
from glob import glob

os.chdir("/Users/user/test")  #change this to the path to the directory with files to rename
for file in glob("*.ai"): #change ai to another extension, or use * to apply to all extensions
    separator = file.rfind(".")
    name, extension = file[:separator], file[separator:]
    name = name.replace(".","-")
    os.rename(file,name+extension)


To use, first replace "/Users/user/test" with the correct path for your files.

The above example applies to files with extension .ai in the selected folder, but you can change it to another extension, say, .pdf, or use "*.*" for files with any extension.


Save the contents to a file, like "replacedots.py".

To run the code,


1) open a Terminal window and type


"python "


WITH a SPACE at the end; DO NOT press enter


2) Drag the replacedots.py file over the Terminal window


3) Press enter


Done.

Nov 12, 2025 9:10 AM in response to VikingOSX

You can even do this briskly in the Terminal, once you have changed directory to the folder containing these multi-dot filenames and using the Zsh shell:

cd ~/Desktop/Test
autoload -U zmv
zmv '(*).(*)' '${1//./-}.$2'


That renames any multi-dot filename, regardless of the extension, to a filename replacing the dots with a dash and the original extension.

Nov 12, 2025 2:15 PM in response to Luis Sequeira1

And here is the Ruby version that works with the System Ruby 2.6.10p210 (this example) or the latest Ruby 3.4.8:

#!/usr/bin/ruby
# frozen_string_literal: false

# Usage: ./notdot.rb ~/Desktop/Test

require 'pathname'
require 'fileutils'

f = File.expand_path(ARGF.filename)

Pathname.glob(File.join(f, '*.{ai,psd}')).each do |z|
  next unless FileTest.size?(z.to_path)

  zpath = z.to_path
  outfile = zpath.split('.')[0..-2].join('-') + z.extname
  FileUtils.mv(zpath, outfile, verbose: true)
end
exit


One simply changes the she-bang to #!/usr/bin/env ruby if using a privately installed Ruby 3.4.8 (as I do). One runs it as follows:

chmod +x ./nodot.rb
./nodot.rb ~/Desktop/Test


and the output will look like this for those non-empty target files:

mv /Users/viking/Desktop/Test/Baz_2.3.5.1.ai /Users/viking/Desktop/Test/Baz_2-3-5-1.ai
mv /Users/viking/Desktop/Test/Foo_1.2.3.4.psd /Users/viking/Desktop/Test/Foo_1-2-3-4.psd


Tested: Ruby 2.6.10, 3.4.8 and macOS Tahoe 26.1

Nov 19, 2025 6:28 PM in response to AglaVerin

AglaVerin wrote:


VikingOSX wrote:

You can even do this briskly in the Terminal, once you have changed directory to the folder containing these multi-dot filenames and using the Zsh shell:
cd ~/Desktop/Test
autoload -U zmv
zmv '(*).(*)' '${1//./-}.$2'

That renames any multi-dot filename, regardless of the extension, to a filename replacing the dots with a dash and the original extension.

Thank you for all these lines of codes. I'm unfamiliar with Python, do all these various workarounds you've offered require downloading Python in some form or do some only require Terminal / ready built in apps? Thanks!

@VikingOSX's code snippet in your quote here is actually for Zsh.


All of @VikingOSX's code in this thread so far requires no outside resources.....it can all be run with a default macOS installation since it utilizes Zsh commands or Ruby commands...all available with a default macOS installation.


Now, @Luis Sequeira1's Python code does require downloading outside resources since macOS no longer ships with Python. If you want to use Python, then it is best to download Python directly from the source/developer since Apple's XCode Command Line Utilities tend to use outdated versions of Python and it can be tricky updating the XCode Command Line Utilities.

https://www.python.org/


Unless you are going to be using Python for other things, using either of @VikingOSX's two options (or three options I guess) would be the better choice only because you don't need to install any extra items. All things being equal, I always prefer to minimize the amount of third party software I install on my devices.

Nov 20, 2025 5:04 AM in response to AglaVerin

All of my code that I provided is run in the Terminal. Two of my code solutions require the Zsh shell which is already installed by macOS. For simplicity, I would just use the Zsh shell which precludes any programmer's editor requirement to preserve indent structure of Python or Ruby, or the actual download/installation of Python 3 from Python.org.


Nov 12, 2025 6:02 AM in response to AglaVerin

Since Apple stopped including Python in the operating system after Monterey 12.3.1, you can avoid installing Python 3.14 from Python.org. You can use this Zsh script even if you continue to use the Bash shell. This script only takes one argument on the Terminal command line, the path to the folder to process.


#!/bin/zsh

:<<"COMMENT"
A script to recursively (if relevant) rename all files in a designated folder
hierarchy from the format Bob 5.4.2.1.ai to Bob 5-4-2-1.ai.

Usage: chmod +x ./nodot.zsh
       ./nodot.zsh /Users/USERNAME/Desktop/Test
       ./nodot.zsh ~/Desktop/Test

Tested: Zsh 5.9, macOS 26.1

COMMENT

STARTDIR="${1:a:s/\~\//}"
[[ $# = 1 ]] || {echo "You need to specify the directory name to process"; exit 1}

# file extension pattern to process
PATTERN="ai|psd"

# force case-insenstive filenames and extensions
setopt nocaseglob

for f in ${STARTDIR}/**/*.${~PATTERN}(.N);
do
    mv "${f}" "${${f:r}//\./-}.${f:e}"
done
echo "Done"
exit 0


Nov 12, 2025 10:13 AM in response to VikingOSX

VikingOSX wrote:

Since Apple stopped including Python in the operating system after Monterey 12.3.1, you can avoid installing Python 3.14 from Python.org. You can use this Zsh script even if you continue to use the Bash shell. This script only takes one argument on the Terminal command line, the path to the folder to process.


That is a great script, and using only the built-in shell.

I do agree that your solution is better and more comprehensive than my admittedly quick hack.


Just a FWIW: Apple does include an old version of python (3.9), at least I have it at /usr/bin/python3 on my mac running Tahoe. So one would write the command as python3 instead of python (or add an alias).



Nov 19, 2025 12:10 PM in response to VikingOSX

VikingOSX wrote:

You can even do this briskly in the Terminal, once you have changed directory to the folder containing these multi-dot filenames and using the Zsh shell:
cd ~/Desktop/Test
autoload -U zmv
zmv '(*).(*)' '${1//./-}.$2'

That renames any multi-dot filename, regardless of the extension, to a filename replacing the dots with a dash and the original extension.


Thank you for all these lines of codes. I'm unfamiliar with Python, do all these various workarounds you've offered require downloading Python in some form or do some only require Terminal / ready built in apps? Thanks!

Batch rename files replacing periods with hyphens excluding the period in the file extension

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