0

I have a Windows machine with a directory structure that contains files with a particular extension. I need a script that will add a zipped file for each file with that extension. Is there a way to do this with 7 Zip (7za.exe)? If not, are there other options?

BEFORE \dir1\dir2\ file.ext

AFTER \dir1\dir2\ file.ext file.zip

4 Answers4

2

I'd suggest using PowerShell.

Get-ChildItem -Include *.ext -Recurse
    | %{ &zipcommand a $_.FullName ($_.FullName+'.zip') }

This assumes that 'zipcommand' is your commandline zip tool and that it takes 3 arguments, 'a' for archive, the file to archive and the archive name.

Cephas
  • 443
  • 1
  • 4
  • 10
0

May not be what you're looking for, but I'd use Cygwin and the *nix find utility:

find dir1 -name "*.ext" | while read i; do zip "$i.zip" "$i"; done

"find" starts at "dir1" and recurses, matching everything with a name of the form "*.ext". It outputs a list of matches, which is piped to "while read i", which reads each line (a matching path) into the variable "i". For each different value of i, the "zip" command is executed, compressing the file into a zip archive of the same name (and path), but with ".zip" appended.

Note that the above will not remove the original file. I could have added "rm $i" after the zip command to do that, but it's better to create the archives in one step, and then use:

find dir1 -name "*.ext" | while read i; rm "$i"; done

to find and remove the originals. Another nice thing to do to test your line before running it is to echo the command, rather than executing it.

find dir1 -name "*.ext" | while read i; do echo zip "$i.zip" "$i"; echo rm "$i"; done

That will show you each of the zip and rm commands that it would do if you removed the echos, so you can make sure it's doing the right thing. Then remove the echos to actually do it.

If there's a nice way to do this with common Windows utilities, I'm not aware of it.

divegeek
  • 141
  • 3
0

You can use a VBScript along with a decent command line zip program. Google for VBScript + FileSystemObject + Recursive to find a suitable vbscript script. You can the modify the code to execute command line zip tool.

Salman A
  • 492
  • 2
  • 9
  • 17
0

Here is an example in Ch, which has zip capabilities built-in:

cd "c:\dir1\dir2"
pwd
string_t file, dir_listing=`ls ./`;
string_t zipfile;
foreach(file; dir_listing) {
    zipfile = strtok(file, "."); 
    zip $zipfile $file
    }
ls -l

I wrote it on a Windows system, and it should work with no problems. At least it did here. If you want to try it out, you can download the standard version of Ch, which is freeware. Just paste the code into a text file and name it whatever.ch. Once you've installed Ch, you can then run it from the command line.

Edit: The script above will zip all files in the specified directory. To filter the files by extension, change

string_t file, dir_listing=`ls ./`;

to

string_t file, dir_listing=`ls *.ext`;

You're just changing the arguments to the ls command for your filetype.

Joe Internet
  • 1,449
  • 8
  • 6