It says
error at line 70
script error: cannot read property of 'replace' of undefined
and I removed the replace from the line..
now it shows no error but
the most of list items do not contain filename as title but contain only size, date etc as body...
only some files in the list have filename as title.
and when I touch the list item which is folder,
it do not browse the touched folder neither it has the option to go back to parent folder.
drwxrwx--- root everybody 2018-04-18 11:11 .AppySite
drwxrwx--- root everybody 2018-07-17 14:24 .DroidScript
drwxrwx--- root everybody 2018-04-09 02:44 .EveryplayCache
drwxrwx--- root everybody 2018-06-11 11:24 .OrMicPark
-rw-rw---- root everybody 5 2018-07-17 12:41 .appodeal
drwxrwx--- root everybody 2018-04-09 02:44 .chartboost
drwxrwx--- root everybody 2018-05-15 16:41 .estrongs
drwxrwx--- root everybody 2018-03-29 21:51 .smartlauncher
-rw-rw---- root everybody 72 2018-07-17 14:24 .userReturn
drwxrwx--- root everybody 2018-03-09 10:02 846b44643ec609f507828878741e1f9a
drwxrwx--- root everybody 2018-03-18 10:40 Algoid
drwxrwx--- root everybody 2018-01-31 00:36 Android
drwxrwx--- root everybody 2018-04-17 22:36 AppySiteCould you please show me your ls -al /sdcard/ output
in function ListFolderSH
put app.WriteFile(lst, app.GetAppPath()+"/log.txt")
before
if(lst.length === 0) {
// use app.ListFolder instead
lst = app.ListFolder(path)
Please try to figure out the reason - I can't reproduce it in any way
btw be sure there's always a '/' at the end of the path - maybe I have to add another test which checks for it
app.Debug( "<b>[DEBUG] path before: " + path );
app.Debug( "<b>[DEBUG] path after: " + path );[DEBUG] path before: /sdcard/
[DEBUG] path after: /sdcard/
-> displayed: /sdcard/
-> click on: 'Music'
[DEBUG] path before: /sdcard/
[DEBUG] path after: /sdcard/Music/
-> displayed: /sdcard/
-> click on: '.'
[DEBUG] path before: /sdcard/Music/
[DEBUG] path after: /sdcard/Music/
-> displayed: /sdcard/Music/
-> click on: '..'
[DEBUG] path before: /sdcard/Music/
[DEBUG] path after: /sdcard/
-> displayed: /sdcard/
-> click on: 'DCIM'
[DEBUG] path before: /sdcard/
[DEBUG] path after: /sdcard/DCIM/
-> displayed: /sdcard/
-> click on: 'DCIM'
[DEBUG] path before: /sdcard/DCIM/
[DEBUG] path after: /sdcard/DCIM/DCIM/
-> displayed: /sdcard/DCIM/
-> click on: 'Camera'
[DEBUG] path before: /sdcard/DCIM/DCIM/
[DEBUG] path after: /sdcard/DCIM/DCIM/Camera/
-> displayed: empty folder
-> click on: '..'
[DEBUG] path before: /sdcard/DCIM/DCIM/Camera/
[DEBUG] path after: /sdcard/DCIM/DCIM/
-> displayed: empty folder
-> click on: '..'
[DEBUG] path before: /sdcard/DCIM/DCIM/
[DEBUG] path after: /sdcard/DCIM/
-> displayed: /sdcard/DCIM/
-> click on: 'Camera'
[DEBUG] path before: /sdcard/DCIM/
[DEBUG] path after: /sdcard/DCIM/Camera/
-> displayed: /sdcard/DCIM/Camera
-> note: no permissions displayed (might be app.ListFolder)
-> click on: '.'
[DEBUG] path before: /sdcard/DCIM/Camera/
[DEBUG] path after: /sdcard/DCIM/Camera/
-> displayed: /sdcard/DCIM/Camera/
-> note: permissions displayed (might be sysproc)
-> click on: '..'
[DEBUG] path before: /sdcard/DCIM/Camera/
[DEBUG] path after: /sdcard/DCIM/
-> displayed: /sdcard/DCIM/Camera/
-> note: images icons/thumbnails are gone for images files
-> clickon: '.'
[DEBUG] path before: /sdcard/DCIM/
[DEBUG] path after: /sdcard/DCIM/
-> displayed: /sdcard/DCIM/
-> click on: '..'
[DEBUG] path before: /sdcard/DCIM/
[DEBUG] path after: /sdcard/
-> displayed: /sdcard/DCIM/
-> click on: 'Camera'
[DEBUG] path before: /sdcard/
[DEBUG] path after: /sdcard/Camera/
-> displayed: /sdcard/
-> note: /sdcard/Camera/ does not exists, it's in DCIM/*
Advances ListFolderSH example by Symbroson
WTFPL license - no warranties
if you find any bugs:
feel free to fix them yourself or report them at least
post it in the related thread in the DroidScript forum
https://groups.google.com/d/topic/androidscript/xOTWRP8ZUm0/discussion
*/
var sys, files = [], path = "/sdcard/";
function error( msg, line, file ) {
app.Alert( file + ": error at line " + line + "\n\n" + msg, "notitle" );
}
function OnStart() {
app.SetScreenMode("full");
app.SetOnError(error);
sys = app.CreateSysProc("sh");
var lay = app.CreateLayout("linear", "VCenter,FillXY");
lstFiles = app.CreateList('', 1, 1);
lstFiles.SetOnTouch(List_OnTouch);
lay.AddChild(lstFiles);
List_OnTouch('.');
if(files.length === 0) {
alert('Couldn\'t load "' + path + '". Stop.');
app.Exit();
}
app.AddLayout(lay);
}
function List_OnTouch(item, body, img, i) {
app.Debug( "<b>[DEBUG] path before: " + path );
var tpath = path;
if(item === "..") path = path.slice(0, path.slice(0, -1).lastIndexOf('/') + 1);
else if(item !== '.') {
if(files[i].type === "lnk") item = item.slice(0, item.lastIndexOf("->") - 1);
if(files[i].type === "dir" || (files[i].type === "lnk" &&
app.IsFolder(tpath + item + '/'))) path += item + (item === '/'? '' : '/');
else return app.OpenFile(path + item);
}
files = ListFolderSH(path, true, true);
if(files.length === 0) {
app.ShowPopup("couldn't switch directory");
files = ListFolderSH(path = tpath, true);
}
files = SortFolderList(files, "name", "asc");
files = FoldersFirst(files);
lstFiles.SetList(MakeFolderList(files).join(";;;"), ";;;");
app.Debug( "<b>[DEBUG] path after: " + path );
}
function OnFResume() {
List_OnTouch('.');
}
function MakeFolderList(list) {
return list.map(function(file) {
var date = file.date.toString();
return file.name + ':' + file.permissions + ' ' +
(file.type == "dir" && file.links > 0? file.links + ' ' : '') +
file.owner + ' ' + file.group +
(file.type != "dir"? ' ' + Math.round(file.size / 1024) + "kB " : ' ') +
date.slice(0, date.lastIndexOf(':')).replace(/:/g, "^c^") +
':' + ({
fil: "Img/file.png", dir: "Img/folder.png",
lnk: "Img/link.png", img: path + file.name,
snd: "Img/music.png", vid: "Img/video.png"
}[file.type] || ' ');
});
}
function SortFolderList(list, sortby, order) {
var order = {asc: 1, inc: 1, dec: -1, desc: -1}[order];
return list.sort( function(file1, file2) {
var res;
switch(sortby) {
case "name":
res = file1.name.toLowerCase() > file2.name.toLowerCase();
break;
default:
res = file1[sortby] < file2[sortby];
}
return res ? order : -order;
});
}
function FoldersFirst(list) {
var lstDir = [], lstFil = [];
list.forEach(function(v, i, l) {
if(v.type == "dir") lstDir.push(v);
else lstFil.push(v);
});
return lstDir.concat(lstFil);
}
function File(type, permissions, links, owner, group, size, date, name) {
return {
name: name, type: type,
permissions: permissions,
links: links, owner: owner,
group: group, size: size,
date: date
};
}
var formats = {
img: ".png.jpg.jpeg.bmp.dds.tiff.svg.cgm",
snd: ".mp3.wav.wma.pcm.aac.ac3.ogg.tta.wma.msv",
vid: ".mp4.vob.flv.mkv.ogv.gif.mng.avi.wmv.amv.mpg.mpeg.svi"
};
function extractType(type, name) {
var ext = name.lastIndexOf('.'), ttype = type;
if(ext > 0) ext = name.slice(name.lastIndexOf('.'));
else ext = null;
// set file type
if(type == '-' && ext !== null)
for(var i in formats)
if(formats[i].indexOf(ext) >= 0)
return i;
if(type === '-') return "fil";
else if(type === 'd') return "dir";
else if(type === 'l') return "lnk";
return type;
}
function ListFolderSH(path, showHidden) {
var s, lst = [], i = 0, files = [];
if(path.length > 0) {
//try shell 3 times
for(var i = 0; lst.length === 0 && i < 2; i++) {
// execute ls command
sys.Out('ls -la "' + path + '"\n');
//read command output
while((lst = sys.In(10, "nowait")) == '' && ++i < 10);
while(s = sys.In(100, "nowait")) lst += s;
}
}
if(lst.length === 0) {
// uae app.ListFolder instead
lst = app.ListFolder(path);
if(lst.length > 0) {
lst = (".,.." + (path.length === 0? ",/" : '')).split(',').concat(lst);
//create and return file object list
return lst.map(function(file) {
var isFld = app.IsFolder(path + file + '/') ? 1 : 0;
return File(
extractType("-d"[isFld], file), "", "", "", "",
app.GetFileSize(path + file + (isFld? '/' : '')),
app.GetFileDate(path + file + (isFld? '/' : '')), file
);
});
}
} else {
// create list of file objects
lst.replace(
/(.)(([r-][w-][x-]){3})\.?([\d\s]*?)\s*(\w*)\s*(\w*)\s*(\d*)\s*(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2})\s(.*)/g,
function(match, type, permissions, _, links, owner, group, size, year, month, day, hours, mins, name) {
type = extractType(type, name);
//assign date object
var date = new Date();
date.setFullYear(year);
date.setMonth(month - 1);
date.setDate(day);
date.setHours(hours);
date.setMinutes(mins);
date.setSeconds(0);
date.setMilliseconds(0);
files.push(File(
type, permissions, 1 * links, owner,
group, 1 * size, date, name
));
}, ''
);
if(!showHidden) {
files = files.filter(function(v, i, l) {
return v.name[0] == '.' || v.name == '.' || v.name == "..";
});
}
}
if(files.length === 0 || files.filter(function(file) {return file.name === "..";}).length === 0)
files.unshift(File("dir", "", "", "", "", app.GetFileSize('.'), app.GetFileDate('.'), ".."));
if(files.length === 0 || files.filter(function(file) {return file.name === '.';}).length === 0)
files.unshift(File("dir", "", "", "", "", app.GetFileSize('.'), app.GetFileDate('.'), '.'));
return files;
};
function ListFolderSH(path, showHidden) {
console.log("ListFolderSH(\"" + path + "\")");
var s, slst = '', i = 0, list;
if(path.length > 0) {
//try shell 2 times
for(var i = 0; slst.length === 0 && i < 2; i++) {
// execute ls command
sys.Out('ls -la "' + path + '";echo \"#*end%!\"\n');
//read command output
while((slst = sys.In(10, "nowait")) == '' && ++i < 50);
while(!slst.endsWith("#*end%!\n")) slst += sys.In(100, "nowait");
}
}
slst = slst.slice(0, -9);
if(slst.length === 0) { /* ... */However I think we can get at least close to a stable ls parser even if it's very complicated. And such things like linebreaks or tab characters in filenames are rare enough to just ignore them because most android filemanagers don't allow that at all. (That doesn't mean we can't try to handle them)
Also getting each file metadata by a command may be difficult because the tools may be not available (ie I don't have the 'stat' command by default)
And at last I hope that android has at least some standardization in it's shell. I won't try to deal with '?' replaced characters - that would be too much..
I will spend some more time and thoughts in the ListFolderSH function since I know about the problems it has
best regards