AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1599429
Accepted
klepp0906
klepp0906
Asked: 2020-11-04 07:44:29 +0800 CST2020-11-04 07:44:29 +0800 CST 2020-11-04 07:44:29 +0800 CST

WinRAR 批处理文件,用于将每个文件夹中的第一个子目录压缩成 zip

  • 772

我正在努力寻找一个解决方案来扫描目录中的所有文件夹,并在其中的第一个/唯一子文件夹的内容的所述文件夹中创建一个 zip 文件。

这是一个示例文件夹/结构/层次结构

I:\Unsorted\Sony - PlayStation Vita\NoNpDrm Physical\SENRAN KAGURA SV [PCSE00398] [NTSC]\PCSE00398

我想把里面的内容PCSE00398压缩成一个zip(同名)并留在I:\Unsorted\Sony - PlayStation Vita\NoNpDrm Physical\SENRAN KAGURA SV [PCSE00398] [NTSC]文件夹中。

batch-file winrar
  • 1 1 个回答
  • 503 Views

1 个回答

  • Voted
  1. Best Answer
    Io-oI
    2020-11-05T04:40:45+08:002020-11-05T04:40:45+08:00
    @echo off 
    
    set "_flag=a -ep1 -m5 -cfg- -y -o+"
    cd /d "%~dp0" && for /d /r "I:\test\." %%i in (*
    )do 2>nul tree.com /a "%%~dpnxi"|findstr /bl \\--- >nul || (
    "%ProgramFiles%\WinRAR\Rar.exe" %_flag% "%%~i" "%%~dpni" |find/i ".rar" )

    要到达子文件夹的最后一级,只需要检查每个子文件夹中是否有另一个文件夹,否则,压缩这个最后一级文件夹。

    您可以使用for /d /r循环,它将遍历所有文件夹,并在循环内的每个文件夹中,将tree命令与 一起使用findstr,您可以在其中检查当前文件夹是否有更多子文件夹。

    • FOR /R - Loop through files (recursively)
      FOR /D - Loop through several folders/directories
    • The option /D /R is undocumented, but can be a useful combination,
      while it will recurse through all subfolders the wildcard will only
      match against Folder/Directory names (not filenames)
      Note: Source linked to ss64.com

    您可以在所有子文件夹中递归地使用,并通过使用findstr "string" ( )检查每个输出,重定向到操作员,如果在命令输出中找不到此字符串,则执行操作 ( ) :tree "current_looping_folder" /afor /d /r"\---"||Rar\---tree\---Last Folder

    Folder PATH listing
    Volume serial number is A0AD-DC56
    F:\SUPER_USER\Q1599429
    \---Last Folder

    上面的输出来自我所在的文件夹,F:\SUPERUSER\Q1599429这里有Last Folder子文件夹,但如果我在F:\SUPER_USER\Q1599429\Last Folder子文件夹中,我会得到以下输出:

    Folder PATH listing
    Volume serial number is A0AD-DC56
    F:\SUPER_USER\Q1599429\Last Folder
    No subfolders exist

    如果findstr没有找到字符串"\---",我在当前文件夹中没有子文件夹,这是最后一个文件夹:

    F:\SUPER_USER\Q1599429\Last Folder
    

    在这种情况下,如果没有子文件夹,命令命令将不会成功,那么让操作员在您实际文件夹级别的最后一个文件夹中执行命令...tree "Actual_Loop_Folder" /a | findstr "\---"||Rar.exe

    • Obs.: 1\转义字符需要额外\的findstr
     ... tree.com /a "%%~dpnxi"|findstr /bl \\--- ... 
    • Obs.: 2这将试图说明||操作员如何机械地工作:
                             command1 || command2
                     execute command1 || only execute command2 (if) command1 fails
              if tree folder /a fails || there is no subfolder in it
          there is no subfolder in it || this is the the last subfolder
           this is the last subfolder || run rar flags in the \Last Folder
    tree /a "%~fi"|findstr "\---">nul || Rar "I:\Unsorted\...\PCSE00398"
    • Obs.: 3如果您需要使用WinRar.exeinstead运行Rar.exe,只需删除/替换/编辑:
    "%ProgramFiles%\WinRAR\Rar.exe" %_flag% "%%~i" "%%~dpni" |find/i ".rar"
    
    "%ProgramFiles%\WinRAR\WinRar.exe" %_flag% "%%~i" "%%~dpni" 
    • Obs.:4您可以编辑为您的Rar/WinRar命令使用您的自定义标志/开关:
    <Commands>
            a    ==  Add files to archive 
    
    <Switches>
    
          cfg-   ==  Disable read configuration 
          ep1    ==  Exclude base directory from names 
      m<0..5>    ==  Set compression level (0-store...3-default...5-maximal) 
       o[+|-]    ==  Set the overwrite mode 
            y    ==  Assume Yes on all queries 
    -----------------------------------------------------------------------
    set "_flag=a -ep1 -m5 -cfg- -y -o+"

    • 一些进一步的阅读:

    [√]循环

    [√] For /D 循环

    [√] For /R 循环

    [√] WinRar.exe | rar.exe /?

    [√] CMD/蝙蝠操作员/dostips.com

    [√]条件执行 || && ...

    • 2

相关问题

  • 我需要帮助弄清楚如何让我的批处理文件停止损坏

  • Net Stop,如何在第一个服务“正在停止...”时停止第二个服务

  • 批量密码生成器

  • Windows 2016 不会运行 .bat 文件

  • 如何制作批处理脚本来备份具有唯一目标文件夹的特定文件(Windows)[关闭]

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve