PoshCode Archive  Artifact [d6b407bdf3]

Artifact d6b407bdf3d9869210070b57ff74ab932d2b8a5a531c7c53149ebdb07bbf675f:

  • File JScript-Random-File.ps1 — part of check-in [1a759dd176] at 2018-06-10 13:43:57 on branch trunk — Creates ramdom file with null length. Just for fun. (user: greg zakharov size: 1346)

# encoding: ascii
# api: powershell
# title: JScript:Random File
# description: Creates ramdom file with null length. Just for fun.
# version: 0.1
# type: function
# author: greg zakharov
# license: CC0
# x-poshcode-id: 4632
# x-archived: 2013-11-25T07:10:48
# x-published: 2013-11-22T14:59:00
#
#
var rnd = rnd || {
  getRandomNumber : function(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  },
  
  setRandomFile : function(dir, len) {
    if (len > 32 || len < 1) WScript.Quit(1);
    
    var arr, ext = [], i, r, std;
    with (new ActiveXObject('WScript.Shell')) {
      std = Exec("cmd /q /k echo off");
      std.StdIn.WriteLine("assoc & exit");
      arr = std.StdOut.ReadAll().split('\n');
      
      for (i = 0; i < arr.length; i++) {
        if (!arr[i].match(/.\d+\=/) && arr[i].match(/=\w+/)) {
          ext.push(arr[i].split('=')[0]);
        }
      }
    }
    
    r = this.getRandomNumber(0, ext.length - 1);
    
    with (new ActiveXObject('Scriptlet.TypeLib')) {
      return dir + GUID.substring(1, 37).replace(/-/g, '').toLowerCase().slice(0, len) + ext[r];
    }
  }
}

//usung examples
with (new ActiveXObject('Scripting.FileSystemObject')) {
  CreateTextFile(rnd.setRandomFile(32));
  CreateTextFile(rnd.setRandomFile('E:\\test\\', 13));
}