Create/Update Android Enterprise/Classic Text Files Using JavaScript

Publish Date: 11-Jan-2024 Last Modified Date: 27-Aug-2025 SOTI MobiControl
1651 0

Summary

This article describes how to use JavaScript to create/update text files for both Android Enterprise/Classic devices.

Related SOTI ONE Platform Products

SOTI MobiControl

Related Device OS

Android Classic;Android Enterprise

Situation

Some common use cases for creating files using data extracted from the SOTI MobiControl agent include:

  • Extracting data using custom data. For example, create a .txt file on each device containing name/serial/MAC address, etc.
  • Replacing specific text (such as URLs) on each device.

Environment

Android Classic and Android Enterprise devices.

Process Description

In the SOTI MobiControl Android Agent JavaScript engine, you can create text files on a device's SD card. You can include device details such as the device names, serial numbers, or MAC addresses. You can also replace a specific URL in an existing text file on the device's SD card.

 

Option 1: Create a new text file on the device

1. Navigate to the Device Information section.
2. Select Actions button () > Send Script.
3. In the Script Type field, select 
JavaScript.

4. Enter the following JavaScript code:

var file = new mobicontrol.io.File('sdcard/DevSerNumber.txt');
file.writeText(mobicontrol.device.serialNumber);

 5. Select Send Script. A text file is created on the device's SD card named DevSerNumber.txt, containing the device's serial number.


Tip: For more information on the file.writeText(text) function, see SOTI MobiControl Android Agent JavaScript.

 

 

Option 2: Replace existing data in a text file

 

This example shows how to replace the data in an existing text file named URL.txt. The file content (a URL) google.com is replaced with yahoo.com:

 

1. Navigate to the Device Information section.
2. Select Actions button () > Send Script.
3. In the Script Type field, select JavaScript.

4. Enter the following JavaScript code:

var inputFile = new mobicontrol.io.File('/sdcard/URL.txt');
var data = inputFile.readText();
var result = data.replace('google.com', 'yahoo.com');
var outputFile = new mobicontrol.io.File('/sdcard/URL.txt');
outputFile.writeText(result);

 5. Select Send Script.

The URL.txt text file is updated so that the URL google.com is replaced with yahoo.com.

 

Was this helpful?