File Name changing script help

Hey Community,

I was getting help a little while ago on a script a month ago, and I’m trying to apply the old script to a new use case, and having a bit of a problem.

I’m having another issue upstream through an integration that is removing the file name. This problem is a bug that doesn’t look like it’ll be fixed in the short term, so I thought if I reuse a name script I’ve been using, it should solve my problem.

The only issue with the script is in the current version it was built to maintain the extension, IE: PDF, MOV, MP4, etc… but I want to completely rename the file via the field I have on my database.

When I’m using this right now, it’s for some reason adding the name of the file at the end of it when the name changes…

I’ve spent nearly 1.5 hours trying to solve this in different ways, but can’t figure it out. Does anyone who is better with scripts know how to change this?

This is what it is currently giving me, and I don’t want the weird stuff at the end… I want it to end at .PDF, which it’s currently pulling from the name field.

image

// Setup
let config = input.config()
let table = base.getTable("TABLE")
let query = await table.selectRecordsAsync()
let record = query.getRecord(config.recordID)

// Collect record data
let newPrefix = record.getCellValue("NEWNAME")
let files = record.getCellValue("ATTACHMENT")

// Get the extension and add it to the new name
let newFiles = []
for (let file of files) {
    let parts = file.filename.split(".")
    let ext = parts[parts.length - 1]
    let fileNumber = "0" + (files.indexOf(file) + 1)
    let newName = `${newPrefix}-${fileNumber}.${ext}`
    newFiles.push({url: file.url, filename: newName})
}

// Reattach the item with the new filename
await table.updateRecordAsync(record, {
    "ATTACHMENT": newFiles
})

Also,

I suddenly started getting this error on this forum that’s been working for months, but it is not affecting it’s from working, just when I test it. Should I be concerned?

TypeError: files is not iterable
    at main on line 13

Hi @Cameron_Goldberg - I’m not exactly sure I’ve got your scenario correctly, but it sounds like you’ve got an attachment and another field with a filename in it, like this:

If there’s only one attachment in the Attachments field and a single filename to go with it, something like this will work:

let table = base.getTable('Table 4');
let query = await table.selectRecordsAsync();

for (let record of query.records) {
    let files = record.getCellValue('Attachments');
    let newAttachments = [];
    for (let file of files) {
        let url = file.url;
        console.log(url)
        let newFileName = record.getCellValue('new name')
        console.log(newFileName)
        newAttachments.push({ url: url, filename: newFileName})
    }
    await table.updateRecordAsync(record, {
        'Attachments': newAttachments
    })
}

On the error, if the ATTACHMENT field is an Attachment type, then it is an array and should be iterable (as in the script above).

Hey Jonathan,

Thanks for adding some color context on this. I missed your notification initially until now, so sorry for the delay.

So, sorry if I didn’t explain the use case correctly.

TLDR: file names of imported files are imported like this:
image

The files are DOCX or PDF, so we rename to PDF they always work.

Anyways, to your question above, we actually generally have multiple files about 50% of the time. I gave it my best shot this week (Before I got your notification) to edit the the scprit:

// Setup
let config = input.config()
let table = base.getTable("TABLE1")
let query = await table.selectRecordsAsync()
let record = query.getRecord(config.recordID)

// Collect record data
let newPrefix = record.getCellValue("NEWNAME")
let files = record.getCellValue("ATTACHMENT")

// Get the extension and add it to the new name
let newFiles = []
for (let file of files) {
  
    let newName = `${newPrefix}`
    newFiles.push({url: file.url, filename: newName})
}

// Reattach the item with the new filename
await table.updateRecordAsync(record, {
    "ATTACHMENT": newFiles
})

Firstly

The above script actually worked for 2 or more files and has been working, but unfortunately calls both files the same name… so ideally I could add a “+1” to it at some point…

Secondly, it actually deleted 2 files (BIG PROBLEM) today when uploading after using the script 100 times without problems… So I’m a little stumped. Perhaps you know why?

Lastly, it always says… but still works

TypeError: files is not iterable
    at main on line 13

I’m curious about your thoughts.

Best,
Cam

just a bit more context, this is what normally happens:

image

this is what happened and it’s super concerning:

image

and what is strange is the file is correct on the one that was removed… anyways, throwing a lot of extra info so you (or anyone) has context.