Menu

Console commands to output the total size of files in a password protected archive

Help
2024-02-28
2024-03-04
  • Stefan Martens

    Stefan Martens - 2024-02-28

    Hi!
    Looking for a command sequence which outputs the total size of files in a password protected archive.

    Example data:
    Archive name: MyArchive.7z
    Archive contents: Several files in different folders with a total size of 10,000,000 bytes
    Password: MyPassword
    Output: 10000000 (nothing more, if possible)

    Thanks for any efforts taken!

     

    Last edit: Stefan Martens 2024-02-28
  • Igor Pavlov

    Igor Pavlov - 2024-02-28
    7z l -pMyPassword MyArchive.7z
    
     
  • Stefan Martens

    Stefan Martens - 2024-02-28

    Thanks Igor, but I want ONLY the total size of all files in the archive, nothing more.

    This works for me in PowerShell: (a bit complicated...)
    It appends file name and size to result.txt like this
    "Archive.7z" 10000000

    Add-Content -Path "result.txt" -Value ('"Archive.7z" ' + (`.\7z l -slt -pMyPassword Archive.7z | Select-String -Pattern "^Size" | ForEach-Object { ($_ -split '\s+')[2] } | Measure-Object -Sum | Select-Object -ExpandProperty Sum))
    

    But I could not find a working CMD solution so far :-)

     
  • Deyan Delchev

    Deyan Delchev - 2024-03-01

    Maybe this will be useful:
    for /f "tokens=2" %I in ('7za.exe t -pMyPassword archive.7z^|find "Size:"') do @echo %I

     

    Last edit: Deyan Delchev 2024-03-01
  • Stefan Martens

    Stefan Martens - 2024-03-02

    Thanks you very much, Deyan!
    It works!

    In the meantime I had embedded my powershell-solution in a command line, which I could use in CMD.

    But your solution is much shorter, and so very useful!

    But I have a question: In case password, filename, or paths used contain blanks, then it should look like this:

    for /f "tokens=2" %I in ('7za.exe t -"pMyPassword" "archive.7z"^|find "Size:"') do @echo %I

    This works, too.

    However, when I use the path for 7z.exe in double quotes:

    "D:\PortableApps\7-ZipPortable\App\7-Zip64\7z.exe"

    it won't work

    This, however, works:

    D:\PortableApps\7-ZipPortable\App\7-Zip64\7z.exe

    What do I have to do, in case the path to my 7z-pgm-folder contains blanks?

     

    Last edit: Stefan Martens 2024-03-02
  • Deyan Delchev

    Deyan Delchev - 2024-03-02

    I'm glad to help.
    You can try this command:
    for /f "tokens=2" %I in ('call "C:\programs portable folder\7za.exe" t -p"My Strong Password" "C:\some archive folder\some test archive.7z"^|find "Size:"') do @echo %I

     
  • Stefan Martens

    Stefan Martens - 2024-03-02

    Thanks again, Deyan!
    So, inserting
    call
    does the trick. :-)
    It's WORKING!!!

     
  • Stefan Martens

    Stefan Martens - 2024-03-04

    Sorry about bothering you again.

    I modified your command as follows:

    for /f "tokens=2" %I in ('call "C:\programs portable folder\7za.exe" t -p"My Strong Password" "C:\some archive folder\some test archive.7z"^|find "Size:"') do @echo %I >> totalsize.txt
    

    This writes the size to totalsize.txt
    So far so good.
    But I also want to add the archive path to totalsize.txt, but in the same line!
    Like this:
    some test archive|123000
    another archive|10000

    How do I do that?
    Of course I know this command:

    echo C:\some archive folder\some test archive.7z >> totalsize.txt

    But this would write the path to a separate line. :-(

    To explain the reason:
    I want to use the command in a batch-file for many archives to create a text-file which I can process with Excel.

     

    Last edit: Stefan Martens 2024-03-04
  • Deyan Delchev

    Deyan Delchev - 2024-03-04

    I have a following suggestion then:

    @ECHO OFF
    SETLOCAL EnableDelayedExpansion
    
    FOR /F %%A IN ('DIR *.7z /A:-D /B') DO 7za.exe t %%A |find "Path =" >> paths.txt
    FOR /F %%B IN ('DIR *.7z /A:-D /B') DO 7za.exe t %%B |find "Size:" >> sizes.txt
    
    < sizes.txt (
       FOR /F "delims=" %%C IN (paths.txt) DO (
          SET /P SIZES=
          ECHO %%C;!SIZES!
       )
    ) > combined.txt
    
    ENDLOCAL
    
     
  • Stefan Martens

    Stefan Martens - 2024-03-04

    Thanks Deyan!
    I also learned about "EnableDelayedExpansion" when asking "chat.openai.com" for help earlier...

    Problem is, that your suggestion is quite extensive, several lines that is.

    What I need is a single line solution, because I want to use it in a batch file. That's what I tried to explain.

    Couple of years ago, I found and used a simple "DOS"-solution for appending strings to a text-file without line feed. But unfortunately I don't remember anymore how I did it ... :-(

    Actually, at the moment I've been using perl scripts for creating batch files for projects such as this one. And I now finally managed to create a working "single-line"-solution, which is based on the PowerShell command, I posted here earlier. I use it in CMD this way:

    powershell -Command "Add-Content ...

    Anyway, I can use your suggestion for improving my skills, for learning... :-)
    And I thank you once again for all your efforts taken!

    Best regards,
    Stefan

     
  • Deyan Delchev

    Deyan Delchev - 2024-03-04

    For now I don’t have a one line solution. If it needs to be used for processing a many files in different folders, with (possibly) different types of archives (I’m trying to think “globally”)… it won’t be exactly a “human readable” line.

    You can think in some directions:
    1. A “pure” script (cmd, PowerShell, VBS, PERL, etc.) solution – these are instruments mainly for automating work;
    2. A script solution with an external tool(s). There are Cygwin, Yori and many other tools which could be useful. I don’t like this way, but it could give an easy and clean result;
    3. Some program language will do the trick for sure. I am not a programmer and I can’t help with this, but it won’t be difficult for someone with such skills to provide a solution.

    You can check in the Stack Overflow and the DOS Batch Forum for help – there are many responsive (and skilled) people there.

    I will think about this problem several days – it could be useful for me too to have such script – and if I get something useful I will post here what I found.

     

Log in to post a comment.