[βœ… Solution] Can file names be changed via automation / calculation / script?

If a file is uploaded into the system, could the filename be changed according to an algorithm?

1 Like

Hi @dirk_s,

thanks for your question - sounds like an interesting use case!

Currently, as files are considered immutable in Tape, there is no way to change their name β€œin place”. However, one can just upload the file again and provide a different filename, then reattach the file to the record.

The strategy could be as follows:

  1. Read your Tape internal file ID and existing filename from your record
  2. Upload the file via the developer API
  3. Reattach the file by updating your file field value

The script action that I used in my test case contained the following code:

// TODO: use your file field variables here
const existingFileUrl = rename_uploaded_file_field_files_file_url;
const existingFileName = rename_uploaded_file_field_files_filename;

// change file name according to some algorithm:
const newFileName = 'NEW-' + existingFileName;

const {data: file_content} = await http.get(existingFileUrl, {responseEncoding: 'binary'});
const encoded_file_content = Buffer.from(file_content, 'binary')
const new_upload = await tape.File.upload({filename:newFileName, source: encoded_file_content });

var_new_file_id = new_upload.data.file_id;

Ultimately, the final workflow looked like this:

It replaces an uploaded file’s name by adding a β€œNEW-” prefix. You can of course use your own algorithm to determine the desired filename. If this needs to work with multiple files, one might need to enhance a the logic a bit.

Let me know if that works, all excited!

Cheers
Tim

We could also delete the old one then?

1 Like

Once the reference to the old file is removed from the record, it will be garbage collected by Tape (as there is no reference to is anymore) and would thereby be deleted soon.

Does that answer your question?

Cheers
Tim

1 Like