Windows batch extract zip files, into folders based on zip name and combining similar zips

7-zipbatchcommand linewindowszip

I have been searching for a way to bulk extract zip files in a directory and extract them into individual folders based on their zip file name.

I found: Extract all Zip's in a directory (incl. subfolders) with a .bat file or dos command

and: https://stackoverflow.com/questions/17077964/windows-batch-script-to-unzip-files-in-a-directory

which do what I wanted. However I realised that I have some zip files that need to be extracted into the same directory. For example:

base dir
|
|
> file.zip
|
> another file.zip
|
> yaf_disk1.zip
|
> yaf_disk2.zip
|
> yaf2_disk1.zip
|
> yaf2_disk2.zip

With the above I would like yaf_disk1 and yaf_disk2 to be extracted into a directory yaf and yaf2_diskX into the directory yaf2.

I have a working script:

for /R "." %%I in ("*.zip") do (
  "%ProgramFiles%\7-Zip\7z.exe" x -y -o"%%~dpnI" "%%~fI" 
)

which I got from the above links which I mostly (now!) understand. I don't know if it is possible to get a batch file to look at the file, see the diskX at the end and extract all files with same name and diskX into the first directory it created from those files?

I am working with Windows 10 if that is of any importance.

Best Answer

Don't worry, created a Python script that done it for me. If anyone comes across this and needs something similar here is my code:

import glob, os, zipfile

zipfiles = glob.glob("*.zip") ## find all files with zip extension in current directory

count = 0 ## current file

while (count < len(zipfiles)):
    print ("Extracting file: "+zipfiles[count])

    zipRef = zipfile.ZipFile(zipfiles[count]) ## make ready for extraction

    directoryToMake = zipfiles[count] ## iniital name for extraction directory

    isMultiDisk = zipfiles[count].find('Disk') ## search for 'Disk' in the directory name and store location

    if (isMultiDisk > 0): ## has found 'Disk' in the name
        print("Multi Disk")
        if (len(directoryToMake[isMultiDisk+4:-4]) < 2): ## checks to see if the disk number is more than 2 characters long
                                                         ## not actually checking the number just the number of characters
                                                         ## to account for some names being DiskA rather than Disk1
            directoryToMake = directoryToMake[:-10] ## remove everything after the Disk to make the clean directory name
        else:
            directoryToMake = directoryToMake[:-11] ## remove extra char for disk numbers of 2 digits
    else: ## disk not found in name
        print("Single Disk")
        directoryToMake = directoryToMake[:-4] ## just remove the .zip at the end

    if not os.path.exists(directoryToMake): ## check if directory exists
        print("Creating Directory: " + directoryToMake)
        os.makedirs(directoryToMake) ## if not make it

    print("Extracting files into directory: " + directoryToMake+ "\n")
    zipRef.extractall(directoryToMake) ## extract into directory

    count += 1
Related Question