Android - Script to update text in a file?

Android - Script to update text in a file?

Hi,

I have been scratching my head for a way to send a script to an Android device that will read a text file and then essentially do a find and replace for a string of text.  We do this frequently with out Windows CE devices using the built-in replacetxt script within MobiControl.  I've tried to research some javascript options, but I really don't have any javascript experience. 

 

EX:  File:   /mnt/sdcard/documents/app/config.txt

-  File contains various configurations in a XML type layout.

There may be a string in there like an IP address or URL that I'd like to be able to change programmatically with a script, so it could be sent to 100s of devices at a time.

 

Any help is greatly appreciated.

6 Answers

Order By:   Standard | Newest | Votes
Matt Dermody | posted this 17 June 2021

The javascript based scripting options include read/write file access so you should be able to accomplish what you need to there. Javascript is a well established and documented programming language with a lot of reference material online.  

  • 0
  • 0
Chris R. | posted this 17 June 2021

That's kind of what I figured and was playing a bit last night.  I found a snippet that looked like it would do it, but got some errors when sending it to the device.  As I read some more of the javascript reference from Soti, there was mention of having to upload the module into a path on the device before it could be used.  Based on that, where do I find these modules?  I struck out last night.  Below is the script I was attempting to run where in theory it would replace yahoo.com with google.com as an example. 

I would assume that the modules readFile, data.replace, and writeFile would need to be added to the device in order for this to work?


var fs = require('fs')
fs.readFile(/mnt/sdcard/Documents/app/config.txt, 'utf8', function (err,data) {
  if (err) {
    return console.log(err);
  }
  var result = data.replace(/yahoo.com/g, 'google.com');

  fs.writeFile(someFile, result, 'utf8', function (err) {
     if (err) return console.log(err);
  });
});

 


The error I was getting back:  "Custom log (invalid flag after regular expression (UserScript#2)) "

  • 0
  • 0
JCMOD@SOTI | posted this 21 June 2021

Hi Chris,

 

Thank you for posting in SOTI Central.

 

I've sent a request internally to clarify a few aspects regarding the capabilities of our Android Agent and the JavaScript feature. We'll be in touch in due course.

 

Regards,

Technical Support | SOTI Inc. |1.905.624.9828 | support@soti.net | www.soti.net |

  • 0
  • 0
Dan Rubanovich | posted this 22 June 2021

Hi Chris,

 

‘fs’ module is a part of Node.js, which is an extension of core JavaScript language. Our JavaScript engine only supports core language features, so ‘fs’ module cannot be used.

 

We also do not support ‘console’ since this is not a part of the core JavaScript language, but rather a browser addition.

 

We do provide alternatives though:

 Basically your code will look something like this:

 

var inputFile = new mobicontrol.io.File(‘/mnt/sdcard/Documents/app/config.txt’);
var data = inputFile.readText();
var result = data.replace(/yahoo.com/g, 'google.com');

var outputFile = new mobicontrol.io.File(‘someFile’);
outputFile.writeText(result);

If there is any error – it will be automatically logged, however if you need a special error handling – you can do so using a try-catch block, see an example in https://www.soti.net/mc/help/javascriptapi/en/mobicontrol.intl.Locale.html

 

Best regards,

Dan, Software Development | SOTI Inc

  • 3
  • 1
JCMOD@SOTI | posted this 23 June 2021

Hi Chris,

 

If the above answer has resolved your query, please mark the post as the solution.

 

Also, feel free to reach out if you have any questions or need clarification.

 

Regards,

Technical Support | SOTI Inc. |1.905.624.9828 | support@soti.net | www.soti.net |

  • 0
  • 0
Chris R. | posted this 23 June 2021

I did test this today and it worked as expected.  Thanks much for the help!

  • 0
  • 0

Give us your feedback
Give us your feedback
Feedback