Upcoming database upgrades. Airtable functionality will be reduced for ~15 minutes at 06:00 UTC on Feb. 4 / 10:00 pm PT on Feb. 3. Learn more here
Jul 12, 2022 12:03 AM
Hi all,
I’d appreciate some guidance on how can I change the filenames of my attachments. I have a table with 500 records. Every record has a name and multiple images (attachments).
The attachments have identical file names in multiple records, e.g I might have a file called 1.jpg in one record, then a different attachment in a different record with the same name of1.jpg.
I managed to find a script that does that, but it changes the name only for one attachment. Ideally, would be to create a rule that adds to the current filename, the name from another field.
e.g: Gorge 1 + current filename.jpg
// Setup
const mainTable = base.getTable("Test")
const mainQuery = await mainTable.selectRecordsAsync()
// Build new attachment data
let updates = []
for (let record of mainQuery.records) {
let attachments = record.getCellValue("images")
if (attachments.length) {
updates.push({
id: record.id,
fields: {
"images": [{
url: attachments[0].url,
filename: record.getCellValue("Name")
}]
}
})
}
}
// Update records
while (updates.length) {
await mainTable.updateRecordsAsync(updates.slice(0, 50))
updates = updates.slice(50)
}
Thank you in advance,
Victor
Solved! Go to Solution.
Jul 12, 2022 01:06 AM
Hi Victor, I’ve put a scripting extension together here for you to check out that should do what you’re looking for
And here’s the code:
// Setup start
const mainTable = base.getTable("Test")
const mainTable_imageFieldName = "images"
const mainTable_nameFieldName = "Name"
// Setup end
const mainQuery = await mainTable.selectRecordsAsync({
fields:[mainTable_nameFieldName, mainTable_imageFieldName]
})
// Build new attachment data
let updates = []
for (let record of mainQuery.records) {
let name = record.getCellValue(mainTable_nameFieldName)
let attachments = record.getCellValue(mainTable_imageFieldName)
if (attachments.length) {
for (let attachment of attachments){
let attachmentName = name + " - " + attachment.filename
updates.push({
id: record.id,
fields: {
[mainTable_imageFieldName]: [{
url: attachment.url,
filename: attachmentName
}]
}
})
}
}
}
// Update records
while (updates.length) {
await mainTable.updateRecordsAsync(updates.slice(0, 50))
updates = updates.slice(50)
}
Jul 12, 2022 01:06 AM
Hi Victor, I’ve put a scripting extension together here for you to check out that should do what you’re looking for
And here’s the code:
// Setup start
const mainTable = base.getTable("Test")
const mainTable_imageFieldName = "images"
const mainTable_nameFieldName = "Name"
// Setup end
const mainQuery = await mainTable.selectRecordsAsync({
fields:[mainTable_nameFieldName, mainTable_imageFieldName]
})
// Build new attachment data
let updates = []
for (let record of mainQuery.records) {
let name = record.getCellValue(mainTable_nameFieldName)
let attachments = record.getCellValue(mainTable_imageFieldName)
if (attachments.length) {
for (let attachment of attachments){
let attachmentName = name + " - " + attachment.filename
updates.push({
id: record.id,
fields: {
[mainTable_imageFieldName]: [{
url: attachment.url,
filename: attachmentName
}]
}
})
}
}
}
// Update records
while (updates.length) {
await mainTable.updateRecordsAsync(updates.slice(0, 50))
updates = updates.slice(50)
}
Jul 12, 2022 02:28 AM
Wow, thanks a lot, Adam! Didn’t know this community is so helpful! Have a great day!