How best to rename many text files?

Hi folks,

I have a bunch of text files in a folder that I would like to modify.

Currently, the file name is not useful.

I would like to be able to grab the first line of each text file and use that as the new filename title and rename each file with the 1st line.

Currently I have Keyboard Maestro and also Hazel and access to AppleScript if needed.

I am curious how best to batch process tried him in the folder?

Thanks Dave

Finder can do batch renaming. Probably can do what you want. No need probably for complicated stuff or another app.

Except maybe for this bit?

I guess you want to automate. I missed that.

For that I would use Python. Probably just a very few lines of code. Applescript surely would work but I never really got into it. Google probably can help you find some code examples to start with.

I would not know where to start with Keyboard Maestro but others can comment.

2 Likes

Ask on the keyboard maestro forum. You’ll have a properly working macro in a day. I’d make one now but I’m on mobile at the moment.

1 Like

@rms — Agreed!

I thought I came across a KM macro that would do this, but in a quick search I couldn’t find it. I’ve found the people on the forum to be very helpful, so I’ll bet they’ll come up with a solution for you.

Actually, I might have seen this on the Hazel forums. You could try looking there in case someone’s already written something. I’m pretty sure this will require a script, but maybe not.

https://www.noodlesoft.com/forums/index.php

I messed up a bunch of PDF names, and used A Better Finder Rename to bail me out… independent dev, great customer service and support… check it out!

3 Likes

@dealtek

It’s coming up to bed-time here, but I thought I would contribute this for you.

Doing this in Python probably is simple and quick, but takes a small bit of care to think what might go wrong.

The psuedo-code, which is almost Python now and probably can be done in other languages like AppleScript, bash scripts, Perl, etc. But I have no time/energy for me to code it now for you.

change to the directory of interest
read the names of all files into a Python List, say "filenames"
loop through the list putting the loop index into a variable, say "orig_filename"
for each orig_filename in filenames
    open the file and read the first line of the file
    put the line into a variable, say "filename_new"
    close the file
    with a while loop check if a file with name "filename_new" exists
        if yes, append a prefix or suffix to the "filename_new" 
        keep checking until there a "filename_new" that does not exist already
        check that the "filename_new" is valid as a file name and if not fix
        have something in there so it doesn't loop forever
    rename the file from "orig_filename" to "filename_new"

Or something like that. Check the logic. Probably ought to put in some try/except statements

And, of course, please test on a collection of test files before doing it for real.

References

In addition to the Python Documentation at Our Documentation | Python.org

Get list of files in a directory:

Check if file exists

Rename files:

Exceptions:

2 Likes

Thank you , that is all good stuff.

Perhaps you are a bit sleepy, but it is @dealtek who is looking to do this. :slight_smile:

Stay well.

1 Like

Thanks for all the code and links!!!

I will be checking out the great suggestions soon

Alternatively, you can augment thinking and ask ChatGPT for a shell or python script to do what you want. It is shockingly good in performing such tasks.

I use ChatGPT more and more for getting ideas for solving similar issues.

1 Like

EDIT: Updated with working macro that should work by grabbing only the first line of each txt file🤞. Thanks to @rms for catching the error.

Here is a KM Macro that will do what I think you want without regex or scripting. It assumes all files in the folder are non-empty txt files, but you could (should) add some error checking to handle files that aren’t txt or txt files that are empty, etc. But this should get you started. Change the path in the first For Each action to the target folder.

Download KM Macro

Nice, but I don’t see where you read the first line of the content of each text message to get the file name, as the OP @dealtek wants. Can you pls point me to that new-to-me line in the macro. The “read file” command reads only the first line? thanks.

I think a proper scripting language is the best solution here, if you are comfortable with the Terminal and command line.

I found this solution and adapted it a little bit.

This will rename all *.txt files in a folder (and its subfolders!) with the first line of content and keep the *.txt extension.

find . -type f -name "*.txt" -print | while read FILE
do
   mv "$FILE" "$( dirname "$FILE" )"/"$( head -1 "$FILE")".txt
done

Please backup your files and work on a totally separate copy of the files before attempting to run this!

4 Likes

If you have a Setapp subscription they have a batch renaming app in there. No experience with it personally but might be able to do what you need.

Whoops. I think the macro accidentally worked with the test files I used :rofl:

I will update the original post with a working version.

2 Likes

Hi Evan Thanks so Much - right now I am very close to a working version (using your great macro) I will post asap. So feel free to relax and wait until I post it soon…

2 Likes

Thanks much - I will give this a try.

Hi Evan, I used your very good macro and made some mods with suggestions from PeterLewis and came up with this example (still a work in progress). Peter suggested :

Or you can use the For Each action and the Lines In collection to read the lines from the file, and break out of the loop immediately.

Also if you can let me know the best way to save the macro safely (so it does not load as enabled…) I will export and upload it.