Jump to content

Redirect 7z Output to a Directory


Recommended Posts

I want to use 7zip for its usual function, but with individually compressed files in their folders and original names. 7zip, unlike WinRar with the option Put each file to a separate archive, only does this with the -o switch when decompressing, not compressing.

I found a batch file at SourceForge (original version quoted at the bottom) that does it exactly as I need, but for a little difference: it leaves the compressed files and directories mixed on the original directory to where I copy the batch file and launch it. Not quite a problem if compressing only a handful of files, but when there are thousands of directories and files it requires a very long time to separate them, and as some are not compressed it can become a real headache, waste of time and patience.

I placed a redirection after the operation (added in green), but is doesn't work. The result at the end of the command is Access is denied. It is a trial directory and also a trial path. I tried the path for the new existing directory both with and without quotes with the same result.

Obviously, my problem is ignorance and that is what makes me ask for help. I searched the internet and found many tutorials, searched them, but not what I am looking for.

How can I redirect the output of the 7zip compressed files and directories to another directory, or maybe better, make the batch file make a new one and redirect the output of 7zip into it?

Any suggestions will be most welcome. Thank you.

Best wishes for Christmas and New Year.

@echo off

:begin

for /f "delims=" %%i in ('dir /b /ad') do (call :zipdir "%%i")

goto end

:zipdir

"C:\Program Files\7-Zip\7z.exe" a -t7z %1.7z %1\

if errorlevel 1 goto error

"C:\Program Files\7-Zip\7z.exe" t %1.7z >> "C:\Users\Administrator\Desktop\Compressed"

if errorlevel 1 goto error

echo rmdir /S /Q %1\

goto :eof

:error

goto :eof

:end

Link to comment
Share on other sites


The only logical way of doing this without potentially deleting something which failed to be added to a working compressed archive is:

  1. Compress Directory Contents to Archive (in a temporary location)
    If successful
  2. Test Archive integrity
    If successful
  3. Extract Archive (to another temporary location)
    If successful
  4. Compare extracted files with originals
    If successful
  5. Delete originals and temporary ones
  6. Move archive to Original Directory

Link to comment
Share on other sites

Thank you jaclaz fpr coming on rescue once more.

What I want to do is simple. I want to compress many directories with files. They are in branches (or trees) like as they normally appear on Windows Explorer.

After compression I want them organised the same way as before compression (similar trees) keeping the same order and individually comperessed. For instance, you have a tree with directories and files and compress them. After compressing they are in an identical tree, but compressed.

The batch file does it exactly that way. You can try it and see the result. I put the batch file on the directory I want to compress, double click and it and it does the job. The only problem is that it saves the compressed directories and files on the original directory where the files to compress are. I want to redirect its output somewhere else and not mix up everything.

I hope I explained better now, but if you try the batch you will see exactly what I mean.

Link to comment
Share on other sites

Thank you for trying to help Wise Owl. I am starting to believe that the more I try to explain what I want, the more I mix it up.

What you say is very complicated end endless when having lots of files. That batch does the job already. So all I need is only to add some code to the batch, so that it outputs its job (the compressed directories with files) to another directory and NOT into the original directory where the uncompressed directories and files already are.

Link to comment
Share on other sites

Let's see if I understand. I normally understand by examples.

Let alone for the moment the multi-file/multifolder issue.

You have a single file:

C:\myfiles\mydir\mysinglefile.ext

you want to use 7-zip (which BTW is NOT IMHO the best choice for single file archiving) to have it compress that file to:

C:\Users\Administrator\Desktop\Compressed\myfiles\mydir\mysinglefile.7z

Is this what you want? :unsure:

BTW, to output to stdout in 7zip you use the

-so (write data to stdout) switch

but this WON'T HELP you unless you first create the target tree.

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

I thought that they had the following:

SourceDir
\SubFolder
\ChildDir1
File789.ext
Filexyz.ext
File1.ext
File2.ext
File3.ext
\SecondSub
\ChildFolder
File123.ext
Fileabc.ext
File7.ext
File8.ext
File9.ext
ThisScript.cmd

When they execute ThisScript.cmd they wish it to produce two 7z files, (in a folder named 'Compressed' on the Desktop), %UserProfile%\Desktop\Compressed\SubFolder\SubFolder.7z and %UserProfile%\Desktop\Compressed\SecondSub\SecondSub.7z.

However I see little point in recreating the top level structure since 7-zip will compress the directories SubFolder and SecondSub not just their contents.

I would probably suggest something like this, (untested):

@ECHO OFF &SETLOCAL ENABLEEXTENSIONS DISABLEDELAYEDEXPANSION
(SET COMPDIR=%USERPROFILE%\DESKTOP\Compressed)
FOR %%# IN (%COMPDIR%) DO (SET _=%%~dp#)
IF /I "%_%"=="%~dp0" GOTO :EOF
(SET UTILDIR=%PROGRAMFILES%\7-ZIP)
IF /I NOT "%CD%\"=="%~dp0" PUSHD %~dp0
FOR /D %%# IN (*) DO (
IF NOT EXIST "%COMPDIR%" MD "%COMPDIR%"
"%UTILDIR%\7Z.EXE" A -T7Z "%COMPDIR%\%%#.7z" "%%#"
)

You should be able to add the integrity verification command line to the above as required.

Link to comment
Share on other sites

Thank you both.

jaclaz

The –so switch, if I used it right, did not redirect the compressed files and directories. It just placed them on the same directory with the originally not compressed.

To see an easy example as you suggest try the following please.

  • Make a new folder on your desktop.
  • Copy a directory with sub-directories and subs and files.
  • Put the batch file on my first post in this directory and double click it.
  • See the result.
  • You will find the original structure compressed where you put the batch, starting with that directory.
  • That is what I get and nearly what I want.

What I want is 7zip to redirect is the compressed structure to another directory. Just this.

You say 7zip is NOT IMHO the best choice for single file archiving. What would you choose that would be simple?

Ysöwl

Yes, the structure is as you wrote on the first part of you post.

Your script works as I need and it can even create a new directory if it doesn’t already exist. All I have to do is to change the path or the name if I need.

Thank you very much again.

Link to comment
Share on other sites

The gzip format was born with this idea of a single compressed archive for each source file.

gzip allows streaming and thus piping and redirection, something that 7z compression may not.

Try (still following my previous example):

"C:\Program Files\7-Zip\7z.exe" a dummy -tgzip -so C:\myfiles\mydir\mysinglefile.ext  >C:\Users\Administrator\Desktop\Compressed\myfiles\mydir\mysinglefile.gz

(of course the target directory must exist)

;)

Basically you fell for the chocolate covered banana :ph34r::

http://homepages.tesco.net/J.deBoynePollard/FGA/put-down-the-chocolate-covered-banana.html

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...