Created
June 13, 2012 11:03
-
-
Save BenExile/2923448 to your computer and use it in GitHub Desktop.
Parsing FTP LIST Command Responses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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