How to read all text files in a folder and display a filter based on a suffix or prefix

I am trying to use jQuery to read all text files in a folder and display their contents, but then a filter that should be displayed based on the folder name.

Here's the javascript:

var obj = {
  "01600610/9874565214_789545621.txt": "",
  "01600610/9874565214_789545622.txt": "",
  "01600610/9874565214_789545623.txt": ""
};
$.each( obj, function(SavedText) {
  $.get(SavedText, function(data){
    $('#NewMessagesHolder').prepend('<div class="MessageClass">'+ '<span class="ChatName">' + CookieName + ' ('+ time + ')</span>' + ': '+ data +'</div>')
    }, 'text');
});

In that:

var obj = {
    "01600610/9874565214_789545621.txt": "",
    "01600610/9874565214_789545622.txt":"",
    "01600610/9874565214_789545623.txt":""
};

Q1 : How to get all the text files inside the folder instead of specifying the file I want?

Q2 : How can I filter? For example, how can I get only files ending or beginning with 789545.

+3
source share
3 answers

When these files are on the local file system, you can use the HTML5 file system API .

, () javascript (, , , ). script ( PHP/Node/Perl/Phython/...), ajax .

script, ( ). :

var search = new RegExp("789545");
for(var file in obj) {
    if(search.test(file)) 
        alert(file+": "+obj[file]);
}

, 789545

+1

index.php . "" .

<?php
function contains($haystack, $needle){
    if (strpos($haystack,$needle) !== false) {
        return true;
    }
    return false;
}
if(isset($_POST['get_files'])){
    $folder = "";
    $filter = "";
    if(isset($_POST['folder'])){
        $folder = $_POST['folder'];
    }
    if(isset($_POST['filter'])){
        $filter = $_POST['filter'];
    }

    $files = array();
    foreach(glob($folder.'/*.*') as $filename){
        if($filter != ""){
            if(contains($filename, $filter)){
                $files[] = $filename;
            }
        }else{
            $files[] = $filename;
        }       
    }
    print_r($files);
    exit;
}
?>
<html>
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
function getData(){
    jQuery.post('index.php',{
        get_files:true,
        folder:"files",
        filter:""
    },function(data){
        jQuery('#container').html(data);
    });
}
</script>
</head>
<body>
<input type="button" value="Get data" onclick="getData();" />
<div id="container"></div>
</body>
</html>

jQuery

0

U . U script. perl php, . U ajax script .

http://perlmeme.org/faqs/file_io/directory_listing.html This link is just an example of what to do in perl. And you could do the same with other languages.

0
source

All Articles