# How to overwrite a file?

**URL:** <https://forums.creativeclouddeveloper.com/t/how-to-overwrite-a-file/3875>\
**Category:** UXP Plugin API\
**Created:** [November 10, 2021, 1:03pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-overwrite-a-file/3875 "2021-11-10T13:03:39Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![AAverin](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/aaverin/32/1368_2.png) [@AAverin](https://forums.creativeclouddeveloper.com/u/AAverin)\
**Post date:** [November 10, 2021, 1:03pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-overwrite-a-file/3875/1 "2021-11-10T13:03:40Z")

</div>

I am trying to overwrite the same file by saving a jpg, but it doesn’t seem to work even though file is opened with `overwrite: true` set.

Having a Folder, I create a file with `overwrite: true` and save my image as jpg into that file.  
Then change the quality of jpg compression and save again, but the file doesn’t update anymore.

I have also tried creating a new File instance once again with the same name, but it also doesn’t update the file.

Any ideas?

---

<div class="post-metadata">

**Author:** ![kerrishotts](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/kerrishotts/32/8_2.png) [@kerrishotts](https://forums.creativeclouddeveloper.com/u/kerrishotts)\
**Post date:** [November 10, 2021, 6:07pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-overwrite-a-file/3875/2 "2021-11-10T18:07:12Z")

</div>

Can you share a code snippet?

---

<div class="post-metadata">

**Author:** ![AAverin](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/aaverin/32/1368_2.png) [@AAverin](https://forums.creativeclouddeveloper.com/u/AAverin)\
**Post date:** [November 10, 2021, 6:11pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-overwrite-a-file/3875/3 "2021-11-10T18:11:54Z")

</div>

@kerrishotts I’ll try. My code at the moment is much more elaborate than the snippet.  
This works first time, but if I call export function again with different quality data is never written to the file.

1. Ask user to select a folder, this gives folderEntry with access.

```auto
const getFile = async (filename, folderEntry) => {
  return await folderEntry.createFile(`${filename}.jpg`, {
        overwrite: true,
      })
}

const export = async (quality, token, documentId) => {
  await batchPlay([
  {
          _obj: "save",
          as: {
            _obj: "JPEG",
            extendedQuality: quality,
            matteColor: {
              _enum: "matteColor",
              _value: "none",
            },
          },
          in: {
            _path: token,
            _kind: "local",
          },
          documentID: documentId,
          lowerCase: true,
          saveStage: {
            _enum: "saveStageType",
            _value: "saveBegin",
          },
        }
])
}

const file = await getFile("Test", folderEntry)
const token = await fs.createSessionToken(file)

await export(token, 12, app.activeDocument._id)

```

---

<div class="post-metadata">

**Author:** ![AAverin](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/aaverin/32/1368_2.png) [@AAverin](https://forums.creativeclouddeveloper.com/u/AAverin)\
**Post date:** [November 10, 2021, 6:20pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-overwrite-a-file/3875/4 "2021-11-10T18:20:57Z")

</div>

I have also tried re-creating the file in the folder with `overwrite: true` and re-creating session token before trying to export again – same thing, data is not updated.  
If after export I call `await file.getMetadata()` I can see that bytes didn’t change even though with lower quality there should be less data written into the file

---

<div class="post-metadata">

**Author:** ![AAverin](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/aaverin/32/1368_2.png) [@AAverin](https://forums.creativeclouddeveloper.com/u/AAverin)\
**Post date:** [November 10, 2021, 6:22pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-overwrite-a-file/3875/5 "2021-11-10T18:22:53Z")

</div>

I have also tried deleting the file. Here it gets even more funny.

I have a permission to **folderEntry** where I have created **file**.  
If I call `await file.delete()` file gets deleted, but I can see an error that `FileSystemProvider: You don't have permission on this file 'Test_14.jpg'.`  
If right after this I try to create the file again it seems to get created and I see a new file descriptor, I create a new token and call my export function. But export descriptor fails with error `Error: no such file or directory`

---

<div class="post-metadata">

**Author:** ![AAverin](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/aaverin/32/1368_2.png) [@AAverin](https://forums.creativeclouddeveloper.com/u/AAverin)\
**Post date:** [November 11, 2021, 11:54am UTC](https://forums.creativeclouddeveloper.com/t/how-to-overwrite-a-file/3875/6 "2021-11-11T11:54:23Z")

</div>

@kerrishotts  
I have also tried using temporary folder and create a file there. Same problem, writing to the file again or even re-creating the file to overwrite doesn’t update file contents anymore

---

<div class="post-metadata">

**Author:** ![AAverin](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/aaverin/32/1368_2.png) [@AAverin](https://forums.creativeclouddeveloper.com/u/AAverin)\
**Post date:** [November 11, 2021, 12:19pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-overwrite-a-file/3875/7 "2021-11-11T12:19:10Z")

</div>

I have also tried creating a file in temporary folder and then deleting it but I get the same exception `FileSystemProvider: You don't have permission on this file`

---

<div class="post-metadata">

**Author:** ![AAverin](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/aaverin/32/1368_2.png) [@AAverin](https://forums.creativeclouddeveloper.com/u/AAverin)\
**Post date:** [November 11, 2021, 12:27pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-overwrite-a-file/3875/8 "2021-11-11T12:27:33Z")

</div>

It looks like one of the main problems is that awaiting for the save descriptor doesn’t guarantee that saving actually completed writing data into filesystem.  
So calling `file.getMetadata()` right after saving results in `No such file or directly` because even though Entry was created, file was not yet saved properly so there is no file yet.

---

<div class="post-metadata">

**Author:** ![Rip](https://avatars.discourse-cdn.com/v4/letter/r/e99b99/32.png) [@Rip](https://forums.creativeclouddeveloper.com/u/Rip)\
**Post date:** [May 17, 2024, 11:41pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-overwrite-a-file/3875/9 "2024-05-17T23:41:09Z")

</div>

> [@AAverin](#):
>
> `FileSystemProvider: You don't have permission on this file`

Has anyone found a workaround for the `FileSystemProvider: You don't have permission on this file` error?

I’m trying to delete a file that was created by a previous run of my plugin, and then rename a new file to that name.

I get this error also when ‘await entry.delete()’ is executed before saving the new file, so I don’t believe it’s caused by the save operation running in the background.

Other files returned by getEntries are deleted without this error and after the save command.

---

<div class="post-metadata">

**Author:** ![Jarda](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/jarda/32/1909_2.png) [@Jarda](https://forums.creativeclouddeveloper.com/u/Jarda)\
**Post date:** [May 18, 2024, 9:11am UTC](https://forums.creativeclouddeveloper.com/t/how-to-overwrite-a-file/3875/10 "2024-05-18T09:11:17Z")

</div>

It could mean the file is in “open/locked” state by something. So it should close the reading of the file first before changing it.

I think in “fs” module you would use `close` for it: [https://developer.adobe.com/photoshop/uxp/2022/uxp-api/reference-js/Modules/fs/#closefd-callback](https://developer.adobe.com/photoshop/uxp/2022/uxp-api/reference-js/Modules/fs/#closefd-callback)

But you need file desctiptor and I don’t know if FSP can provide it. Maybe you could switch everything to “fs” module.

---

<div class="post-metadata">

**Author:** ![Rip](https://avatars.discourse-cdn.com/v4/letter/r/e99b99/32.png) [@Rip](https://forums.creativeclouddeveloper.com/u/Rip)\
**Post date:** [May 20, 2024, 7:18pm UTC](https://forums.creativeclouddeveloper.com/t/how-to-overwrite-a-file/3875/11 "2024-05-20T19:18:07Z")

</div>

Thanks.

1. I was able to get fs to delete the file created by a previous run of my plugin like this:

> let path = app.activeDocument.path  
> let lastIndexOf = path.lastIndexOf(“\”)  
> path = path.slice(0, lastIndexOf+1);  
> path = path.replaceAll(“\”, “/”)  
> fileName = “test\_file\_to\_delete.tif”  
> const directFS = require(‘fs’)  
> fileDeleted = await directFS.unlink(“file:”+path+fileName);

1. Also, I discovered that if I moved the delete operation for other files ahead of the save operation for the new file, LFS deleted all of the files without throwing the error “FileSystemProvider: You don’t have permission on this file”.

I’m unclear as to the reason the sequence of deleting unrelated files is important.

I’d still like to use fs to save a log file to the same directory as the new TIF.

Based on the examples here:

> [@UXP accessing files and folders](http://forums.creativeclouddeveloper.com/t/uxp-accessing-files-and-folders/5930/17):
>
> Oh sorry I had forgotten. . on your manifest 5 add this after icons. "requiredPermissions": { "localFileSystem": "fullAccess", // this is important for the access // with regards to the question the rest is not of much importance "network": { "domains": ["http://forums.creativeclouddeveloper.com/t/uxp-accessing-files-and-folders/5930/15",] }, "launchProcess": { "schemes": [ …

Fs requires double backlashes instead of single forward slashes on a Mac, like this:  
file:c:\path\to\allowed\file.json

Which of these functions will return a string with double slashes on a Mac?  
anEntry.nativePath  
homedir()  
document.path

And will they be the required back slashes, or will the slashes have to be reversed, like on a PC?

Thanks again!
