Skip to content

$ps.file.getFileList

Overview

Use "$ps.file" in "Server Script" to get a list of file names in a specified directory.

Prerequisites

DisableServerScriptFile in "Script.json" must be set to false.

Syntax

$ps.file.getFileList(section, path)

Parameters

Parameter Type Required Description
section string Yes Section name. For details about sections, see "About Sections" in "$ps.file".
path string Yes Directory name. For both Windows and Linux, the directory separator is "/".

Return Value

Returns an array of file name strings.

Exceptions

If an exception occurs in C#, the exception class name and error message are placed in the Error object in the server script to raise an exception.

Usage Examples

The following example gets a list of file names in a specified directory in the web server and outputs the results to a log.

JavaScript
const lists = $ps.file.getFileList('test_data','parts');
context.Log('listCnt='+lists.length);
for (const element of lists) {
context.Log('list='+element);
}

Code Samples

Modify 【 ... 】 in the code as necessary.
1. Import CSV files stored in a given folder

This is a sample code that imports CSV files stored in a given folder.

The processing flow is as follows.
(1) Import the CSV files stored in the received folder into Pleasanter
(2) After processing, move them to backup/received

JavaScript
const SECTION = 'files';
const RECEIVED = 'received';
const BACKUP_RECEIVED = 'backup/received';
const SITE_NAME = '【Site name】';
// Get the site information
const site = items.GetClosestSite(SITE_NAME);
if (!site) {
    logs.LogException(`Site: ${SITE_NAME} was not found.`);
    return false;
}
const siteId = site.SiteId;
// Read the files in the received folder and import them
const param = {};
const lists = $ps.file.getFileList(SECTION, RECEIVED);
if (lists.length > 0) {
    for (const element of lists) {
        const result = $ps.file.import(
            SECTION,
            `${RECEIVED}/${element}`,
            siteId,
            $ps.JSON.stringify(param),
        );
        if (!result) {
            logs.LogException(
                `Import failed. File name: ${element}`,
            );
            return false;
        } else {
            logs.LogInfo(
                `Import succeeded File name: ${element} Result: ${$ps.JSON.stringify(
                    result,
                )}`,
            );
        }
    }
    // After the import is complete, move the files to the backup folder
    for (const element of lists) {
        const moveResult = $ps.file.moveFile(
            SECTION,
            `${RECEIVED}/${element}`,
            `${BACKUP_RECEIVED}/${element}`,
        );
        if (!moveResult) {
            logs.LogException(`File move failed File name: ${element}`);
            return false;
        } else {
            logs.LogInfo(`File move succeeded File name: ${element}`);
        }
    }
}
2. Export table data to a given folder

This is a sample code that exports table data to a given folder.

The processing flow is as follows.
(1) Move the previously exported CSV files stored in the send folder to backup/send
(2) Store the exported CSV file in the send folder

JavaScript
// Settings
const SECTION = 'files';
const SEND = 'send';
const BACKUP_SEND = 'backup/send';
const SITE_NAME = '【Site name】';
const FILE_DATE_FORMAT = 'utilities';
// Get the processing date and time as a string
function getDateTimeString() {
    return new Date()
        .toLocaleString('ja-JP', {
            year: 'numeric',
            month: '2-digit',
            day: '2-digit',
            hour: '2-digit',
            minute: '2-digit',
            second: '2-digit',
            hour12: false, // 24-hour format
        })
        .replaceAll('/', '')
        .replaceAll(':', '')
        .replaceAll(' ', '');
}
// Get the site information
const site = items.GetClosestSite(SITE_NAME);
if (!site) {
    logs.LogException(`Site: ${SITE_NAME} was not found.`);
    return false;
}
const siteId = site.SiteId;
// Move the files in the send folder to the backup folder
const lists = $ps.file.getFileList(SECTION, SEND);
if (lists.length > 0) {
    for (const element of lists) {
        const moveResult = $ps.file.moveFile(
            SECTION,
            `${SEND}/${element}`,
            `${BACKUP_SEND}/${element}`,
        );
        if (!moveResult) {
            logs.LogException(`File move failed File name: ${element}`);
            return false;
        } else {
            logs.LogInfo(`File move succeeded File name: ${element}`);
        }
    }
}
// File export: uses the export definition with ID 1. The target records are those whose "target month is this year"
const param = {
    ExportId: 1,
    View: {
        ColumnFilterHash: {
            DateA: '["ThisYear"]',
        },
    },
};
const path = `${SEND}/${FILE_DATE_FORMAT}${getDateTimeString()}.csv`;
const exportResult = $ps.file.export(
    SECTION,
    path,
    siteId,
    $ps.JSON.stringify(param),
);
if (!exportResult) {
    logs.LogException(`Export failed. File name: ${path}`);
    return false;
} else {
    logs.LogInfo(`Export succeeded File name: ${path}`);
}

Supported Versions

Supported versions Body
1.4.12.0 and later Added function