You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何递归删除文件夹及内容?含Inno Setup安装时删指定文件夹方法

1. 递归删除文件夹及其所有内容的通用方法

针对不同操作系统和场景,常见实现方式如下:

  • Windows 命令行:使用rmdir命令搭配/s /q参数,示例:
    rmdir /s /q "C:\目标文件夹路径"
    
    其中/s表示删除文件夹及所有子内容,/q表示静默删除不弹出确认提示。
  • Linux/macOS 终端:使用rm命令搭配-rf参数,示例:
    rm -rf /目标文件夹路径
    
    -r(或-R)表示递归处理层级内容,-f表示强制删除不触发确认提示。
  • Python 脚本:通过shutil模块的rmtree方法实现,示例:
    import shutil
    shutil.rmtree("目标文件夹路径")
    
2. Inno Setup安装过程中递归删除{app}\manuals文件夹的方法

在Inno Setup中有两种可靠实现方式:

  • 方法一:编写递归删除函数
    在脚本的[Code]段添加自定义Pascal函数,遍历并删除所有文件与子文件夹:
    [Code]
    procedure DeleteDirectoryRecursive(Path: string);
    var
      FindRec: TFindRec;
    begin
      if FindFirst(Path + '\*', FindRec) then
      begin
        try
          repeat
            if (FindRec.Name <> '.') and (FindRec.Name <> '..') then
            begin
              if FindRec.Attributes and FILE_ATTRIBUTE_DIRECTORY <> 0 then
                DeleteDirectoryRecursive(Path + '\' + FindRec.Name)
              else
                DeleteFile(Path + '\' + FindRec.Name);
            end;
          until not FindNext(FindRec);
        finally
          FindClose(FindRec);
        end;
      end;
      RemoveDir(Path);
    end;
    
    procedure CurStepChanged(CurStep: TSetupStep);
    begin
      if CurStep = ssInstall then
      begin
        DeleteDirectoryRecursive(ExpandConstant('{app}\manuals'));
      end;
    end;
    
    该代码会在安装阶段启动时,自动递归删除指定文件夹及其全部内容。
  • 方法二:调用系统命令实现
    通过Exec函数调用Windows原生rmdir命令,示例:
    [Code]
    procedure CurStepChanged(CurStep: TSetupStep);
    var
      ErrorCode: Integer;
    begin
      if CurStep = ssInstall then
      begin
        Exec('cmd.exe', '/c rmdir /s /q "' + ExpandConstant('{app}\manuals') + '"', '', SW_HIDE, ewWaitUntilTerminated, ErrorCode);
      end;
    end;
    
    这种方式依赖系统命令,适合简单场景,无需编写复杂递归逻辑。

内容的提问来源于stack exchange,提问作者tmighty

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 17:52:17