Created
April 30, 2014 15:02
-
-
Save fabienhinault/54dc3dfc162a68acde4c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // -*- javascript -*- | |
| // utility functions | |
| if (!Array.prototype.reduce) | |
| { | |
| Array.prototype.reduce = function(fun /*, initial*/) | |
| { | |
| var len = this.length; | |
| if (typeof fun != "function") | |
| throw new TypeError(); | |
| // no value to return if no initial value and an empty array | |
| if (len == 0 && arguments.length == 1) | |
| throw new TypeError(); | |
| var i = 0; | |
| if (arguments.length >= 2) | |
| { | |
| var rv = arguments[1]; | |
| } | |
| else | |
| { | |
| do | |
| { | |
| if (i in this) | |
| { | |
| rv = this[i++]; | |
| break; | |
| } | |
| // if array contains no values, no initial value to return | |
| if (++i >= len) | |
| throw new TypeError(); | |
| } | |
| while (true); | |
| } | |
| for (; i < len; i++) | |
| { | |
| if (i in this) | |
| rv = fun.call(null, rv, this[i], i, this); | |
| } | |
| return rv; | |
| }; | |
| } | |
| add = function(x, y){return x + y;}; | |
| enumerate = function(coll, f) | |
| { | |
| var e = new Enumerator(coll); | |
| var res = new Array(coll.Count); | |
| var i = 0; | |
| for (; ! e.atEnd(); e.moveNext()) | |
| { | |
| var x = e.item(); | |
| res[i] = f(x); | |
| i++; | |
| } | |
| return res; | |
| }; | |
| toArray = function(coll) | |
| { | |
| return enumerate(coll, function(i){return i;}); | |
| }; | |
| function print(s) | |
| { | |
| sFilePath = fs.GetSpecialFolder(2).Path + "\\copiedFiles.txt"; | |
| WScript.Echo(sFilePath); | |
| var f = fs.OpenTextFile(sFilePath, 2, true); | |
| f.WriteLine(s); | |
| f.Close(); | |
| sh.Run(f.Path, 9); | |
| } | |
| function getFileObject(f) | |
| { | |
| if("Shortcut" == f.Type) | |
| { | |
| var path = sh.CreateShortcut(f.Path).TargetPath; | |
| return fs.GetFile(path); | |
| } | |
| else | |
| { | |
| return f; | |
| } | |
| } | |
| function getRealPath(f) | |
| { | |
| if("Shortcut" == f.Type) | |
| { | |
| var sc = sh.CreateShortcut(f.Path); | |
| return sc.TargetPath; | |
| } | |
| else | |
| { | |
| return f.Path; | |
| } | |
| } | |
| function find(dir) | |
| { | |
| e = new Enumerator(dir.subFolders); | |
| var res = toArray(dir.Files.map); | |
| for (; ! e.atEnd(); e.moveNext()) | |
| { | |
| x = e.item(); | |
| res.append(find(dir)); | |
| } | |
| return res; | |
| } | |
| function copyFile(source, target) | |
| { | |
| if(source.Name == target.Name) | |
| { | |
| WScript.Echo("copyFile " + target.Path); | |
| if( ! fs.FileExists(target.Path + "~")) | |
| { | |
| fs.CopyFile(target.Path, target.Path + "~"); | |
| } | |
| fs.CopyFile(source.Path, target.Path); | |
| return target.Path + "\n"; | |
| } | |
| return ""; | |
| } | |
| function copyPdb(source, target) | |
| { | |
| if((source.Name.replace(".pdb", ".dll") == target.Name) || | |
| (source.Name.replace(".pdb", ".exe") == target.Name)) | |
| { | |
| WScript.Echo("copyPdb " + target.Path); | |
| fs.CopyFile(source.Path, target.ParentFolder.Path + "\\"); | |
| return target.Path + "\n"; | |
| } | |
| return ""; | |
| } | |
| function FileCopier(source) | |
| { | |
| this.source = source; | |
| this.copy = function(target) | |
| { | |
| copyFile(this.source, target); | |
| }; | |
| }; | |
| function PdbCopier(source) | |
| { | |
| this.source = source; | |
| this.copy = function(target) | |
| { | |
| copyPdb(this.source, target); | |
| }; | |
| }; | |
| function createCopier(f) | |
| { | |
| var o = getFileObject(f); | |
| var sName = o.Name | |
| var sub = sName.substr(sName.length - 4, 4); | |
| if(sub == ".pdb") | |
| { | |
| return new PdbCopier(o); | |
| } | |
| else | |
| { | |
| return new FileCopier(o); | |
| } | |
| } | |
| function copyList(l, target) | |
| { | |
| var res = new Array(); | |
| for(var i = 0; i < l.length; i++) | |
| { | |
| res[i] = l[i].copy(target); | |
| } | |
| return res; | |
| } | |
| function applyRec(e, fn, targetDir) | |
| { | |
| if(targetDir.Name == "tocopy") | |
| { | |
| return; | |
| } | |
| var resF = enumerate(targetDir.Files, function(targetFile){fn(e, targetFile);}); | |
| var resSD = enumerate(targetDir.SubFolders, | |
| function(subdir) | |
| {applyRec(e, fn, subdir);}); | |
| resF.concat(resSD); | |
| return resF; | |
| } | |
| function copyListToDir(l, targetDir) | |
| { | |
| return applyRec(l, copyList, targetDir); | |
| } | |
| var fs = WScript.CreateObject("Scripting.FileSystemObject"); | |
| var sh = WScript.CreateObject("WScript.Shell"); | |
| var source = fs.GetFolder(".\\tocopy"); | |
| var dir = fs.GetFolder("."); | |
| var a = enumerate(source.Files, createCopier); | |
| // for(i=0; i<a.length; i++) | |
| // { | |
| // WScript.Echo(a[i].Path); | |
| // } | |
| var res = applyRec(a, copyList, dir); | |
| //print(res.reduce(add, "")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment