Page 1 of 1

Batch File to extract files from a zipped file

Posted: 01 Oct 2022 17:43
by data808
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.

Re: Batch File to extract files from a zipped file

Posted: 02 Oct 2022 06:42
by aGerman
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

Re: Batch File to extract files from a zipped file

Posted: 02 Oct 2022 13:26
by data808
Yes extract into one location folder on desktop. There are no name clashes. Each file is a unique name.

Re: Batch File to extract files from a zipped file

Posted: 02 Oct 2022 14:10
by aGerman
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

Re: Batch File to extract files from a zipped file

Posted: 03 Oct 2022 03:45
by miskox
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

Re: Batch File to extract files from a zipped file

Posted: 03 Oct 2022 04:06
by aGerman
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

Re: Batch File to extract files from a zipped file

Posted: 03 Oct 2022 04:51
by miskox
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

Re: Batch File to extract files from a zipped file

Posted: 03 Oct 2022 04:54
by miskox
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

Re: Batch File to extract files from a zipped file

Posted: 03 Oct 2022 05:01
by aGerman
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

Re: Batch File to extract files from a zipped file

Posted: 03 Oct 2022 05:48
by koko
Fwiw 7-Zip, which has a useful and easy-to-use CLI binary, still supports XP according to their home page.

Re: Batch File to extract files from a zipped file

Posted: 03 Oct 2022 06:34
by miskox
@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

Re: Batch File to extract files from a zipped file

Posted: 24 Oct 2022 02:14
by data808
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?

Re: Batch File to extract files from a zipped file

Posted: 24 Oct 2022 08:56
by aGerman
Obviously yes. There are two variables which have to be customized.

Steffen

Re: Batch File to extract files from a zipped file

Posted: 26 Jan 2024 16:00
by ohenryx2
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