Is it possible to rename files in a UXP plugin?

The title already says what I need.
I tried this but I get an error: file.rename is not a function

      const aFolder = await fs.getFolder();
      const file = await aFolder.getEntry('name.jpg')
      await file.rename(aFolder, "New name.jpg");

Does anyone know how I can rename files using a UXP plugin?

Reading the UXP docs you’d find an answer to your question. Basically you have two options: 1) either you rename the file as an operation of its parent folder (as documented here):

await aFolder.renameEntry(fileEntry, "New name.jpg");

Or 2) by moving the file itself to a new location, in its original parent folder (i.e. equivalent to how you would rename a file through command line):

await fileEntry.moveTo(aFolder, {newName: "New name.jpg"});
1 Like

@dotproduct thanks man, saved my day. Thanks! :heart_eyes:

Unfortunately I was not successful with replacing files. I saw that in the documentation there is this possibility, but it didn’t work for me. Please, is there any possibility?

await aFolder.renameEntry(fileEntry, "New name.jpg" ,{ overwrite: true });
await aFolder.renameEntry(fileEntry, {newName: 'New name.jpg', overwrite: true});

Your first line looks correct, according to docs

I discovered that the problem lies in the loop.
for(const item of array) and array.forEath(), didn’t work, I don’t know if it was due to the handling of Asynchronous Functions.
Here is the loop that worked perfectly:

  let cont = 1;
  for (let i = 0; i < conteudo.length; i++) {
      const arquivo = conteudo[i];
      const listaNomes = await aFolder.getEntry(arquivo);
      const decimal = `${cont}`.padStart(digitos, "0");
      if (maiuscula === true) { novoNome = recebeNome.toUpperCase();}
       else { novoNome = recebeNome; }
      if (rename === true) {
          await aFolder.renameEntry(listaNomes, `${novoNome}_${decimal}.${extensao}`, {overwrite: true});
      } 
      else { prevNomes.push(`${novoNome}_${decimal}.${extensao}`); }
      cont++;
  }

Always avoid forEach if possible to use other option

1 Like

It took me a while to understand the forEach() method, I think it is something modern, but I see that it is much better to continue with our traditional for(). @Karmalakas I’ll follow your advice, thanks

Could you elaborate? Is this a general Javscript thing, UXP bound or just bad practice? In Python its the only way to iterate over a list, if I member correctly?

It’s a JS thing. I think this article explains it quite clear:
Avoiding common pitfalls when using forEach loop in JavaScript | by criss | Medium.

Depending on what you do inside the forEach, you might mess things up quite badly and it’s difficult to debug too

2 Likes

sorry but that post was clearly generated by ChatGPT. need a better source

You know what to search for, so please do and find a better source. IMO the link I gave has all the key points listed. I personally don’t really care who wrote the article, until it explains things as they are. If it’s just the style you don’t like, I’m sorry, but I’m not going to try and find something that would satisfy your needs, which I don’t even know what they are :man_shrugging:

2 Likes

i’m not trying to be unnecessarily inflammatory but if the best you could find was that post it probably means the points expressed in it are simply wrong.

  1. early termination: as the name suggests, the method is intended to iterate over each one of the elements. if you’re trying to, e.g., find an element, the method is simply not the right tool for the job and you should just use find()
  2. mutation: mutating the array inside of forEach() is simply not intended usage. this doesn’t mean that the method is bad, it just means it’s being misused.
  3. perf: the low-quality benchmarks i could find were pretty inconclusive. but you’re definitely not worrying about iteration performance while writing UXP, especially on the V8 engine.
  4. scopes: (this point does not make any sense)

i generally prefer for...of over forEach. i just think it’s harmful to discredit a perfectly valid technique in the language as an objective fact without providing a valid explanation (the comment on the post wasn’t meant to be a joke; the post is literally tagged with “ChatGPT”). having your personal preference is fine but this wasn’t it

I’m really glad if you didn’t have bad experience with forEach. I personally wasted days specifically because of scope the array being unintentionally mutated. Exact same approach with the for loop fixed the issue :man_shrugging:

There was a time when I didn’t know better and was using forEach in cases when I needed to break out from he loop. IIRC it’s possible to do that with a simple return too, but as you mentioned, function is not intended for that.

I remember performance being very slightly worse with the forEach than for, but don’t take my word for it now. Quite a bit has changed since.

My point still stands - if you’re new to JS, I would advise to just avoid forEach when possible. Now I’m aware of forEach problems (unlike years ago when started learning JS) and I still prefer to avoid it. It makes developing so much easier when you need to debug or even just read the code

1 Like

okay haha, after hearing your horror story with forEach i understand your sentiment much better. i see how this is especially relevant because even the OP was trying to use it in a way that didn’t work (with async callbacks), so i get how it’s better for new users to ignore features like this and revisit them down the line (even if they decide to continue ignoring it, like you did). sorry for coming off as rude earlier!