Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Delete Random characters in filename?

Please check out the screenshot below. I made a program that sorts files the way I need personally, but I'm lost on this one! I have no idea how to approach this at all. Below are downloads from my College class and most downloads are like this. As you see the problem is a string of random characters at the end of a filename coerced with the filename. How in the world could I separate this random string from the filename by a script automatically? I'm thinking not possible? Idk so I just thought I'd throw it out there and see if anyone has a suggestion or idea I could go with or try to create. Thanks for reading.



As you see the name before the string of random characters could be any length so not sure if there's an approache to this here unless the amount of characters in the random string are a set number of characters like 12-4-4-4-8 from the end of the filename


Bless

User uploaded file

MacBook Pro, OS X Mavericks (10.9.2)

Posted on Apr 13, 2014 3:13 PM

Reply
8 replies

Apr 13, 2014 3:26 PM in response to CyX SenZe

the random characters are a machine-generated UUID, which (as you moticed) is always in the format 8-4-4-4-12: a total of 36 characters, plus the extension and punctuation for 41 characters. you can get rid of it with code like the following:


set txt to "some_file_name-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx.mov"

set cleanTxt to text 1 thru -42 of txt

--> "some_file_name"

Apr 13, 2014 3:41 PM in response to twtwtw

Ok cool and I can just make a list of special characters with an if statement to eliminate a dash or underscore before that string. I've done that in my previous script. Because not all teachers put a dash or special character at the end of the filename. So I'd just keep at the 41 characters with an If on the -42nd Thanks a lot! I had no idea about the machine-generated UUID. Keep up the awesome work 🙂

Apr 13, 2014 3:53 PM in response to CyX SenZe

🙂


Just as an observation, I'm not sure why that UUID is getting added. The only time I ever see something like that is when the system needs to distinguish between otherwise duplicate files (e.g. if you put two copies of a file in the trash, or do some funky copying from the command line). My guess would be that you're having students upload files with the same name into a common folder. You'd do yourself a favor by stopping that practice. several ways to do it (I wrestled with this issue when I was teaching, so...):


  • ask your students to append each file name with their student id. Easy to do, but big classes will only get you 80% compliance unless you threaten to dock points for it.
  • create individual upload folders for each student. More maintenance involved in this, but it's more effective as well.
  • set up a folder action that watches for new files and sorts/renames them based on file properties. this borders on power-scripting and is not for the feint-of-heart, but it removes the human element from the process as much as possible.


There are a couple of other tricks that might work, assuming that I have the right idea and depending on context. if you want to describe more of what you're doing we can see.

Apr 13, 2014 4:20 PM in response to twtwtw

As a bash script:


#!/bin/bash

for f in ~/Desktop/*
do
    newname=$(echo "$f" | sed -E 's/[_-][A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{8}//')
    if [ $f != $newname ]; then
        mv "$f" "$newname"
    fi
done



Searches for files in Desktop, edit to Folder as needed

Also, this will eliminate a dash or underscore before that string: [_-]

Delete Random characters in filename?

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