Unzip multiple zip files into different folders with a particular name
Well i got the solution for it. Here is what I did and it worked.
def extract_zip_Files():
os.chdir(dir_name) # change directory from working dir to dir with files
for item in entries: # loop through items in dir
if item.endswith(extension): # check for ".zip" extension
file_name = os.path.abspath(item) # get full path of files
print('file is', file_name)
zip_ref = zipfile.ZipFile(file_name) # create zipfile object
zip_ref.extractall(cwd+"/unZipped/"+item) # extract file to dir
zip_ref.close() # close file
Unzip Multiple Files into Different Directories
Because you specify only one destination path they will all be extracted into c:\users\name\downloads
. I suppose the zip archives each contain a folder named "folder", so all contents from all archives end up together in c:\users\name\downloads\folder
You would have to specify a different destination path for each archive. Not sure what your naming convention should be, I have used a simple counter:
$counter = 0
Get-ChildItem 'c:\users\name\downloads' -Filter *.zip | foreach {
$destination = Join-Path $_.DirectoryName ("YourName" + $counter++)
Expand-Archive $_.FullName -DestinationPath $destination
}
Of course I suppose, now every of those folders will have the subfolder "folder", but if that's how the archives are built there's not really a way to change that. If you are absolutely sure that all archives have that subfolder, you could do something like this:
$counter = 0
Get-ChildItem 'c:\users\name\downloads' -Filter *.zip | foreach {
# expand to the root folder first
Expand-Archive $_.FullName -DestinationPath $_.DirectoryName
# now rename the extracted "folder" to whatever you like
Rename-Item (Join-Path $_.DirectoryName "folder") -NewName ("YourName" + $counter++)
}
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.
From batch file, I Need to recurse directories (and sub directories) and unzip every zip file found into their current directory w/delete of archive
... do (
... 7z x ....
if errorlevel 1 (echo fail %%s) else (echo del %%s)
)
should fix your problem. 7zip
appears to follow the rules and return errorlevel
as 0 for success and non-zero otherwise. if errorlevel
operates on the run-time value of errorlevel
.
Unzip multiple zip folders with the original name
As it's unclear upfront what the zip file contains, I suggest to
- first create a folder in
D:\Output
with the zip files BaseName and unzip there.
## Q:\Test\2019\05\23\SO_56279162.ps1
$ZipFilesPath = "D:\Input" # 'A:\Input' #
$UnzipBase = "D:\Output" # 'A:\Output' #
$Shell = New-Object -com Shell.Application
$ZipFiles = Get-Childitem $ZipFilesPath -Recurse -Include *.ZIP
$progress = 1
foreach ($ZipFile in $ZipFiles) {
Write-Progress -Activity "Unzipping to $($UnzipPath)" `
-PercentComplete (($progress / ($ZipFiles.Count + 1)) * 100) `
-CurrentOperation $ZipFile.FullName `
-Status "File $($Progress) of $($ZipFiles.Count)"
$ZipFolder = $Shell.NameSpace($ZipFile.fullname)
$UnzipPath = Join-Path $UnzipBase $ZipFile.BaseName
New-Item $UnzipPath -ItemType Directory | Out-NUll
$Location = $Shell.NameSpace($UnzipPath)
$Location.Copyhere($ZipFolder.items(), 1040)
$progress++
}
Sample tree (on my RamDrisk A:)
> tree /F A:\
A:\
├───input
│ ET_NM.TEST_DATA_2.ET_ID.84.C_ID.4016.Part.1.20190502.0826.zip
│
└───Output
└───ET_NM.TEST_DATA_2.ET_ID.84.C_ID.4016.Part.1.20190502.0826
└───1ac7b-2d62-403c-8394-5bd33cbe7
test.txt
PowerShell extract each zip file to own folder
You could do with a few less variables. When the $zipfiles
collection contains FileInfo objects as it appears, most variables can be replaced by using the properties the objects already have.
Also, try to avoid concatenating to a variable with +=
because that is both time and memory consuming.
Just capture the result of whatever you output in the loop in a variable.
Something like this:
# capture the stuff you want here as array
$info = foreach ($zip in $zipfiles) {
# output whatever you need to be collected in $info
$zip.Name
# construct the folderpath for the unzipped files
$dst = Join-Path -Path $zip.DirectoryName -ChildPath $zip.BaseName
if (!(Test-Path $dst -PathType Container)) {
$null = New-Item -ItemType Directory $dst -ErrorAction SilentlyContinue
$null = Expand-Archive -LiteralPath $zip.FullName -DestinationPath $dst -ErrorAction SilentlyContinue
}
}
# now you can create a multiline string from the $info array
$result = $info -join "`r`n==========`r`n"
Related Topics
Linux: Block Until a String Is Matched in a File ("Tail + Grep with Blocking")
Getting Current Path in Variable and Using It
How Is Stack Size of Linux Process Related to Pthread, Fork and Exec
Compiling Out-Of-Tree Kernel Module Against Any Kernel Source Tree on the Filesystem
How to Give Highest Priority to Ethernet Interrupt in Linux
Any Porting Available of Backtrace for Uclibc
Linux - Without Hardware Soundcard, Capture Audio Playback, and Record It to File
How to Rename Files You Put into a Tar Archive Using Linux 'Tar'
Apache Server Keeps Crashing, "Caught Sigterm, Shutting Down"
What Is the Safest Way to Run an Executable on Linux
Why Is Crond Failing to Run a Non-Root Crontab on Alpine Linux
/Usr/Bin/Perl: Bad Interpreter: Text File Busy
Merging Through Fuzzy Matching of Variables in R
Linux Synchronization with Fifo Waiting Queue
Perf Stat Does Not Count Memory-Loads But Counts Memory-Stores
Does Madvise(_, _, Madv_Dontneed) Instruct the Os to Lazily Write to Disk