2

I have a script to extract all the archives in a folder to folders with names of the archives, I want to modify that so it will extract only a single file from each archive (doesn't matter which one), it will behave as if there's a counter that will count 1 file and move to the next archive.

For example say we have the following files in the folder, each archive contains what's in the curly braces:

a.7z {folder{1.txt, 2.txt, 3.txt}}
b.7z {folder2{4.txt, 5.txt, 6.txt}}
c.7z {7.txt}

So running the script will result in having the following extracted (those are folders):

a{folder{1.txt}}
b{folder2{4.txt}}
c{7.txt}

Is this possible?

This is the script I currently have:

PATH %PATH%;"C:\Program Files\7-Zip";
7z x *.* -o*
shinzou
  • 5,850
  • 10
  • 60
  • 124

2 Answers2

3

You can list the files of the archive using

7z l a.7z

If all the files end with txt, you can use

7z l a.7z | find "txt"

Use this answer to get the first line: Windows batch command(s) to read first line from text file

Then use this answer to get the file name (last word) from the line and save it in a batch variable FILE_NAME: https://superuser.com/questions/667494/batch-to-extract-a-word-from-a-string-inside-a-text-file

Then use the following command to extract the file:

7z e -i!%FILE_NAME% a.7z -o*

You can use this example to iterate all the *.7z files Iterate all files in a directory using a 'for' loop

for /r %%i in (*.7z) do echo %%i

All the parts together:

@echo off

for  %%i in (*.7z) do call :extract_file %%i

:extract_file
"c:\Program Files\7-Zip\7z.exe" l %1 > temp.txt
for /f "skip=15 tokens=1-10" %%a in (temp.txt) do (
  echo %%f
  "c:\Program Files\7-Zip\7z.exe" e -i!%%f %1 -o*
  goto :end
)
:end
EXIT /B 0

:eof
Community
  • 1
  • 1
Ale
  • 146
  • 7
  • The extensions of the archives can be anything, even `zip1` or `rar1`, the files in them can be anything as well, and I'd like to keep the folder structure inside each archive when extracting but extract just one file and not all of the archive. – shinzou Feb 28 '16 at 13:18
  • I edited the answer with a more complete solutions. I still didn't found a way to keep the 7z file structure. – Ale Mar 07 '16 at 15:27
  • I think using "7z x" instead of "7z e" will preserve the directory structure. Also, you may need to put double quotes around and wildcarded filters; e.g. `"*.pdf"` instead of just `*.pdf`. – Dave Hein Dec 19 '20 at 13:08
  • i'm getting: `>7z e -i!%"Target\NMD\2021-2022\Renewal.xlsm"% Target.zip -o* ; No files to process ; Everything is Ok ; Files: 0 ; Size: 0` – johny why Sep 22 '21 at 03:39
0

The following works as desired. The file is "Target.zip". The top-level folder inside the ZIP file is "Target". The desired file, "Renewal.xlsm", is at a deeper level. The directory tree in the zip should be duplicated for the uncompressed file. Unlike Scripting.FileSystemObject (scrrun.dll), Windows Script Host (wshom.ocx), and Microsoft Shell Controls (shell32.dll), this solution runs asynchronously.

This will extract the single file Renewal.xlsm in Target.zip to a folder called Target.

unzip "Target.zip" "Target\NMD\2021-2022\Renewal.xlsm" -d "Target"

https://unix.stackexchange.com/a/57522/20731

unzip is installed on my corporate computer, but not my personal off-the-shelf Windows. By their own report, Unzip has vulnerabilities, so I wouldn't recommend it.

UnZip


Here's a method using 7-Zip (command-line executable 7z), which is a widely trusted tool. The original relative filepath is restored, meaning the original tree of folders leading to that file is recreated.

7z x -i!"Relative\Path\To\YourDesiredFile.ftw" "YourArchive.zip"

If you want all loose files extracted into a single directory, use e instead of x. Check out the -o switch.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
johny why
  • 2,047
  • 7
  • 27
  • 52