Home > Net >  How do I exclude a subfolder of a subfolder in compress-archive
How do I exclude a subfolder of a subfolder in compress-archive

Time:05-06

I want to zip a folder containing files, and subfolders containing more files and more subfolders. But I want exclude just one subfolder of a subfolder. I have seen many various complicated answers but none seem to really work on this scenario.

folder structure

temp/
   a/
     aa/
        aaa/
            aaaa/
                 filesinaaaa.txt
            filesinaaa.txt
        filesinaa.txt
     at.txt
   b/
     bb/
     bt.txt
   c.txt

I want to exclude all files in a/aa/aaa/ while keeping the resulting zip in the same dir structure. And if possible remove the files that were included in the zip.

So result expected is

temp/
  a/
     aa/
        aaa/
            aaaa/
                 filesinaaaa.txt
            filesinaaa.txt
  result.zip

In linux it is as simple as

cd temp
zip -rm result.zip . -x "a/aa/aaa/*"

what is the simplest way to do this in powershell?

answers i've tried do not really work on subfolder of a subfolder.

Exclude sub-directories from Compress-Archive Powershell Cmd

How do I exclude a folder in compress-archive

over complicated result Archive folder without some subfolders and files using PowerShell

CodePudding user response:

You can use 7z CLI to do so, here is the doc explaining the usage of -x option in 7z

Showing an example of the option below for reference

The directory structure explained in the question:

C:\Users\adam_\Desktop\temp〉dir -R
a  b  c.txt

./a:
aa  at.txt

./a/aa:
aaa  filesinaa.txt

./a/aa/aaa:
aaaa  filesinaaa.txt

./a/aa/aaa/aaaa:
filesinaaaa.txt

./b:
bb  bt.txt

./b/bb:

Creating the archive:

PS C:\Users\adam_\Desktop\temp> 7z a -tzip result.zip . -x!a/aa/aaa/*

7-Zip 19.00 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2019-02-21

Scanning the drive:
5 folders, 4 files, 63 bytes (1 KiB)

Creating archive: result.zip

Add new data to archive: 5 folders, 4 files, 63 bytes (1 KiB)


Files read from disk: 4
Archive size: 1217 bytes (2 KiB)
Everything is Ok

Result archive details:

PS C:\Users\adam_\Desktop\temp> 7z l .\result.zip

7-Zip 19.00 (x64) : Copyright (c) 1999-2018 Igor Pavlov : 2019-02-21

Scanning the drive for archives:
1 file, 1217 bytes (2 KiB)

Listing archive: .\result.zip

--
Path = .\result.zip
Type = zip
Physical Size = 1217

   Date      Time    Attr         Size   Compressed  Name
------------------- ----- ------------ ------------  ------------------------
2022-05-05 09:48:37 D....            0            0  a
2022-05-05 09:48:12 D....            0            0  a\aa
2022-05-05 09:47:45 D....            0            0  a\aa\aaa
2022-05-05 09:48:12 ....A           13           13  a\aa\filesinaa.txt
2022-05-05 09:48:37 ....A           13           13  a\at.txt
2022-05-05 09:49:20 D....            0            0  b
2022-05-05 09:48:58 D....            0            0  b\bb
2022-05-05 09:49:20 ....A           19           19  b\bt.txt
2022-05-05 09:49:56 ....A           18           18  c.txt
------------------- ----- ------------ ------------  ------------------------
2022-05-05 09:49:56                 63           63  4 files, 5 folders
  • Related