MSFN Forum: Redirect 7z Output to a Directory - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Redirect 7z Output to a Directory Rate Topic: -----

#1 User is offline   Andrews 

  • Newbie
  • Group: Members
  • Posts: 32
  • Joined: 07-May 10
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 24 December 2011 - 11:14 AM

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.



Quote

@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



#2 User is online   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 9,111
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 24 December 2011 - 01:10 PM

I am not sure to get what you are asking. :unsure:

Can you post a short examples of the files and dir as they are and the way you want them compressed?

jaclaz

#3 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,195
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 24 December 2011 - 04:28 PM

The only logical way of doing this without potentially deleting something which failed to be added to a working compressed archive is:
  • Compress Directory Contents to Archive (in a temporary location)
    If successful
  • Test Archive integrity
    If successful
  • Extract Archive (to another temporary location)
    If successful
  • Compare extracted files with originals
    If successful
  • Delete originals and temporary ones
  • Move archive to Original Directory


#4 User is offline   Andrews 

  • Newbie
  • Group: Members
  • Posts: 32
  • Joined: 07-May 10
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 24 December 2011 - 04:41 PM

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.

#5 User is offline   Andrews 

  • Newbie
  • Group: Members
  • Posts: 32
  • Joined: 07-May 10
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 24 December 2011 - 04:52 PM

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.

#6 User is online   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 9,111
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 25 December 2011 - 11:24 AM

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

Quote

-so (write data to stdout) switch

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

jaclaz

This post has been edited by jaclaz: 25 December 2011 - 11:28 AM


#7 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,195
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 25 December 2011 - 02:25 PM

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.

#8 User is offline   Andrews 

  • Newbie
  • Group: Members
  • Posts: 32
  • Joined: 07-May 10
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 25 December 2011 - 08:04 PM

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.

#9 User is online   jaclaz 

  • The Finder
  • Group: Developers
  • Posts: 9,111
  • Joined: 23-July 04
  • OS:none specified
  • Country: Country Flag

Posted 26 December 2011 - 06:52 AM

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.tes...red-banana.html

jaclaz

This post has been edited by jaclaz: 26 December 2011 - 07:03 AM


#10 User is offline   Andrews 

  • Newbie
  • Group: Members
  • Posts: 32
  • Joined: 07-May 10
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 26 December 2011 - 09:51 AM

sorry, can't write for few days. small operation to finger tendon.

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy