Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Feb 11, 2013, 01:22 PM   #1
537635
macrumors regular
 
Join Date: Mar 2009
CLI truncate filenames

I would like to create an automator workflow and inside a shell script, which would rename files:

123456789.jpg -> 456.jpg
etc.

All the files have the same name lenght and I would like to truncate the names with using only the middle characters (6 of them).

Help appreciated!
537635 is offline   0 Reply With Quote
Old Feb 11, 2013, 01:37 PM   #2
chown33
macrumors 603
 
Join Date: Aug 2009
How about the trial version of A Better Finder Rename:
http://www.publicspace.net/ABetterFi.../download.html

If it works, and it's something you'll be doing again, it's probably worth paying for it.
chown33 is offline   0 Reply With Quote
Old Feb 11, 2013, 01:40 PM   #3
537635
Thread Starter
macrumors regular
 
Join Date: Mar 2009
Thank you for the idea, but I intentionally posted this in the programming section and not in the applications section.
537635 is offline   0 Reply With Quote
Old Feb 11, 2013, 01:54 PM   #4
chown33
macrumors 603
 
Join Date: Aug 2009
Quote:
Originally Posted by 537635 View Post
I would like to create an automator workflow and inside a shell script, which would rename files:

123456789.jpg -> 456.jpg
etc.

All the files have the same name lenght and I would like to truncate the names with using only the middle characters (6 of them).
"6 of them" is unclear. Your example shows only 3 middle characters, not 6, and a preserved suffix (extension). Did you really mean the 3 middle characters, after excluding the suffix?

What's your experience with shell globbing patterns, reg-ex, or any programming language?
chown33 is offline   0 Reply With Quote
Old Feb 11, 2013, 02:09 PM   #5
537635
Thread Starter
macrumors regular
 
Join Date: Mar 2009
Basically I would just like to know, which parameters to use with "mv" command. It's not important, what is the position of the characters, I'll add that later. And suffix should be preserved.

for f in "$@"
do
Shortname=${f:3:6}
mv "$f" "$Shortname"
done

but it doesn't really work...

Experience? None .

But so far I managed to put together a shell script, which renames files with exiftool, so I can sort pictures by name and date at the same time (YYYY-MM-DD HH-MM-SS).
537635 is offline   0 Reply With Quote
Old Feb 11, 2013, 02:21 PM   #6
DennisVar
macrumors newbie
 
Join Date: Jun 2010
You were pretty close! Disclaimer: use at your own risk, test with a small set of files first, and so on:


for F in *
do
SHORTNAME=${F:3:3}${F:9:4}
mv "$F" "$SHORTNAME"
done


Note that the first line lists all files in the current folder, which means that you probably want to have the script (if you save it in a file) be in another folder.

As you're learning, I highly recommend using "echo" to sanity-check things. Here, you can tack on echo before the mv line. This will print each mv command it's about to run instead of actually running it. I.e.:


for F in *
do
SHORTNAME=${F:3:3}${F:9:4}
echo mv "$F" "$SHORTNAME"
done

Last edited by DennisVar; Feb 11, 2013 at 02:32 PM.
DennisVar is offline   0 Reply With Quote
Old Feb 16, 2013, 10:05 AM   #7
537635
Thread Starter
macrumors regular
 
Join Date: Mar 2009
Here's the final update:

Your disclaimer was spot on. The first thing I did, was to expect "mv" in an automator process to start in a given folder. Well it starts in home folder, as I later learned. The script worked and it nicely renamed all the files and folders in my /home. Lesson learned.

From here on everthing worked like a charm.

This is the final code.

for f in "$@"
do
FILEBASE=$(basename "$f")
FOLDERBASE=$(dirname "$f")
SHORTNAME=20${FILEBASE:14:2}${FILEBASE:10:2}${FILEBASE:6:2}${FILEBASE:16:3}${FILEBASE:20:2}${FILEBAS E:23:2}
NEWNAME=$FOLDERBASE"/"$SHORTNAME
mv "$f" "$NEWNAME".mov
done


The problem with movies files on iPhone is, that they are a headache to organize. Timestamp in EXIF is wrong (don't really know why), only the filename is correct.

Video 10. 02. 13 17 42 11.mov

The problem is, that I have my pictures organized as YYYYMMDD HHMMSS. Therfor I needed this script to match the names of the movies.


This is the final result:

Renaming the images via the EXIF timestamp:


Renaming the movies by truncating the filename to match:


Result:




Took quite so time, but I've learned a lot in the process. Coming from Windows I must say that it is astonishing what an average user can accomplish in terms of modifying the UI to his needs with relatively little effort.


Thanks for all the help!
537635 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Icon for CLI / X11 apps? richyw Mac Basics and Help 0 Mar 4, 2014 04:56 PM
How to get filenames back for videos Umbrarian iOS 7 4 Sep 21, 2013 12:54 PM
Get CPU/GPU temperature on Apple TV 2 from CLI kraades Apple TV and Home Theater 2 Jul 9, 2012 09:13 AM

Forum Jump

All times are GMT -5. The time now is 11:59 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC