Welcome, guest | Sign In | My Account | Store | Cart

Naturally, commnad language hasn't native methods to create archives but there is no obstacle to use JScript inside a batch. By the way, this do not need creation temporary files. OK, how does it work? Maybe you heard about conditional compilation in JScript, so you must be familiar with this trick. Take a look at this:

Batch, 65 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
@set @script=0 /*
  @echo off
    set @script=
    cscript //nologo //e:jscript "%~dpnx0" %1 %2
  exit /b
*/

with (WScript.Arguments.Named) {
  if (length != 2) {
    WScript.echo(WScript.ScriptName + " </a:archive> </f:folder>");
    WScript.Quit(1);
  }

  //validator extension of future archive
  RegExp.prototype.Validate = function() {
    var raw = this.exec(Item('a')),
        res = RegExp.lastMatch;
    return res.toUpperCase();
  }

  var fso = new ActiveXObject('Scripting.FileSystemObject'),
      app = new ActiveXObject('Shell.Application');

  //store files into .CAB or .ZIP files
  if ((new RegExp('.cab', 'i').Validate()) == '.CAB') {
    try {
      with (new ActiveXObject('MakeCab.MakeCab')) {
        CreateCab(Item('a'), false, false, false);

        with (new Enumerator(fso.GetFolder(Item('f')).Files)) {
          for (; !atEnd(); moveNext()) {
            var itm = item();
            AddFile(itm.Path, itm.Name);
          }
        }

        CloseCab();
      }
    }
    catch (e) { WScript.echo(e.message + '.'); }
  }
  else if ((new RegExp('.zip', 'i').Validate()) == '.ZIP') {
    try {
      var zip = fso.CreateTextFile(Item('a'), true);
      zip.Write('PK\05\06' + new Array(19).join('\0'));
      zip.Close();

      with (new Enumerator(fso.GetFolder(Item('f')).Files)) {
        for (; !atEnd(); moveNext()) {
          var itm = item();
          if (itm != fso.GetFile(Item('a')).Path) {
            app.NameSpace(fso.GetFile(Item('a')).Path).CopyHere(itm.Path);
            WScript.Sleep(1000);
            WScript.echo("File added: " + itm.Path);
          }
        }
      }
    }
    catch (e) { WScript.echo(e.message + '.'); }
  }
  else {
    WScript.echo("Unsupported archive format.");
    WScript.Quit(1);
  }
}
Created by greg zakharov on Thu, 1 Nov 2012 (GPL3)
Batch recipes (42)
greg zakharov's recipes (59)

Required Modules

  • (none specified)

Other Information and Tasks