Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Help with add files to separate archive

Help
Eric
2011-11-28
2012-12-07
  • Eric
    Eric
    2011-11-28

    I've been searching and reading within this forum to be able to get 7-zip to perform these actions for me but haven't been able to get this to work.  I have been using the GUI version but read that my tasks could be completed easier using the command line version.  I'm trying to create a batch file to drop into a folder, double click it and have all files in that folder be created into a separate .7z file.  I've created this command line for 7-zip but can't even get this to work:

    for /D %%A in (*) do "c:\program files\7-Zip\7z.exe" a -t7z -m9=LZMA2 -md=256m "%%A.7z" "%%A"

    If it doesn't make sense, this is what setting I'm trying to create:

    .7z file
    Ultra compression
    LZMA2
    256MB Dictionary
    Keeping the same file name within the same directory

    Can anybody help me to create a batch file with the above parameters so that I could just drop the batch file in the folder and double click it so that 7-zip starts compressing the files one by one?  Thanks

     
  • Igor Pavlov
    Igor Pavlov
    2011-11-28

    -mx9 -m0=lzma2:d256m

     
  • Eric
    Eric
    2011-11-28

    I still can't seem to get it to work, comes back "The syntax of the command is incorrect".  This is what I'm typing in the command line under the folder I want to compress:

    for /D %A in (*) do "c:\program files\7-Zip\7z.exe" a -t7z -mx9 -m0=lzma2:d256m "%A.7z" "%A"

    and this is the contents of my batch file:

    @echo off

    for /D %%A in (*) do "c:\program files\7-Zip\7z.exe" a -t7z -mx9 -m0=lzma2:d256m "%%A.7z" "%%A"

     
  • fernando
    fernando
    2011-11-28

    Why do you use FOR/D when you do not want to work on Directory names?

     
  • Eric
    Eric
    2011-11-28

    I really only have a limited understanding of what the command line is saying.  What should I use instead?

     
  • fernando
    fernando
    2011-11-28

    @ECHO OFF
    FOR %%A IN (*) DO "c:\program files\7-zip\7z.exe" a -mx9 -m0=lzma2:d256m "%%~nA.7z" "%%A"
    DEL "%~n0.7z"
    
     
  • Eric
    Eric
    2011-11-28

    Thank you so much, works like a charm.  Just a quick concern, in the command prompt during compression, it states:
    DEL: WARNING: The system cannot find the file specified

    Compress.7z: WARNING: The system cannot find the file specified.

    It repeats it during each file compress action.  It still compresses everything in the folder, don't know what this warnings mean tho.  Thanks!

     
  • fernando
    fernando
    2011-11-28

    DEL "%~n0.7z"
    is a separate line and used in a batch script only; not from a command line.

    You indicated that the batch script would be placed in the same directory in which it would be executed.
    The script will archive ALL files, including batchFileName.bat.
    That DEL line translates to DELete batchFileName.7z

    Note:
    If there are two files - for example fileName.DAT and fileName.TXT, the both will be archived in fileName.7z
    If you would prefer fileName.DAT => fileName.DAT.7z and fileName.TXT => fileName.TXT,.7z
    change the "%%~nA.7z" to "%%A.7z"

    "%%~nA" == filename of %%A without the .EXTension

     
  • Eric
    Eric
    2011-11-28

    I removed the line from the batch file and everything is working perfectly.  Thanks so much for your help.

     

  • Anonymous
    2012-02-16

    Hello,

    I stumbled upon this thread and it was a GREAT help to me!  Thank you all for making this community awesome!

    I do have one question…

    How would I change the command to make it so instead of files, it was folders that got turned into 7z archives?
    For example, if I want to put a batch file in a folder, and then have each folder IN that folder created into separate 7z archives - how would I write that in the CMD?

    Thank you!

     

  • Anonymous
    2012-02-16

    Apologies…I figured it out.  Total brain glitch on my part.

    I misread the opening few posts - the answer was there for me :)

    For /D %%A in (*) do "c:\program files\7-Zip\7z.exe" a -t7z -mx9 -m0=lzma2:d256m "%%A.7z" "%%A"

    Worked like a charm!

    Ben

     

  • Anonymous
    2012-02-16

    Sorry, but I was wrong.  It's not working.

    The command itself seems to work but for some reason it ignores folders.  I place the batch file in a folder, where there are 15 folders I want archived to 7z.  It stops after 9 folders.  Just ignores the others.  Why is this?  How would I fix it?

    :(

    Please help, thank you!

     

  • Anonymous
    2012-02-17

    I found out that it's due to something related to the dictionary and word values.  If I lowered them both to 64m (vs. 256m) - the folders work fine - all of them.

    Why would a higher dictionary and / or word values cause this batch file to skip folders or just plain not execute?

     

  • Anonymous
    2012-08-30

    super thanks…. !!! this 1 line based on @ikxcsshcm7 's reply saved our skins. We encrypted all files in the containing folder with pass-word as the filenames themselves and wrote another script to update the filenames.

    FOR %%A IN (*) DO "c:\program files\7-zip\7z.exe" a -p"%%~nA" -tzip "%%~nA.zip" "%%A"