1.c++:
#include <iostream> #include <fstream> #include <vector> #include <string> #include "cocos2d.h" #if Cc_target_platform = = Cc_platform_win32#include <windows.h> #include <strsafe.h> #else # include < Dirent.h> #endifnamespace FS {int readDir (const char *path, vector<string> &names) {names.clear (); #if cc_ Target_platform = = Cc_platform_win32win32_find_dataa Ffd;//large_integer filesize;string szDir;//size_t length_of_ Arg HANDLE hfind = Invalid_handle_value;dword dwerror=0;string strdir = Path;szdir = Strdir + "\\*"; hfind = FindFirstFileA (SzD Ir.c_str (), &FFD); if (Invalid_handle_value = = hfind) {//cclogwarn ("Get File name error", path); return 0;} Do{if (! ( Ffd.dwfileattributes & file_attribute_directory) {string filename=ffd.cfilename;//(const char*)//string Filedir =strdir+ "\ \" +filename;string Filedir = Filename;names.push_back (Filedir);}} while (Findnextfilea (hfind, &ffd)! = 0);d Werror = GetLastError (); if (dwerror! = error_no_more_files) {Cclogwarn ("FindFirstFile error", path); return 0;} FindClose (hfind); #elseDIR *DP = opendir (path), if (!DP) {//cclogwarn ("Open dir Error:%s.", path); return 0;} struct Dirent *DIRP = Readdir (DP), while (DIRP) {if (!strcmp (Dirp->d_name, ".") | |! strcmp (Dirp->d_name, "..")) Continue;names.push_back (dirp->d_name);} Closedir (DP); sort (Names.begin (), Names.end ()); #endifreturn names.size ();}}
2. Bind JS:
BOOL Js_fs_readdir (Jscontext *cx, uint32_t argc, Jsval *vp) {if (argc = = 1) {//Get the native object from the second objec T to the first objectjsval *ARGV = JS_ARGV (CX, VP); string path;jsval_to_std_string (CX, argv[0], &path); Vector<stri Ng> Names;fs::readdir (Path.c_str (), names); Jsval Jsret = Jsval_null;jsret = Std_vector_string_to_jsval (cx, names); Js_set_rval (CX, VP, Jsret); return true;} return false;}
3.js Call:
var files = fs.readdir (Full_path);
PS: Here to pass the full path, it seems that Android to copy files to the writable path to get to the file list.
Cocos3--5.js getting a list of files under a folder