Skip to content

Instantly share code, notes, and snippets.

@tobiasoberrauch
Forked from BenExile/gist:2923448
Created June 16, 2012 22:56
Show Gist options
  • Save tobiasoberrauch/2942715 to your computer and use it in GitHub Desktop.
Save tobiasoberrauch/2942715 to your computer and use it in GitHub Desktop.
Parsing FTP LIST Command Responses
var response = '', // FTP LIST response as a string
lines = response.trim().split("\r\n"),
parsed = [],
regexp = new RegExp(
'^([\\-dbclps])' + // Directory flag [1]
'([\\-rwxs]{9})\\s+' + // Permissions [2]
'(\\d+)\\s+' + // Number of items [3]
'(\\w+)\\s+' + // File owner [4]
'(\\w+)\\s+' + // File group [5]
'(\\d+)\\s+' + // File size in bytes [6]
'(\\w{3}\\s+\\d{1,2}\\s+' + // 3-char month and 1/2-char day of the month [7]
'(?:\\d{1,2}:\\d{1,2}|\\d{4}))' + // Time or year (need to check conditions) [+= 7]
'\\s+(.+)$' // File/directory name [8]
);
// Add each parsed line to the parsed array
for(var line in lines){
var parsedLine = regexp.exec(lines[line]);
if(parsedLine === null) {
continue; // Skip if no match
} else {
parsed[line] = {
type: parsedLine[1],
perms: parsedLine[2],
items: parsedLine[3],
owner: parsedLine[4],
group: parsedLine[5],
size: parsedLine[6],
date: parsedLine[7],
file: parsedLine[8],
};
}
}
alert(JSON.stringify(parsed));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment