This is the internal zip packer from Total Commander:
However I want to use my 7zip packer. When I select 5 files, I get 5 separate .zip archives for each selected file. How do I do that in 7zip?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityI am not sure you can do what you are suggesting using the graphical user interface, but you can certainly from the command line:
FOR %i IN (*.*) DO 7z.exe a "%~ni.7z" "%i"
You would need to change directory (the cd
command) to the F:\Downloads
directory first, or whatever directory you would want to perform the mass compression. Also, it is easy enough to substitute in *.exe or whatever extension you want to filter for to just compress those documents.
And the secret decoder ring:
%i
is a variable that holds the file name for each step in the loop(*.*)
is the selection criteria, it could easily be *.exe or similar7z.exe
is the command line version of 7-Zip%~ni
- this expands the %i variable to just the file name - no extensionIf you wanted to just add the folders for a given directory, the command is a bit more complex as by default FOR just works with files. We need to feed it some additional information:
FOR /F "usebackq delims=?" %i IN (`DIR /B /A:D`) DO 7z.exe a "%i.7z" "%i"
This works because of a few pieces of what seems like magic:
/F
tells FOR to iterate over the expanded value in ()usebackq
tells FOR that I am going to pass a command and use the output to iteratedelims=?
tells FOR that I want to break tokens apart on the ?
- an illegal character in file names and directories. I only want one token./B
in DIR
is for bare format - just the name/A:D
in DIR
is for restricting the results by attribute, the D is for directoriesIf you want to encapsulate this inside of a batch file, the only change you will need to make is to double escape the %i variable:
FOR %%i IN (*.*) DO 7z.exe a "%%~ni.7z" "%%i"
(*.*)
actually is an impressed face by a user who has become confused of trying to understand your script code.
Jan 14, 2019 at 18:32
I like Goyuix's answer. However, if you are using Windows 7 (or have Power Shell installed) and also use the command-line version of 7-zip, 7za.exe, then you can run this command:
dir | ForEach-Object { & "7za.exe" a $_.BaseName $_.Name }
You can also change "dir" to "dir *.exe" to only select executable files.
One nice thing about Power Shell is that you are working with objects instead of strings. So you could get really specific if you wanted to. For example:
dir *.csv | ? { $_.Length -lt 18900 -and $_.LastWriteTime -ge [DateTime] "07/01/2011" } | ForEach-Object { & "7za.exe" a $_.BaseName $_.Name }
This will only include files that:
EDIT If you want zip files instead of 7-zip files, use InfoZip's zip.exe instead.
7z a -tzip archive.zip *.exe
I've just been working on exactly this problem and one of the issues I found was duplication of the folder name within the archive, i.e. when compressing a folder called "foo" an archive containing .\foo\fighters.txt
is produced instead of just one containing fighters.txt
.
The answer, therefore is to go into the directory being processed and have the archive appear in the previous (root) directory, then return to that directory in order to process the next folder.
It is also important to have 7ZIP ignore existing ZIP and CMD files within the root directory to prevent them ending up within the archive, hence the -x!*.zip
and -x!*.cmd
exclusion arguments.
This script will also maintain recursive folder structures.
@echo off
REM Ensures variables set within the batch file are local to
REM this script only and will be removed when finished.
setlocal
if exist *.zip goto zip_exist
for /d %%X in (*) do (
cls
cd /D %%X
"e:\Program Files\7-Zip\7z.exe" a -r -x!*.zip -x!*.cmd "..\%%X.zip" "*.*"
cd ..
)
goto end
:zip_exist
Echo.
Echo Note: for this script to work, compression of
Echo pre-existing zip files is not possible.
:end
pause
The above code should be copied to a batch file such as "compress_dirs.cmd" and run as admin from the directory containing the folders you wish to compress.
Hope this helps.
If you need to keep the folder structure, and save each file in its own structure, try this:
Save this command in a file with extension .bat and put it in the folder you need to zip:
FOR /R %%i IN (*.*) DO "C:\Program Files\7-Zip\7z.exe" a "%%i.7z" "%%i" -sdel
Note:
-sdel: Deletes the original file (optional, if you only want to keep the compressed file)
You can download and install 7zip plugin for Total Commander from http://www.ghisler.com/plugins.htm Once you do that on the right side of the window from your screenshot there will be another selection for 7zip compression. With that option you can accomplish exactly what you are asking for.