Home > Software design >  Batch extracting specific file types from master directory with many subdirectories
Batch extracting specific file types from master directory with many subdirectories

Time:03-27

I have a quantity of folders with archive files, Parent folder with subfolder eg

Graphics:

  • graphics 01012021/file31241.7z
  • graphics 01022021/file4231.7z
  • odds and ends 01032022/filejohnny.7z

etc each folder contains an archive - various names numbers. each archives contains various files, pdf's txt files invoices and image files. Generally the images are .jpg named various names.

What I would like to do is batch attack the parent folder to extract an image file/s from the each archive from each sub directory and leave the image in the subdirectory with the archive where it came from. If the archive has multiple images that's fine, I am not targeting a single particular image.

hopefully ending up with something like

Graphics:

  • graphics 01012021/ file31241.7z yellowstone.jpg flintstone.jpg
  • graphics 01022021/ file4231.7z martha.jpg
  • odds and ends 01032022/ filejohnny.7z artemis.jpg French toast.png

I would rather avoid if possible extracting all the files separating the images then having to re archive.

What I tried to discover originally was to batch extract the image files to the directory it belongs to, have the image file renamed to its directory name. I didn't get close with a solution, so I think if possible just extracting the image would be fine and I can use a renaming app to do the other I've found bulk rename utility to be just fine once I got my head around it. You wouldn't think that over the years you would collect so many archives, like small drops they ended up become an ocean full.

I have tried researching stack and seen a lot of examples of how eg 7zip works but I just cant get my head quite around it.

I am due to retire they tell me 65 is the time for the chicken coop, I've been a pencil pusher and mouse skater most of my life in the gfx industry. I used to know what was in each archive but memory is a little how to say... rusty nowadays, I know all my archives have images in them. My life would be a lot easier in the sunset of it to look at the pictures and not have to rack my brains trying to remember what was in the archive itself.

Cheers and ty in advance from the colonies downunder.

Grumpy

CodePudding user response:

To answer your question the task is simple involving For loops with recursion, however to be robust the solution will be complex without knowing how those specific long term possibly mixed, 7zip files are subdivided, thus if a 7zip has itself two sub folders with identical named files you will hit error conditions. I have allowed for that using -aou to auto rename if necessary. however I have not added the folder name to each file as that's an extra step.

@echo off & Title Extract Images from 7z files
set "extractor=C:\Program Files (x86)\7-Zip\7z.exe"
set "archives=*.7z"
set "filetypes=*.png *.jpg *.jpeg *.tif *.tiff"
set "startDir=C:\Users\WDAGUtilityAccount\Desktop"

FOR /R "%startdir%" %%I IN (%archives%) DO ( "%extractor%" e "%%I" -o"%%~dpI" %filetypes% -aou)

You can add extra archive types such as .rar or .zip in the same way I have allowed for different image file types. HOWEVER do test for such variations first otherwise a second run will invoke that autonaming of existing duplicates. You can of course change that -aou to -aos to avoid overwite or remove it as desired.

An alternative approach on windows is that, standard windows.zip files can be navigated and thus kept as compressed folders where a click on each file will open in the default application. Thus only need extraction to the folder when you wish to use a non default app.

enter image description here

The secondary advantage more directly related to your need is that some viewers can read just the images and ignore any other contents (even if they can read text or html) and one I support that reads images in zips is SumatraPDF and although not listed it can open .7Z files to show in the same way

enter image description here

Hence my answer to the posed question is I suggest converting 7zip to standard zip rather than expand them, or use SumatraPDF as default/alternative 7z viewer !!.

CodePudding user response:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION 
rem The following settings for the source directory, destination directory, target directory,
rem batch directory, filenames, output filename and temporary filename [if shown] are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.

SET "sourcedir=u:\your files"
SET "destdir=u:\your results"
SET "outfile=           
  • Related