Batch File to extract files from a zipped file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
data808
Posts: 47
Joined: 19 Jul 2012 01:49

Batch File to extract files from a zipped file

#1 Post by data808 » 01 Oct 2022 17:43

I have a zipped file that contains hundreds of .txt files but they are in folders within the zipped file itself. Is there a way to make a batch file that can extract all those .txt files so that I don't have to do them one by one? Thanks.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch File to extract files from a zipped file

#2 Post by aGerman » 02 Oct 2022 06:42

Batch can't unzip files by itself. Do you have any 3rd party utility installed which has a command line interface? If so, read about how to use it on their manpage.

Furthermore, I'm struggling to understand your request. Do you want to extract all files into one location without subfolders? If so, can you ensure not having any name clashes?

Steffen

data808
Posts: 47
Joined: 19 Jul 2012 01:49

Re: Batch File to extract files from a zipped file

#3 Post by data808 » 02 Oct 2022 13:26

Yes extract into one location folder on desktop. There are no name clashes. Each file is a unique name.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch File to extract files from a zipped file

#4 Post by aGerman » 02 Oct 2022 14:10

You could probably use a JScript hybrid code.

Code: Select all

@if (0)==(0) echo off

set "zip=Test.zip"
set "dst=TestFolder"
cscript.exe //nologo //e:jscript "%~f0" "%zip%" "%dst%"
pause

goto :eof @end var oFSO=new ActiveXObject('Scripting.FileSystemObject'), oShApp=new ActiveXObject('Shell.Application');
new ActiveXObject('WScript.Shell').CurrentDirectory=oFSO.GetParentFolderName(WScript.ScriptFullName);
var dstPath=oFSO.GetAbsolutePathName(WScript.Arguments(1));
if(!oFSO.FolderExists(dstPath)) oFSO.CreateFolder(dstPath);
var dstShFolder=oShApp.NameSpace(dstPath);
unzipFiles(oShApp.NameSpace(oFSO.GetAbsolutePathName(WScript.Arguments(0))));
function unzipFiles(oFolder){
  for(var oEnum=new Enumerator(oFolder.Items()); !oEnum.atEnd(); oEnum.moveNext()){
    if(oEnum.item().IsFolder) unzipFiles(oEnum.item().GetFolder);
    else dstShFolder.CopyHere(oEnum.item(), 1564); // 1564=4|8|16|512|1024, https://learn.microsoft.com/en-us/windows/win32/shell/folder-copyhere
  }
}
Steffen

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Batch File to extract files from a zipped file

#5 Post by miskox » 03 Oct 2022 03:45

Steffen: I receive an error something like (it is in Slovenian so can't do copy/paste): (13,7), error executing Microsfot script: 'null' value has a "null" value or is not an object*

Saso

*not sure if this is the correct translation

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch File to extract files from a zipped file

#6 Post by aGerman » 03 Oct 2022 04:06

I can't reproduce this error. However, I updated the code above to avoid possible issues with relative paths.
- the current directory of the JScript is now explicitly set to the script path
- relative paths are resolved to absolute paths

FWIW: A couple of days ago I wrote you
viewtopic.php?p=67529#p67529
Do you still need to get this working on XP? On more recent Windows versions ZIP files are supported as a kind of compressed Shell folders where VBS or JS can be used along with the methods of the "Shell.Application" Com interface.
You are out of luck if you tried on XP ¯\_(ツ)_/¯

Steffen

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Batch File to extract files from a zipped file

#7 Post by miskox » 03 Oct 2022 04:51

Tried on Windows 10. Maybe there are some other restrictions. Will try at home on Windows 10 too.

And your updated script works (Windows 10, 64-bit)

Thanks. Useful.

Saso

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Batch File to extract files from a zipped file

#8 Post by miskox » 03 Oct 2022 04:54

aGerman wrote:
03 Oct 2022 04:06
FWIW: A couple of days ago I wrote you
viewtopic.php?p=67529#p67529
Do you still need to get this working on XP? On more recent Windows versions ZIP files are supported as a kind of compressed Shell folders where VBS or JS can be used along with the methods of the "Shell.Application" Com interface.
You are out of luck if you tried on XP ¯\_(ツ)_/¯

Steffen
And I answereed that XP support would be useful.

I will stay with my zip.exe and unzip.exe I have at the moment. If anyone at sourceforge will be able to repack them that would be very good.

Thanks.
Saso

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch File to extract files from a zipped file

#9 Post by aGerman » 03 Oct 2022 05:01

And I answereed that XP support would be useful.
Yes, I remember :D However, I can't make XP getting new features :lol:
I just asked because this was another possible reason why the script failed.

Steffen

koko
Posts: 38
Joined: 13 Oct 2016 00:40

Re: Batch File to extract files from a zipped file

#10 Post by koko » 03 Oct 2022 05:48

Fwiw 7-Zip, which has a useful and easy-to-use CLI binary, still supports XP according to their home page.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Batch File to extract files from a zipped file

#11 Post by miskox » 03 Oct 2022 06:34

@koko: thanks. I use zip.exe and unzip.exe. They have been very useful for a very long time. Another idea (see that thread) was to have even smaller .exe files (or one exe for zip and unzip) with support for only those switshes that I use). Your idea for 7-zip is good, but console .exe is 800+ kb in size. Maybe if there would an option to make custom exe...

Thanks.
Saso

data808
Posts: 47
Joined: 19 Jul 2012 01:49

Re: Batch File to extract files from a zipped file

#12 Post by data808 » 24 Oct 2022 02:14

aGerman wrote:
02 Oct 2022 14:10
You could probably use a JScript hybrid code.

Code: Select all

@if (0)==(0) echo off

set "zip=Test.zip"
set "dst=TestFolder"
cscript.exe //nologo //e:jscript "%~f0" "%zip%" "%dst%"
pause

goto :eof @end var oFSO=new ActiveXObject('Scripting.FileSystemObject'), oShApp=new ActiveXObject('Shell.Application');
new ActiveXObject('WScript.Shell').CurrentDirectory=oFSO.GetParentFolderName(WScript.ScriptFullName);
var dstPath=oFSO.GetAbsolutePathName(WScript.Arguments(1));
if(!oFSO.FolderExists(dstPath)) oFSO.CreateFolder(dstPath);
var dstShFolder=oShApp.NameSpace(dstPath);
unzipFiles(oShApp.NameSpace(oFSO.GetAbsolutePathName(WScript.Arguments(0))));
function unzipFiles(oFolder){
  for(var oEnum=new Enumerator(oFolder.Items()); !oEnum.atEnd(); oEnum.moveNext()){
    if(oEnum.item().IsFolder) unzipFiles(oEnum.item().GetFolder);
    else dstShFolder.CopyHere(oEnum.item(), 1564); // 1564=4|8|16|512|1024, https://learn.microsoft.com/en-us/windows/win32/shell/folder-copyhere
  }
}
Steffen
How do these work? Do I just paste this into a .bat file and run it?

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Batch File to extract files from a zipped file

#13 Post by aGerman » 24 Oct 2022 08:56

Obviously yes. There are two variables which have to be customized.

Steffen

ohenryx2
Posts: 5
Joined: 08 Apr 2023 14:16

Re: Batch File to extract files from a zipped file

#14 Post by ohenryx2 » 26 Jan 2024 16:00

I periodically download the latest nightly build of ffmpeg from this url:

https://github.com/BtbN/FFmpeg-Builds/releases

The downloaded file is "ffmpeg-master-latest-win64-gpl.zip".

The structure of the zip file looks like:

ffmpeg-master-latest-win64-gpl
.........bin
..............ffprobe.exe
..............ffplay.exe
..............ffmpeg.exe
.......doc
............lots of files I don't want

All I want are the 3 exe files, ff*.exe, which are buried 2 levels deep and I want them placed directly into
"C:\Bin"

This 7z command does the trick:

7z.exe e "ffmpeg-master-latest-win64-gpl.zip" -oc:\bin -r ff*.exe

Post Reply