I'm using the SFTP transfer module to copy over fi...
# general
s
I'm using the SFTP transfer module to copy over files from Amazon S3 to the File Cabinet. One instance works fine, but a second insists on renaming the incoming file
file_1.csv
file_2.csv
etc, even though the directory is empty at the time of the transfer. Because the filename changes, I can't use the Task module to run a CSV import. I can't see any way to get a directory contents in SuiteScript to figure out the filename. Any suggestions?
w
This may not be what you want, nor the best way to do it.., but I usually upload one file to the directory and then move it to a directory (not a sub-directory) where I keep completed csv files. This is the code I use:
Copy code
function getFileContents(folderId) {
            let file_results = this.getFiles(folderId);
            let fileId;
            if (file_results && file_results.length > 0) {
                //Just get the first file
                fileId = file_results[0];
            }
            if (fileId) {
                let objFile = file.load(fileId);
                //let fileType = objFile.fileType;
                let fileContents = objFile.getContents();
                //let fileName = objFile.name;
                return fileContents;
            }
            else {
                return null;
            }
        }
Copy code
function getFiles(folderId,moveToFolder) {
            let arrFileResults = [];
            let i = 0;
            let fileSearch = search.create({
                type: 'file',
                filters: [
                    search.createFilter({
                        name: 'folder',
                        operator: <http://search.Operator.IS|search.Operator.IS>,
                        values: folderId
                    })
                ]
            }).run().each(function(fileRecordSearchResult) {
                i++;
                arrFileResults.push(fileRecordSearchResult.id);
                return true;
            });
            if (moveToFolder) {
                arrFileResults.forEach(function (fileId) {
                    let recFile = file.load({
                        id: fileId
                    });
                    recFile.folder = moveToFolder;
                    recFile.save();
                })
            }
            else {
                return arrFileResults;	//Array of id's
            }
        }
s
Nice, thank you
I think I also figured out the root issue. Looks like NS looks at all files in a folder including any subfolders when determining if a filename is unique
w
oh yes, it does. That's why I never put any sub folders underneath the folders I am using to load files