Manage Files and Directories

The following sample shows how you can manage files and directories.

Note:

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.

Important:

This sample uses SuiteScript 2.1. For more information, see SuiteScript 2.1.

          /**
 * @NApiVersion 2.1
 */
require(['N/sftp', 'N/file'], (sftp, file) => {
    // Establish a connection
    log.debug('Establishing SFTP connection...');
    let connection = sftp.createConnection({
        username: 'sftpuser',
        keyId: 'custkeysftp_nft_demo_key',
        url: 'myurl',
        port: 22,
        directory: 'inbound',
        hostKey: 'myhostkey'
    });
    log.debug('Connection established!');
     
    // List the directory and log the number of elements there
    let list = connection.list({
        path: 'yyy/test'
    });
    log.debug('Items in directory "test" at the beginning: ' + list.length);
 
    // Generate the test file
    log.debug('Generating test file...');
    let myFileToUpload = file.create({
        name: 'asdf.txt',
        fileType: file.Type.PLAINTEXT,
        contents: 'I am a test file.'
    });
    log.debug('Test file generated, uploading to "test" directory...');
 
    // Upload the test file
    connection.upload({
        directory: 'yyy/test',
        filename: 'af.txt',
        file: myFileToUpload,
        replaceExisting: true
    });
    log.debug('Upload complete!');
 
    // List the directory to confirm there is one more file than before
    list = connection.list({
        path: 'yyy/test'
    });
    log.debug('Items in directory "test" after the upload: ' + list.length);
 
    // Create a new directory
    log.debug('Creating directory "test2"...');
    try {
        connection.makeDirectory({
            path: 'yyy/test2'
        });
        log.debug('Directory created.');
    } catch (e) {
        log.debug('Directory not created.');
        log.error(e.message);
    }
    list = connection.list({
        path: 'yyy/test2'
    });
    log.debug('Items in directory "test2": ' + list.length);
 
    // Move the test file to the new directory
    log.debug('Moving the test file from "test" to "test2"...');
    connection.move({
        from: 'yyy/test/af.txt',
        to: 'yyy/test2/af.txt'
    })
    log.debug('File moved!');
 
    // List the original directory again to see the file is moved
    list = connection.list({
        path: 'yyy/test'
    });
    log.debug('Items in directory "test" after the upload: ' + list.length);
 
    // List the new directory for the file
    list = connection.list({path: 'yyy/test2'});
    log.debug('Items in directory "test2" after the upload: ' + list.length);
    log.debug(JSON.stringify(list));
 
    // Try to remove the directory
    log.debug('Removing directory "test2"...');
    try {
        connection.removeDirectory({
            path: 'yyy/test2'
        });
        log.debug('Directory removed!');
    } catch (e) {
        log.debug('Directory not removed!');
        log.error(e.message);
    }
 
    // The directory is not empty so delete the file first
    log.debug('Removing test file from "test2" directory...');
    connection.removeFile({
        path: 'yyy/test2/af.txt'
    });
    log.debug('Test file removed!');
     
    list = connection.list({
        path: 'yyy/test2'
    });
    log.debug('Items in directory "test2": ' + list.length);
 
    // Try to remove the directory again
    log.debug('Removing directory "test2"...');
    try {
        connection.removeDirectory({
            path: 'yyy/test2'
        });
        log.debug('Directory removed!');
    } catch (e) {
        log.debug('Directory not removed!');
        log.error(e.message);
    }
 
    // Try to list the removed directory
    log.debug('Trying to list directory "test2"...');
    try {
        list = connection.list({
            path: 'yyy/test2'
        });
    } catch (e) {
        log.error(e.message);  
    }
}); 

        

General Notices