Create a dedicated folder for every zip files in a directory and extract zip files
"extract here" is merely a feature of whatever unzip
wrapper you are using. unzip
will only extract what actually is in the archive. There is probably no simpler way than a shell script. But sed
, awk
etc. are not needed for this if you have a POSIX-compliant shell:
for f in *.zip; do unzip -d "${f%*.zip}" "$f"; done
(You MUST NOT escape the *
or pathname expansion will not take place.) Be aware that if the ZIP archive contains a directory, such as with Eclipse archives (which always contain eclipse/
), you would end up with ./eclipse*/eclipse/eclipse.ini
in any case. Add echo
before unzip
for a dry run.
Unzip all zipped files in a folder to that same folder using Python 2.7.5
Below is the code that worked for me:
import os, zipfile
dir_name = 'C:\\SomeDirectory'
extension = ".zip"
os.chdir(dir_name) # change directory from working dir to dir with files
for item in os.listdir(dir_name): # loop through items in dir
if item.endswith(extension): # check for ".zip" extension
file_name = os.path.abspath(item) # get full path of files
zip_ref = zipfile.ZipFile(file_name) # create zipfile object
zip_ref.extractall(dir_name) # extract file to dir
zip_ref.close() # close file
os.remove(file_name) # delete zipped file
Looking back at the code I had amended, the directory was getting confused with the directory of the script.
The following also works while not ruining the working directory. First remove the line
os.chdir(dir_name) # change directory from working dir to dir with files
Then assign file_name as
file_name = dir_name + "/" + item
ExtractToDirectory , how to extract a zip folder within a zip folder using c#
As per what I see in your code, I think you should call the extractZipFiles
method recursively, so that after the extracting you call the method again with the directory where you extracted the files so it scans it for *.zip
files.
I'm not sure about what variables you'd want to use, but something like this:
public void extractZipFiles(string targetFileDirectory, string zipFileDirectory, string Number)
{
Directory.GetFiles(zipFileDirectory, "*.zip", SearchOption.AllDirectories).ToList()
.ForEach(zipFilePath => {
var test = Number + "_" + Path.GetFileNameWithoutExtension(zipFilePath);
var extractPathForCurrentZip = Path.Combine(targetFileDirectory, test);
if(!Directory.Exists(extractPathForCurrentZip))
{
Directory.CreateDirectory(extractPathForCurrentZip);
}
ZipFile.ExtractToDirectory(zipFilePath, extractPathForCurrentZip);
extractZipFiles(targetFileDirectory, extractPathForCurrentZip, Number);
});
}
Script to extract zip files in seperate folders to their own folders
Under unix you could use something like
find <dir> -iname '*.zip' -execdir unzip {} \;
The program find traverses <dir>
recursively and on every .zip file it finds it will change to that files directory and executes unzip on it.
Batch script to unzip files into a certain folder
When reading the help for 7z
in your cmd window, you will notice that the -o switch is the output directory option. Currently you are telling it to be %%~dpI
which is in fact Drive and Path of the current of the zip files. So you would want to change the output directory:
for /R "C:\root\folder" %%I in ("*.zip") do (
"%ProgramFiles(x86)%\7-Zip\7z.exe" x -y -oc"C:\root\folder2" "%%~fI"
)
I do not currently have 7zip installed, but I am almost 100% sure that it has a recursive function builtin, if so, you do not even need the for loop, you can just try:
7z.exe x -y "C:\root\folder\*.zip" -oc:"C:\root\folder2" -r
If it does not work, I will remove this section from the answer.
Related Topics
Execute Command Line and Return Command Output
Generate Random Float Number in Given Specific Range of Numbers Using Bash
Chrome Certificate Selection Appears Multiple Times
Is There a Special Restriction on Commands Executed by Cron
Android Studio 3.0 Emulator Does Not Start
Cmake Auto Header File Dependency
Where Is the Linux Isr Entry Point
How to Add a Custom Extended Attribute from Linux Kernel Space (I.E from a Custom System Call)
Different Results Between Ps Aux and 'Ps Aux' Inside a Script
How to Get Details of All Modules/Drivers That Were Initialized/Probed During the Linux Kernel Boot
Why Does '/Proc/Meminfo' Show 32Gb When Aws Instance Has Only 16Gb
While Do Loop and Variables in a Bash Script
How to Count the Number of Occurrences of a String in an Entire File