Device file management module for Android devices in the Total Control scripting system.
This module provides a high-level file I/O interface for managing files and folders
on connected Android devices, as well as transferring files between the device and
the host computer. All functions are exposed as Java-layer extensions to the
Device object so they can be invoked via device.cpFile(), device.readFile(),
etc.
Features
- Copy and move files with optional overwrite
- Rename files or relocate them atomically
- Check whether a file or directory exists on the device
- Read text content from a file
- Write/append text to a file with optional offset and append mode
- Delete files and remove directories
- Upload files from PC → device and download files from device → PC with timeout control
Usage Example
const { Device } = require("sigma/device");
const file = require("sigma/file"); // methods are attached to Device.prototype
const device = Device.getMain();
// Copy a file and rename it
let ret = device.cpFile("/sdcard/test.txt", "/sdcard/aa/test2.txt", true);
if (ret === 0) print("Copy OK");
// Write then read a file
ret = device.writeFile("/sdcard/aa/hello.txt", "Hello, Amy!");
if (ret === 0) {
const text = device.readFile("/sdcard/aa/hello.txt");
print("Content: " + text);
}
// Download to PC
ret = device.download("/sdcard/aa/hello.txt", "E:/File", 60000);
if (ret !== 0) print("Download failed: " + lastError());
Integration
- Methods are automatically bound to
Device.prototypeandDeviceArray.prototype
viaattachMethodJavaFn. - Exported API names:
cpFile,doesExist,download,mvFile,readFile,
renameFile,rmDir,rmFile,upload,writeFile.
Functions
(inner) cpFile(oldFilePath, newFilePath, overlay) → {number}
Copy the file. copy a file from oldFilePath to newFilePath.
Example
// Example 1: Copy and rename the file
// Copy the file test.txt to /sdcard/aa/ and rename it to test2.txt
const { cpFile } = require("sigma/file");
device = Device.getMain();
const ret = device.cpFile("/sdcard/test.txt", "/sdcard/aa/test2.txt", true);
if (ret != 0) {
print(lastError());
} else {
print("copy file successfully!");
}
// Example 2: Copy the file without renaming
// Copy the file test.txt to /sdcard/aa/test.txt (same name)
const { cpFile } = require("sigma/file");
device = Device.getMain();
const ret = device.cpFile("/sdcard/test.txt", "/sdcard/aa/test.txt", true);
if (ret != 0) {
print(lastError());
} else {
print("copy file successfully!");
}
// Operation Result:
// If it executes successfully, it will print:
"copy file successfully!"
Parameters:
| string | oldFilePath |
The path to the source file on the phone. (e.g., "/sdcard/test.txt"). |
| string | newFilePath |
The path to the destination file on the phone. |
| boolean | overlay |
When the file exists in the destination folder, whether to overwrite the file, true is overwritten, false is not overwritten. |
Returns:
| number |
This function returns 0 on success or -1 on failure. Specific error information can be obtained by the lastError() function. |
| string | filePath |
The path to a file or folder on your phone. (e.g., "/sdcard/test/test.txt"). |
Returns:
| number |
This function returns 0 on success or -1 on failure. Specific error information can be obtained by the lastError() function. |
| string | from |
The file on the phone that needs to be downloaded. (e.g., "/sdcard/aa/test2.txt"). |
|
| string | to |
Target folder (folder in the computer). (e.g., "E:/File"). Must be a valid writable path. |
|
| number | timeOut |
<optional> |
Optional parameters, timeout (ms), default 60 seconds. |
Returns:
| number |
Returns 0 if successful; otherwise, returns a non-zero error code. Use |
| string | oldFilePath |
The path to the source file on the phone. (e.g., "/sdcard/test.txt"). |
| string | newFilePath |
The path to the destination file on the phone. (e.g., "/sdcard/aa/123.txt"). |
| boolean | overlay |
Whether to overwrite the destination file if it already exists. |
Returns:
| number |
This function returns 0 on success or -1 on failure. Specific error information can be obtained by the lastError() function. |
| string | filePath |
The full path of the file to read (e.g., "/sdcard/aa/test3.txt"). |
Returns:
| number | null |
This function returns the contents of the specified file on success or null on failure. Specific error information can be obtained by the lastError() function. |
| string | oldFilePath |
The path to the source file on the phone. (e.g., "/sdcard/aa/test2.txt"). |
| string | newFilePath |
The path to the destination file on the phone. (e.g., "/sdcard/aa/test3.txt"). |
Returns:
| number |
This function returns 0 on success or -1 on failure. Specific error information can be obtained by the lastError() function. |
| string | filePath |
Delete the folder. (e.g., "/sdcard/test/"). |
Returns:
| number |
This function returns 0 on success or -1 on failure. Specific error information can be obtained by the lastError() function. |
| string | filePath |
The file on the phone. (e.g., "/sdcard/aa/test3.txt"). |
Returns:
| number |
This function returns 0 on success or -1 on failure. Specific error information can be obtained by the lastError() function. |
| string | from |
The file on the phone that needs to be downloaded. (e.g., "E:\File\testupload.txt"). |
|
| string | to |
Target folder (folder in the computer). (e.g., "/sdcard/aa/"). |
|
| number | timeOut |
<optional> |
Optional parameters, timeout (ms), default 60 seconds. |
Returns:
| number |
This function returns 0 on success or -1 on failure. Specific error information can be obtained by the lastError() function. |
| string | filePath |
The file path on the phone. (e.g., "/sdcard/aa/test.txt"). |
|
| string | content |
Text content. |
|
| number | offset |
<optional> |
Optional parameters, offset, relative to the end of the file offset. The default is 0, which means appending content at the end of the original file. |
| number | type |
<optional> |
Optional parameters, Append mode, the default is 1.
|
Returns:
| number |
This function returns 0 on success or -1 on failure. Specific error information can be obtained by the lastError() function. |