Copy a File Using Conflict Resolution
The following sample shows how to copy a file in the File Cabinet using the RENAME_TO_UNIQUE conflict resolution option. Using this option, if a file with the same name as the original file already exists in the target folder, the original file is copied and a number is appended to the file name to make the name unique. In this sample, the folder ID values are hard-coded. For the script to run in the SuiteScript Debugger, you must replace these hard-coded values with valid folder IDs from your account.
This sample script uses the require function so that you can copy it into the SuiteScript Debugger and test it. You must use the define function in an entry point script (the script you attach to a script record and deploy). For more information, see SuiteScript 2.x Script Basics and SuiteScript 2.x Script Types.
/**
* @NApiVersion 2.x
*/
require(['N/file'], function(file) {
var fileObj = file.create({
name: 'test.txt',
fileType: file.Type.PLAINTEXT,
contents: 'Hello World\nHello World'
});
fileObj.folder = 2667;
var id = fileObj.save();
fileObj = file.copy({
id: id,
folder: 2670,
conflictResolution: file.NameConflictResolution.RENAME_TO_UNIQUE
});
});
Related Topics
- N/file Module Script Samples
- Create and Save a File to the File Cabinet
- Create a File, Set Property Values, and Save It to the File Cabinet
- Create and Save a CSV File then Reload the File and Parse Its Contents
- Read and Log File Contents Using Commas and New Lines as Separators
- Read and Log Segments of a File Using a Set of Characters as Separators
- SuiteScript 2.x Modules
- SuiteScript 2.x