javascript to create folder and create ini file when folder and file not exist

R
rathasatekun
Gosoft (Thailand) Co.,Ltd.

Hi SOTI Comminity

I new for javascript and i have use case that

I will check folder and file  by if not found folder i will create folder 'sdcard/SOTI' and create file 'sdcard/SOTI/info.ini'

how to do with javascript?

I try 

checkFolder();
function checkFolder() {
var foldercheck = new mobicontrol.io.File('/sdcard/.SOTI');
if (foldercheck.exists) {
mobicontrol.log.info('/sdcard/.SOTI exists');
 
}
    else { 
mobicontrol.log.info('/sdcard/.SOTI not exist.')
newFolder();
};
}
newFolder();
function checkFolder() {
var file = new java.io.File('/sdcard/.SOTI');
var path = file.mkdir();
}

But error not module java

My SOTI server version 15.3

and SOTI Client verson 2024.0.4 Build 1056

a year ago
SOTI MobiControl
ANSWERS
ZC
Zafer Cigdem
a year ago

Hi,

You can not use all JS functions on SOTI MobiControl. To create a new folder you may prefer below function:

var folderPath = '/sdcard/Download/Zafer';
new mobicontrol.io.File(folderPath).createDirectory();

So overall code, when we add this to your function would be as below:


var folderPath = '/sdcard/Download/Zafer2';


checkFolder();
function checkFolder() {
var foldercheck = new mobicontrol.io.File(folderPath);
if (foldercheck.exists) {
mobicontrol.log.info('Folder exists');
 
}
else {
mobicontrol.log.info('Folder does not exist.')
newFolder();
};
}
newFolder();
function newFolder() {
new mobicontrol.io.File(folderPath).createDirectory();
}

I hope it helps.

Zafer

ZC
Zafer Cigdem
a year ago

And i would recommend not to use a "." in a folder name (original post).

And the creation of your ini file is missing as well which can be done like this:

var iniFile = new mobicontrol.io.File('/Path/Filename.ini');
if (!iniFile.exists) {
iniFile.createFile();
}
ZC
Zafer Cigdem
a year ago

I agree with Rafael,

And thanks, I forgot the ini file :)

.ini file can be created by putting some values inside of the .ini file as well that allows us to read some data and list on MobiControl WebConsole.

Example as:

var file = new mobicontrol.io.File('/sdcard/Download/user_name.ini');
var textContent = "[User]\n UserName=Zafer";
file.writeText(textContent);
S
SGMOD@SOTI
a year ago

Hi Rathasatekun,

Thanks for posting on SOTI Pulse, Thanks Zafer and Rafael
for responding to the post, your expertise and willingness to help are greatly appreciated!

Have you had an opportunity to test the suggested solutions by Zafer and Rafael and has it successfully addressed your query?

If not, or If you have any additional questions or concerns, please don't hesitate to reach out. We're dedicated to providing assistance and support.