Menu

Extract all files in subfolders

Help
Anonymous
2010-02-02
2019-08-02
  • Anonymous

    Anonymous - 2010-02-02

    Hello,

    I'm trying to get this command line to  work.

    7z e *.zip -r

    Basically, I want to extract all zip files in all the subfolders and keep their destination intact.

    In addition, how do I set them to auto rename themselve while unzipping too.

    Many thanks.

     
  • Giorgio Tani

    Giorgio Tani - 2010-02-02

    You should use x to extract the archived objects keeping the folders structure, rather than e that extracts all files to the same output path.
    The policy to apply if the destination contains objects with the same name of objects being extracted can be set with the -ao patameter: -aoa overwrites all existing files, -aos skip extracting if a file with same name exists in destination, -aou rename files being extracted in case of naming conflict, -aot instead renames existing files.

     
  • Anonymous

    Anonymous - 2010-02-03

    Hi,

    Thanks, I haven't tried this yet. (will do it later back home)

    So I assume the command line would be.

    7z x *.zip -r -aou

    The original command line did not work at all. It does not seem to be able to find any zipped files in any of the subfolders. Which I assume, the name of the subfolders do not matter much, except they must be located in the root directory of where the 7z.exe is executed?

    e.g.

    c:\sub1\1.zip
    c:\sub2\2.zip
    c:\sub3\3.zip

    command line done from c:\  and will extract all 1, 2, 3.zip files from those sub directories.

    Thanks again

     
  • fernando

    fernando - 2010-02-03

    Switch -r (Recurse subdirectories) is not for your task. One method is to use a FOR/IN/DO loop to recurse the directory structure performing the desired 7-Zip command upon each ZIP archive.

    Look at the below command line. If it "echos" correct, remove the "ECHO."…

    FOR /R "C:" %I IN (*.zip) DO ECHO.7z x "%I" -aou
    
     
  • Anonymous

    Anonymous - 2010-02-03

    Hi ikxcsshcm7,

    I'm sorry but I don't understand how to use that command line.

    How exactly do I execute that from the cmd window @ the root directory?

    Thanks

     
  • fernando

    fernando - 2010-02-04

    Ensure 7z is in the root directory or on PATH.
    Type (or paste) the above command line at the CMD prompt in the root directory and press Enter.

     
  • Anonymous

    Anonymous - 2010-02-10

    Thanks alot ikxcsshcm7,

    Your command line,

    FOR /R "C:" %I IN (*.zip) DO ECHO.7z x "%I" -aou

    Does not work exactly and it did not unzip any of the zipped files in the subfolders.

    However, using this without ECHO

    FOR /R "C:" %I IN (*.zip) DO 7z x "%I" -aou

    does work, except it seem to extract all the files into the root directory instead of going to their respective sub folders.

    In a general sense, it is of no big issue, but would be useful if it can do it.

    Many thanks again :)

     
  • fernando

    fernando - 2010-02-11

    Sorry… left a bit off. Try:

    FOR /R "C:" %I IN (*.zip) DO @ECHO.7z x "%I" -aou -o"%~dpI"
    

    If it "echos" correctly, remove the "@ECHO."

     
  • fernando

    fernando - 2010-02-11
    $ :: Directory Structure Before Extraction:
    $ TREE X: /A /F
    Folder PATH listing for volume TEST
    Volume serial number is 1234-ABCD
    X:\
    +---sub1
    |       archiv1.zip
    |
    \---sub2
            archiv2.zip
    $ FOR /R "X:" %I IN (*.zip) DO 7z x "%I" -aou -o"%~dpI"
    $ 7z x "X:\sub1\archiv1.zip" -aou -o"X:\sub1\"
    7-Zip 4.65  Copyright (c) 1999-2009 Igor Pavlov  2009-02-03
    Processing archive: X:\sub1\archiv1.zip
    Extracting  dir1
    Extracting  dir1\file1.txt
    Everything is Ok
    Folders: 1
    Files: 1
    Size:       1848300
    Compressed: 5611
    $ 7z x "X:\sub2\archiv2.zip" -aou -o"X:\sub2\"
    7-Zip 4.65  Copyright (c) 1999-2009 Igor Pavlov  2009-02-03
    Processing archive: X:\sub2\archiv2.zip
    Extracting  dir2
    Extracting  dir2\file2.txt
    Everything is Ok
    Folders: 1
    Files: 1
    Size:       4620750
    Compressed: 13670
    $ :: Directory Structure After Extraction:
    $ TREE X: /A /F
    Folder PATH listing for volume TEST
    Volume serial number is 1234-ABCD
    X:\
    +---sub1
    |   |   archiv1.zip
    |   |
    |   \---dir1
    |           file1.txt
    |
    \---sub2
        |   archiv2.zip
        |
        \---dir2
                file2.txt
    $
    
     
  • CarcaBot

    CarcaBot - 2010-10-10

    Yes, this command is working nice for me , but what  is not good , the subfolders structure is not kept.
    To keep the archive structure you have to run

    FOR /R "C:" %I IN (*.zip) DO @ECHO.7z x "%I" -aou -o"%~dpI\*"

    good luck

     
  • euliutos

    euliutos - 2011-09-22

    This thread was very helpful guys!
    I have a slightly different problem
    I want to extract the contents of the zip files to another root directory, but to help in sorting, i want to retain the path in the file name.

    For example, I have the following

    C:\source\dir1\ar1.zip\abc\file1
    C:\source\dir1\ar1.zip\abc\file2
    C:\source\dir1\ar1.zip\abc\file3
    C:\source\dir1\ar2.zip\abc\file1
    C:\source\dir1\ar2.zip\abc\file2
    C:\source\dir1\ar2.zip\abc\file3
    C:\source\dir2\ar1.zip\abc\file1
    C:\source\dir2\ar1.zip\abc\file2
    C:\source\dir2\ar1.zip\abc\file3
    C:\source\dir2\ar2.zip\abc\file1
    C:\source\dir2\ar2.zip\abc\file2
    C:\source\dir2\ar2.zip\abc\file3

    And I am trying to extract them like this:

    C:/destination/dir1_ar1_abc_file1
    C:/destination/dir1_ar1_abc_file2
    C:/destination/dir1_ar1_abc_file3
    C:/destination/dir1_ar2_abc_file1
    C:/destination/dir1_ar2_abc_file2
    C:/destination/dir1_ar2_abc_file3
    C:/destination/dir2_ar1_abc_file1
    C:/destination/dir2_ar1_abc_file2
    C:/destination/dir2_ar1_abc_file3
    C:/destination/dir2_ar2_abc_file1
    C:/destination/dir2_ar2_abc_file2
    C:/destination/dir2_ar2_abc_file3

    Finally, all the files will be extracted in the same location, but the file name will depict their original paths.

    Any help guys? I am clueless!

     
  • Chiliem

    Chiliem - 2012-02-03

    I have one uncompressed folder containing several sub folders with scattered .jar, .tar and .iso files - apart from non-archive file formats.
    What I would like is to extract all extractable files in their directories and afterwards delete the archive file formats.

    Can this be done in one command line?

    thx a lot!

     
  • fernando

    fernando - 2012-02-03

    TEST something like the below command line

    FOR /R %I IN (*.iso *.jar *.tar) DO (7Z x "%I" -aoa -o"%~dpI\*" |FIND "Everything is Ok" >nul &&DEL "%I" ||ECHO.%I : EXTRACT FAIL - ARC NOT DELETED >>ERR.TXT)

    Above is one single line.

     
  • Chiliem

    Chiliem - 2012-02-04

    Thx. There's another problem tho. It seems that when I try to open 7za.exe, the standalone console window disappears straight away. Any ideas on this?

    thx!

     
  • LogicDaemon

    LogicDaemon - 2012-06-24

    btw, you just can

    7z x archive.7z -o*
    

    or even

    7z x *.7z -o*
    

    (this is 7z-specific, won't work with others)

     
  • Andrei

    Andrei - 2018-03-10

    THis is very useful so far, however I am trying to use the command in powershell and trying to resolve why the syntax seems to not work the same as cmd.

    (I am using powershell because dealing with UNC paths not supported with cmd)

    PS Microsoft.PowerShell.Core\FileSystem::\\server\folder1\childFolder1\grandCHildFOlder1>FOR /R '\\server\folder1\childFolder1\grandCHildFOlder1' %I IN (*.rar) DO @ECHO.7z x "%I" -aou -o"%~dpI\*"
    At line:1 char:4
    + FOR /R '\\server\folder1\childFolder1\grandCHildFOlder1 ...
    +    ~
    Missing opening '(' after keyword 'for'.
    At line:1 char:150
    + ...server\folder1\childFolder1\grandCHildFOlder1' %I IN (*.rar) DO @ECHO.7z x "%I ...
    +                                                                  ~
    Missing property name after reference operator.
    At line:1 char:144
    + ... server\folder1\childFolder1\grandCHildFOlder1' %I IN (*.rar) DO @ECHO.7z x "% ...
    +                                                             ~~~~~
    The splatting operator '@' cannot be used to reference variables in an expression. '@ECHO' can be used only as an
    argument to a command. To reference variables in an expression use '$ECHO'.
        + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
        + FullyQualifiedErrorId : MissingOpenParenthesisAfterKeyword 
    

    I tried extra parentheses like this:

    FOR /R ('\server\folder1\childFolder1\grandCHildFOlder1' %I IN (*.rar)) DO @ECHO.7z x "%I" -aou -o"%~dpI*"

    but same error

    any ideas?

     
  • Andrei

    Andrei - 2018-03-12

    I mostly figured it out using the following code:

    Get-ChildItem -Filter *.zip -Recurse C:\Archives | % { $_.FullName } | Split-Path | Get-Unique | % { cd $_ ; &'C:\Program Files\7-Zip\7z.exe' x *.zip -o* }
    

    Although this doesnt delete archive afterwards, but could be modified to do so I believe

     
  • Gregm

    Gregm - 2019-08-02

    Hmm.. I'm also doing recursive folder walks but i can't figure out how to get it to leave the path alone.

    For example, I have the following

    C:\source\dir1\dir2\file1.gz
    C:\source\dir1\dir2\file2.gz
    C:\source\dir1\dir3\dir4\file3.gz
    C:\source\dir1\dir5\file4.gz

    when I do
    1)FOR /R %I IN (*.gz) DO 7z x "%I" -aou -o"%~dpI\*")
    - The files are extracted, but it adds them into a folder with the file name
    - eg. c:\source\dir1\dir2\dir-file1\file1
    2) FOR /R %I IN (*.gz) DO (7Z x "%I" -aou)
    - The files are extracted but all into c:\source with no folder hierarcy retained

    How Do i get it to extract in the same directory folder as the file?

     

Log in to post a comment.