$ps.file.moveFile
Overview¶
Use "$ps.file" in "Server Script" to rename or move the specified file.
Prerequisites¶
DisableServerScriptFile in "Script.json" must be set to false.
Syntax¶
Parameters¶
| Parameter | Type | Required | Description |
|---|---|---|---|
| section | string | Yes | Section name. For details about sections, see "About Sections" in "$ps.file". |
| old_name | string | Yes | Old file name. For both Windows and Linux, use "/" as the directory separator. |
| new_name | string | Yes | New file name. For both Windows and Linux, use "/" as the directory separator. |
Return Value¶
Returns true if the file was moved, and false if it was not moved.
Exceptions¶
If an exception occurs in C#, the server script will throw an exception by putting the exception class name and error message into the Error object.
Usage Examples¶
The following example changes the specified file name in the web server.
JavaScript¶
The following example moves the specified file name in the web server.
JavaScript¶
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 or later | Added function |