cocos3——5.js获取文件夹下文件列表

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>

#endif

namespace fs
{

	int readDir( const char *path, vector<string> &names )
	{
		names.clear();

#if CC_TARGET_PLATFORM == CC_PLATFORM_WIN32
		WIN32_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(szDir.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);

		dwError = GetLastError();
		if (dwError != ERROR_NO_MORE_FILES)
		{
			//CCLOGWARN("FindFirstFile error", path);
			return 0;
		}
		FindClose(hFind);

#else
		DIR *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());
#endif

		return names.size();
	}
}

2.绑定js:

bool js_fs_readDir(JSContext *cx, uint32_t argc, jsval *vp)
{
	if (argc == 1) {
		// get the native object from the second object to the first object
		jsval *argv = JS_ARGV(cx, vp);
		string path;
		jsval_to_std_string(cx, argv[0], &path);
		vector<string> 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调用:

var files = fs.readdir(full_path);

PS: 这里要传全路径,貌似android要把文件拷到可写路径去才能获取到文件列表。。

时间: 2024-08-05 11:20:22

cocos3——5.js获取文件夹下文件列表的相关文章

获取列表中某一个文件夹下的列表项集合(不包含子文件夹对象,也不包含子文件夹中的列表项)

RT,方法如下: 1 SPListItemCollection GetSubItemsWithoutFoldersInParrentFolder(SPFolder parrent) 2 { 3 SPList list = parrent.Item.ParentList; 4 SPQuery query = new SPQuery(); 5 query.Folder = parrent; 6 query.Query = "<Where><Eq><FieldRef Na

Linux统计某文件夹下文件、文件夹的个数

统计某文件夹下文件的个数 ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数 ls -l |grep "^d"|wc -l 统计文件夹下文件的个数,包括子文件夹里的 ls -lR|grep "^-"|wc -l 如统计/home/han目录(包含子目录)下的所有js文件则: ls -lR /home/han|grep js|wc -l 或 ls -l "/home/han"|grep "js&qu

统计某文件夹下文件的个数

统计某文件夹下文件的个数ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数ls -l |grep "^d"|wc -l 统计文件夹下文件的个数,包括子文件夹里的ls -lR|grep "^-"|wc -l 如统计/home/han目录(包含子目录)下的所有js文件则:ls -lR /home/han|grep js|wc -l 或 ls -l "/home/han"|grep "js"|

android开发步步为营之54:读取assets,raw文件夹下文件

一.读取assets文件下文件products.json public String readAssetFile(Context c, String file) { Elapsed profiler = new Elapsed(); BufferedReader bufReader = null; try { InputStreamReader inputReader = new InputStreamReader(c.getResources().getAssets().open(file))

iOS NSFileManeger 计算文件是否超时,和计算文件夹下文件的总大小

//获得指定文件距离上次修改时间是否达到了指定值(秒)timeout +(BOOL)isTimeout:(NSString *)path time:(NSTimeInterval)timeout { //获得当前时间 NSTimeInterval now = [[NSDate date] timeIntervalSince1970]; NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:path

计算文件夹下文件的总大小

-(float)fileSizeForDir:(NSString*)path//计算文件夹下文件的总大小 {          NSFileManager *fileManager = [[NSFileManager alloc] init];     float size =0;     NSArray* array = [fileManager contentsOfDirectoryAtPath:path error:nil];     for(int i = 0; i<[array cou

Linux统计某文件夹下文件的个数

ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数 ls -l |grep "^d"|wc -l 统计文件夹下文件的个数,包括子文件夹里的 ls -lR|grep "^-"|wc -l 统计/imagedata/data/20161108目录(包含子目录)下的所有txt文件 ls -lR /imagedata/data/20161108|grep txt|wc -l   或者  ls -lR "/imagedata/

Linux随笔 - Linux统计某文件夹下文件、文件夹的个数

统计某文件夹下文件的个数 ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数 ls -l |grep "^d"|wc -l 统计文件夹下文件的个数,包括子文件夹里的 ls -lR|grep "^-"|wc -l 统计/imagedata/data/20161108目录(包含子目录)下的所有txt文件 ls -lR /imagedata/data/20161108|grep txt|wc -l   或者  ls -lR &quo

linux 统计文件夹下文件,文件夹,所有个数

统计某文件夹下文件的个数 ls -l |grep "^-"|wc -l 统计某文件夹下目录的个数 ls -l |grep "^d"|wc -l 统计文件夹下文件的个数,包括子文件夹里的 ls -lR|grep "^-"|wc -l 原文地址:https://www.cnblogs.com/dhName/p/11018555.html

php文件夹下文件批量重命名

php文件夹下文件批量重命名 <?php header("Content-type:text/html;charset=utf-8"); $dir = __DIR__.'./color/'; $file_arr = scandir($dir); unset($file_arr[0]); unset($file_arr[1]); $file_arr = array_values($file_arr); $n = count($file_arr); for ($i = 0; $i &