压缩子文件夹中的单独文件 [英] Compress separately files within subfolders

查看:35
本文介绍了压缩子文件夹中的单独文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,感谢您的回答,

Hi all and thanks for the answers,

首先,我试图找到问题的答案,但我什么也没找到.

Firstly, I tried to find the answer to my problem but I did not find anything.

我有一个文件夹和子文件夹树,我想使用 7zip 分别压缩这些文件夹中的文件.

I have a tree of folders and sub-folders and I want to use 7zip to compress the files within those folders separately.

我从这个网站上得到了这段代码,它做了我想要的,但它将压缩文件放在主文件夹中:

I have got this piece of code from this very website, it does what I want to get but it places the compressed files on the main folder:

    set extension=.*
    for /R %%a in (*%extension%) do "%sevenzip%" a -mx "%%~na.zip" "%%a"

我想知道是否可以获取每个文件的 zip 文件并将其放在包含源文件的子文件夹中.或者执行上述过程并将每个 zip 文件放在适当的子文件夹中.

I wonder if I can get a zip file of every file and have it in the sub-folder containing the source file. Or doing the process above and place every zip file inside the appropriate sub-folder.

我尝试使用双 'For/d' 但我无法得到它:

I tried with a double 'For /d' but I was unable to get it:

    cd /d %~dp0

    rem 7z.exe path

    set sevenzip=

    if "%sevenzip%"=="" if exist "%ProgramFiles(x86)%7-zip7z.exe" set 

    sevenzip=%ProgramFiles(x86)%7-zip7z.exe


    if "%sevenzip%"=="" if exist "%ProgramFiles%7-zip7z.exe" set 

    sevenzip=%ProgramFiles%7-zip7z.exe


    if "%sevenzip%"=="" echo 7-zip not found&pause&exit

    for /D %%O in (*) do (

        for /R %%I in ("%%O*") do (
            "%sevenzip%" a -mx "%%~na.zip" "%%a"

            ::        rd /s /q "%%I"  **Because I do not want to delete anything by now.

        )
    )

再次感谢.

亚历克斯.

推荐答案

如果您的文件夹结构有些复杂,那么您最好使用 dir 中的普通列表:

If you have somewhat complex folder structure then you probably better use plain list from dir:

  dir /a:-d /s /b /o

只需在 for 中使用它的输出:

Just use its output in for:

for /f %%f in ('dir /a:-d /s /b /o') do (
  echo %%f <-- %%f is a full path to a file, do something with it
)

顺便说一句,7zip 有一个有用的选项 -sdel 可以在成功创建存档后删除源文件.

Btw, 7zip has useful option -sdel to remove the source file when archive has been created successfully.

这篇关于压缩子文件夹中的单独文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆