Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

7-Zip turn off recursive for folders that match *.*

I'm using 7-Zip 19.00 64-bit. I want to compress the files in a folder, but exclude sub-directories; disable recursive.

The command that I'm executing:

7z u -mx9 -r- -bd wwwroot.7z C:\inetpub\wwwroot\*.*

C:\inetpub\wwwroot\ will have sub-directories in it that are named after domains.

C:\inetpub\wwwroot\domain.com (directory)
C:\inetpub\wwwroot\domain.org (directory)
C:\inetpub\wwwroot\domain.net (directory)
C:\inetpub\wwwroot\images (directory)
C:\inetpub\wwwroot\javascript (directory)
C:\inetpub\wwwroot\index.html
C:\inetpub\wwwroot\robots.txt
C:\inetpub\wwwroot\favicon.ico

7-Zip is including sub-directories that match the wildcard; domain.com, domain.org, domain.net will be in wwwroot.7z. images and javascript will be excluded. index.html, robots.txt, and favicon.ico will be in wwwroot.7z as expected.

Because the folders for sites/domains will change over time, I can't hard-code the list of folders to exclude.

I've tried to use the following and none of them work as I want:

7z u -mx9 -r- -bd wwwroot.7z C:\inetpub\wwwroot\.
7z u -mx9 -r- -bd wwwroot.7z C:\inetpub\wwwroot\

I've looked in the documentation for ways to exclude by attribute but couldn't find it. I've looked at the -x option, but it only seems to apply to files.

The system that I'm running this on is Windows Server 2012 R2.

@Mofi: wwwroot.7z should only contain files, No sub-directories.

To accomplish this task with WinRAR, I'd use this command:

rar u -ma5 -m5 -ep1 C:\inetpub\wwwroot\wwwroot.rar C:\inetpub\wwwroot\*.*
like image 322
Scott Jibben Avatar asked Dec 04 '25 03:12

Scott Jibben


2 Answers

The solution to compress only the files in specified directory non-recursive into a 7-Zip archive is:

7z.exe u -mx9 -bd -x!*\ -- wwwroot.7z C:\inetpub\wwwroot\*

The switch -x!*\ results in excluding all directories in directory C:\inetpub\wwwroot and all files in these directories. 7-Zip does not even try to access one of the subdirectories on using this switch as it can be seen with Sysinternals (Microsoft) free tool Process Monitor.

The wildcard * is used instead of *.* to compress into the archive file also files without a file extension. The 7-Zip help page Command Line Syntax explains the difference between * and *.* in comparison to Windows which interprets *.* always like *. WinRAR interprets *.* also different to * like 7-Zip and so also different in comparison to Windows.

Note: I do not understand why the default switch -r- even on being explicitly used on command line does not result in ignoring all files in all subdirectories as it should according to the explanation on help page -r (Recurse subdirectories) switch on using 7-Zip 19.00 (x86 or x64 version). -r- works on using *.txt instead of just *. So it looks like the behavior on adding also files in subdirectories on usage of wildcard pattern * or *.* with using implicit default -r- or on having specified this switch explicitly on command line is either a bug of 7-Zip 19.00 or a not good documented behavior if intentionally by design.

like image 191
Mofi Avatar answered Dec 05 '25 18:12

Mofi


@echo off
setlocal

set "target=C:\inetpub\wwwroot"

dir /b /a-d "%target%\*" > "%cd%\include.tmp"

pushd "%target%\" && (
    call 7z u -mx9 -bd "%cd%\wwwroot.7z" -ir0@"%cd%\include.tmp"
    popd
)

del "%cd%\include.tmp"

A list file can be created with the output of dir, which can give a list of just files. pushd into the target directory and run 7z. The %cd% will remain the same as delayed expansion is not used i.e. with !cd!. If you want script directory instead of current directory, change %cd% with %~dp0.

A list file may give you the flexibility that you may need.

like image 21
michael_heath Avatar answered Dec 05 '25 16:12

michael_heath



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!