• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >Lua > Lua脚本实现递归删除一个文件夹

Lua脚本实现递归删除一个文件夹

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了lua 遍历文件夹,lua 创建文件夹,lua 删除文件夹,lua文件夹,lua脚本等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

rmdir in quick-cocos2d-x with lua.
</div>
在使用 quick-cocos2d-x 做项目热更新的时候,我需要建立临时文件夹以保存下载的更新包。在更新完成后,我需要删除这些临时文件和文件夹。

cocos2d-x 和 quick-cocos2d-x 都没有提供删除文件夹功能。我做了如下2个尝试:

1. 使用C++

在 cocos2d-x 2.x 中的 AssetsManager 包中提供了一个 CreateDirectory 方法。这个方法可以跨平台支持创建文件夹。在实际项目中运行没有问题。
bool AssetsManager::createDirectory(const char *path)
{
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
    mode_t processMask = umask(0);
    int ret = mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO);
    umask(processMask);
    if (ret != 0 && (errno != EEXIST))
    {
        return false;
    }

    return true;
#else
    BOOL ret = CreateDirectoryA(path, NULL);
if (!ret && ERROR_ALREADY_EXISTS != GetLastError())
{
return false;
}
    return true;
#endif
}
</div>

在 cocos2d-x 2.x 的 AssetsManager sample 范例中提供了一个 reset 方法,这个方法使用系统命令递归删除文件夹。

void UpdateLayer::reset(cocos2d::CCObject *pSender)
{
    pProgressLabel->setString(" ");

    // Remove downloaded files
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
    string command = "rm -r ";
    // Path may include space.
    command += "\"" + pathToSave + "\"";
    system(command.c_str());
#else
    string command = "rd /s /q ";
    // Path may include space.
    command += "\"" + pathToSave + "\"";
    system(command.c_str());
#endif
    // Delete recorded version codes.
    getAssetsManager()->deleteVersion();

    createDownloadedDir();
}
</div>

但是,这个 reset 在 ios 模拟器中运行的时候,xcode会报这样的warinng:

The iOS Simulator libSystem was initialized out of order. This is most often caused by running host executables or inserting host dylibs. In the future, this will cause an abort.

因此,我转而考虑另一个方案。

2. 纯lua

纯 lua 其实是个噱头。这里还是要依赖 lfs(lua file sytem),好在 quick-cocos2d-x 已经包含了这个库。

lfs.rmdir 命令 和 os.remove 命令一样,只能删除空文件夹。因此实现类似 rm -rf 的功能, 必须要递归删除文件夹中所有的文件和子文件夹。

让我们扩展一下 os 包。

require("lfs")

function os.exists(path)
    return CCFileUtils:sharedFileUtils():isFileExist(path)
end

function os.mkdir(path)
    if not os.exists(path) then
        return lfs.mkdir(path)
    end
    return true
end

function os.rmdir(path)
    print("os.rmdir:", path)
    if os.exists(path) then
        local function _rmdir(path)
            local iter, dir_obj = lfs.dir(path)
            while true do
                local dir = iter(dir_obj)
                if dir == nil then break end
                if dir ~= "." and dir ~= ".." then
                    local curDir = path..dir
                    local mode = lfs.attributes(curDir, "mode")
                    if mode == "directory" then
                        _rmdir(curDir.."/")
                    elseif mode == "file" then
                        os.remove(curDir)
                    end
                end
            end
            local succ, des = os.remove(path)
            if des then print(des) end
            return succ
        end
        _rmdir(path)
    end
    return true
end
</div>

上面的代码在 iOS 模拟器和 Android 真机上测试成功。Windows系统、Mac OSX 以及 iOS 真机还没有测试。我测试后会立即更新。

</div>

您可能想查找下面的文章:

  • Lua脚本实现递归删除一个文件夹
  • Lua中实现递归删除一个文件夹

相关文章

  • Lua中的迭代器和泛型for介绍
  • Lua中调用函数使用点号和冒号的区别
  • 浅析Lua中的迭代器
  • Lua利用cjson读写json示例分享
  • lua日志文件处理代码
  • Mac平台中编译安装Lua运行环境及Hello Lua实例
  • Lua学习笔记之数据类型
  • Lua cjson模块编译笔记及错误解决方法
  • Lua教程(九):元表与元方法详解
  • Lua与C语言间的交互实例

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • lua操作excel方法分享
    • Lua中的文件I/O操作教程
    • Lua中调用C语言函数实例
    • Lua教程(十三):弱引用table
    • Lua中调用C++函数实例
    • C++遍历Lua table的方法实例
    • Lua UnPack函数用法实例
    • Lua编程示例(八):生产者-消费者问题
    • Lua中for循环语句的使用教程
    • Lua cjson模块编译笔记及错误解决方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有