The first segment below makes a copy of a file and...
# suitescript
m
The first segment below makes a copy of a file and saves the content changes and renames the file name. The duplicate file is due to file.create
Copy code
const updatedFileName = myFile.name + "_updated";
             const updatedFile = file.create({
                name: updatedFileName,
                fileType: myFile.fileType,
                contents: fileContent,
                folder: myFile.folder,
            });
             const updatedFileId = updatedFile.save();
             log.debug({title: "Updated File ID", details: updatedFileId});
             }
The second segment renames the file but the content doesn't get saved. What should I do so that the file changes are saved as well as the file rename?
Copy code
const updatedFileName = myFile.name + "_updated";
            myFile.name = updatedFileName;
            myFile.contents = fileContent;
            const updatedFileId = myFile.save();
            log.debug({title: "Updated File ID", details: updatedFileId});
a
contents
isn't a property of the File Object you can just set like that and save
just do the first one, and delete the original file
m
So something like this?
Copy code
const updatedFileName = myFile.name + "_updated";
            myFile.name = updatedFileName;
            myFile.save();
That ^ didn't work. I might use the first segment and then delete the original one like you said...
m
Check out the file.File object members. If you are appending lines you will see a function to do that, but I don't see anything to completely override contents. There is a possibility to iterate over all the lines and change them. However if you aren't appending lines it's probably easiest to create a new file with the new contents. Then delete the old if that is a requirement.
s
methinks if you 'create' a new file with the same name/location as the old, it overwrites the contents of the old file.
👍 1
m
I thought they were renaming and updating contents at the same time.
Oh IC your saying that if they kept the name instead of updating it essentially works like an in place update?
m
The idea was to load the file and modify it. Rename it. Save. I could rename it, but contents wouldn't Save. Instead, I'm loading the file, grabbing it's contents, creating new file and putting the modified content to the new file then moving the original one else where.
Not ideal, but does the job.