Batch File to extract files from a zipped file
Moderator: DosItHelp
Batch File to extract files from a zipped file
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
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
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
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
You could probably use a JScript hybrid code.
Steffen
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
}
}
Re: Batch File to extract files from a zipped file
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
Saso
*not sure if this is the correct translation
Re: Batch File to extract files from a zipped file
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
Steffen
- 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
You are out of luck if you tried on XP ¯\_(ツ)_/¯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.
Steffen
Re: Batch File to extract files from a zipped file
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
And your updated script works (Windows 10, 64-bit)
Thanks. Useful.
Saso
Re: Batch File to extract files from a zipped file
And I answereed that XP support would be useful.aGerman wrote: ↑03 Oct 2022 04:06FWIW: A couple of days ago I wrote you
viewtopic.php?p=67529#p67529You are out of luck if you tried on XP ¯\_(ツ)_/¯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.
Steffen
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
Yes, I remember However, I can't make XP getting new featuresAnd I answereed that XP support would be useful.
I just asked because this was another possible reason why the script failed.
Steffen
Re: Batch File to extract files from a zipped file
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
@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
Thanks.
Saso
Re: Batch File to extract files from a zipped file
How do these work? Do I just paste this into a .bat file and run it?aGerman wrote: ↑02 Oct 2022 14:10You could probably use a JScript hybrid code.
SteffenCode: 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 } }
Re: Batch File to extract files from a zipped file
Obviously yes. There are two variables which have to be customized.
Steffen
Steffen
Re: Batch File to extract files from a zipped file
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
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