Help

Batch Rename Extension

Topic Labels: Extensions
Solved
Jump to Solution
842 3
cancel
Showing results for 
Search instead for 
Did you mean: 
thearchitectuk
4 - Data Explorer
4 - Data Explorer

Hi all

I have a table, which has lots of PDF’s with random filenames. I’d like to batch rename them into Determination.pdf

I’ve had a look at other similar posts, but the script keeps saying:

ERROR

TypeError: Cannot read properties of null (reading ‘length’) at main on line 9

Here’s the modified script by Justin_Barrett which I found here.

// Setup
const mainTable = base.getTable("Projects | Applications")
const mainQuery = await mainTable.selectRecordsAsync()

// Build new attachment data
let updates = []
for (let record of mainQuery.records) {
    let attachments = record.getCellValue("Determination")
    if (attachments.length) {
        updates.push({
            id: record.id,
            fields: {   
                "Determination": [{
                    url: attachments[0].url,
                    filename: ("Determination.pdf")
                }]
            }
        })
    }
}

// Update records
while (updates.length) {
    await mainTable.updateRecordsAsync(updates.slice(0, 50))
    updates = updates.slice(50)
}

What have I done wrong? Thanks :slightly_smiling_face:

Also, once I’ve batched renamed them all, is it easy to convert the script above into an automation every time a new PDF is attached to a new record?

Thanks!

1 Solution

Accepted Solutions
kuovonne
18 - Pluto
18 - Pluto

One of your records does not have any attachments in it. Thus the value is null instead of an array, and null does not have a length.

Try changing this

To
if (attachments) {

My usual thought to questions like this is “easy for whom”? For some people with experience writing Airtable scripts, it is very easy. For others who are new to coding, it is not easy. A lot of the code would need to change to make this an automation script. And automation scripts that are based off of a new attachment upload have extra considerations to make sure the attachment is fully uploaded.

See Solution in Thread

3 Replies 3
kuovonne
18 - Pluto
18 - Pluto

One of your records does not have any attachments in it. Thus the value is null instead of an array, and null does not have a length.

Try changing this

To
if (attachments) {

My usual thought to questions like this is “easy for whom”? For some people with experience writing Airtable scripts, it is very easy. For others who are new to coding, it is not easy. A lot of the code would need to change to make this an automation script. And automation scripts that are based off of a new attachment upload have extra considerations to make sure the attachment is fully uploaded.


Update:

I’ve made an automation using this script, which runs whenever a new attachment is added to a record. Other than it renaming every file again, is seems to work well. Is there a problem with it doing this?


Thank you that worked!

Okay, I understand what you’re saying about the concept of something being “easy”!

I have another idea. Can I set up an automation that just simply runs this script say, once every 24h?

The only thing I’ve noticed, is that this script attempts to rename everything, even if it’s already had the name changed. Can it be modified so that it only renames those files not called “Determination.pdf”?

Thanks for your help!

It depends on what constitutes a problem. If you base is small and doesn’t have much activity you can probably get away with this.

If your base is large and has lots of activity, this could slow down the base with unnecessary work.

Yes, it can be modified to do so. However, again, how hard this is depends on the skill of the script writer.

How you go about this (running the script against all records on a schedule versus running this script on new file uploads) depends on how often you upload files, how soon the files need to be renamed, how many automation runs you have available, etc.