Mikki Weesenaar
Mikki Weesenaar

Reputation: 5

Batch: Extracting each zip in folder x, to a seperate folder

'Ello,

Using Batch (or Powershell or something), I want to do the following: From folder x, I want to extract all .zip files to their own folder.

Example:

[Folder x]
- a.zip
- b.zip
- c.zip

Into:

[Folder x]
- [a]
- [b]
- [c]
- a.zip
- b.zip
- c.zip

I got until the point that I can extract every zip using the following batch:

for /r %%f in (*.zip) do "C:\Program Files\7-Zip\7z.exe" x "%%f" -o"%%~dpf"

But that resulted in that all contents were extracted to the root folder rather than to a new folder for each zip. I used Script to extract zip files in seperate folders to their own folders as basis for the batch line above.

Any tips?

Upvotes: 0

Views: 2641

Answers (1)

user189198
user189198

Reputation:

NOTE: This is a PowerShell example, which differs significantly from your batch script. Just an FYI.

Assume that you have some ZIP files in the c:\test folder. The following script will:

  • Get a list of ZIP files in c:\test
  • For each ZIP file ...
    • Create a folder based on the ZIP file's "base name" (file name sans extension)
    • Build the command line arguments to extract the ZIP file
    • Extract the ZIP file

To make this code work for you, you will need to update:

  • The path to the ZIP file(s)
  • The path to 7za.exe
    • If 7za.exe is contained in the PATH environment variable, then you do not need to specify the full path to the executable. I simply did it for clarity.

Script:

$ZipFileList = Get-ChildItem -Path c:\test\*.zip;

foreach ($ZipFile in $ZipFileList) {
    mkdir -Path ('{0}\{1}' -f $ZipFile.Directory, $ZipFile.BaseName);
    $ArgumentList = 'x "{0}" -o"{1}\{2}\"' -f $ZipFile.FullName, $ZipFile.Directory, $ZipFile.BaseName;
    Start-Process -FilePath c:\windows\7za.exe -ArgumentList $ArgumentList -Wait;
}

Upvotes: 2

Related Questions