Jump to content

Batch scripting + 7zip?


oligarchy314

Recommended Posts

So I have a folder full of files (read roms), and I want to automate the process of going through and individually zipping them to save space. Is there a way for me to write a script to use the command line version of 7zip to go through the directory and zip each file to individual archives? I have 750+ files and I could just zip them all manually, but I figure there has to be a better way. Now, before someone suggests it, no zipping them all into one big archive is not an option, I want them as individual files so I can open each one directly.

I figure all it really would take are some wild card options to say open file *.x and compress to file *.zip, and something to say when it's reached the end of the directory to quit. I'm just not sure how to do the file handling in the batch script. I've already tried just putting wild cards into the command line options of 7z.exe, but it gives me an error about not finding the file * to write the archive to.

Also I would be willing to use a different archiver if there is one out there that can handle this automatically, I just personally like 7zip. Any and all help is greatly appreciated.

Link to comment
Share on other sites

Edit [Reworked my original batch file, this is much more useful.]

Edit Edit [Fixed the selection of archive type, previously all selections other than "zip" defaulted to 7z archives, just with different extensions that were effectively meaningless.]

Just copy all this text into your favorite text editor and save it as a batch file.

::-----------------------------------------------------------------------------
:: This is a script to tell 7zip to archive each file in a directory to its
:: own individual zip file so that you can save space, but still be able to
:: access each file independently of the others.

::-----------------------------------------------------------------------------
:: Prompt user for required information
::      path1 == Path to input files
::      path2 == Path to output files
::      crit == Criteria for files (multiple criteria separated with spaces)
::      type == Archive type to use
::      ext == File extension for archive type selected

cls
@echo off
echo Mass Archiver Batch Script (v4.05) 2007.08.07
echo Welcome to the 7zip mass archival tool. This utilizes the 7z command line 
echo program for the processing of files. This tool will help you in compressing
echo entire directories of files when you want each file to go to its own
echo individual archive.
echo.

:start
echo What would you like to compress? (ex. *.txt, *.log) 
set crit=
set /P crit=Use spaces between multiple critera: 

echo.
set path1=
set /P path1=What is the full path to the input directory? (C: ...) 

echo.
set path2=
set /P path2=What is the full path to the ouput direcory? (C: ...) 

echo.
set type=
set /P type=What compression you would like to use? (7z,zip,gzip,bzip2,tar) 

::-----------------------------------------------------------------------------
:: Set file extension to correspond to the archive type selected.

set ext=%type%
if %type%==gzip set ext=gz
if %type%==bzip2 set ext=bz2

::-----------------------------------------------------------------------------
:: Change directory to working folder by selecting drive and path

cd "%path1%"

::-----------------------------------------------------------------------------
:: set compression level to "Ultimate" unless type == tar

set level="-mx9"
if %type%==tar set level=""

::-----------------------------------------------------------------------------
:: Run archive command ::
:: For every filename in the current directory that meets some criteria:
:: load 7zip, create an archive "filename.ext" and compress the file
:: "filename" to the archive "filename.ext"

for %%f in (%crit%) do C:Progra~17-zip7z a -t%type% %level% "%path2%%%f".%ext% "%path1%%%f"

echo.
echo.
echo.
echo.
echo.
echo Job Finished . . .
echo.
echo.
echo.
echo.
echo.

::-----------------------------------------------------------------------------
:: Ask user if they would like to remove the uncompressed files
set cleanup=
set /P cleanup=Would you like to delete the original files? (%crit%) (y/n) 

if %cleanup%==y del %crit%
if %cleanup%==Y del %crit%

::-----------------------------------------------------------------------------
:: Ask user if they would like to run the script again
echo.
set repeat=
set /P repeat=Would you like to compress something else? (y/n) 

cls
if %repeat%==y goto start
if %repeat%==Y goto start

:end

My earlier attempts at this didn't work with spaces in the file names, but this will handle them fine. Also this can be run from anywhere on the computer, so long as the path to the 7z.exe is correct, because this prompts for input and output directories. Finally, this allows you to pick the archive type for the output files, and then compresses at "Ultimate" level to an archive with the appropriate file extension.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...