Skip to content

Instantly share code, notes, and snippets.

@Kreijstal
Created June 21, 2020 22:10
Show Gist options
  • Save Kreijstal/f4601c7bc1f83bad714138db7bdb0df5 to your computer and use it in GitHub Desktop.
Save Kreijstal/f4601c7bc1f83bad714138db7bdb0df5 to your computer and use it in GitHub Desktop.
//How much should we love JScript?
//Seriously Tho, I never thought I would get this far with WSH.
var FSO=WScript.CreateObject("Scripting.FileSystemObject");
function readBinaryFile(filename){
//var bin=WScript.CreateObject("ADODB.Stream");
/*bin.Type=1;
bin.Open();
bin.LoadFromFile(filename);
return bin.Read();*/
var file=FSO.getFile(filename);
if(!file)return;
var ret=[];
var stream=file.OpenAsTextStream(),x,charLimit=400;
while(!stream.atEndOfStream&&charLimit--){
ret.push(x=stream.read(1));
}
stream.close();
return ret;
}
function saveBinaryFile(filename,bytearray){
var stream=FSO.createTextFile(filename);
stream.Write(bytearray);
stream.Close();
}
var WshShell = WScript.CreateObject("WScript.Shell");
var folder=FSO.GetFolder(WshShell.CurrentDirectory);
WScript.Echo(WshShell.CurrentDirectory);
// Reference the File collection of the Text directory
var FileCollection = folder.Files;
var strFileNames=[],strFileName,r=/f_(?:\d|[A-F])+/i,allFiles=[],currentFile={};
for(var match,objEnum=new Enumerator(FileCollection);!objEnum.atEnd();objEnum.moveNext()) {
if(/\\f_\d+/.test(strFileName=FSO.GetAbsolutePathName(objEnum.item()))){
//WScript.Echo("Ehhh? "+objEnum.item());
strFileNames.push(strFileName);
if(match=readBinaryFile(strFileName).join('').match(/webm|ogg|riff|png|gif|lame|mp4|jfif/i)){
//WScript.Echo("Hue");
if(currentFile.files){allFiles.push(currentFile);}
currentFile={files:[strFileName],ext:match[0]};
}else{if(!currentFile.files)continue;currentFile.files.push(strFileName);}
};
}
allFiles.push(currentFile);
function StringToArray(str){
for(var arr=[],i=0,l=str.length;i<l;i++){
arr[i]=str[i];
}
return arr;
}
//WScript.Echo(readBinaryFile(strFileNames[0]).join('').match(/webm|ogg/));
var sav;
/*WScript.Echo(allFiles);
WScript.Echo("lel");*/
for(var i=0,commands=[];i<allFiles.length;i++){
commands.push("copy /b \""+allFiles[i].files.join('" + "')+'" ' +
allFiles[i].files[0].match(/[\da-f]+$/i)[0]+
'_'+
allFiles[i].files[allFiles[i].files.length-1].match(/[\da-f]+$/i)[0]+
'.'+allFiles[i].ext);
//WScript.Echo(commands[commands.length-1]);
}
function ASCIIToEncodedBatch(str){
return str.replace(/%/g,'%%').replace(/\^/g,'^^').replace(/[&\|<>"'`\=\\\/,;\(!\)0-9]/g,function(a){return '^'+a;}).replace(/[\n\r]/g,function(a){return '^'+a+a})
}
for(var i=0;i<commands.length;i++){
//WScript.Echo("cmd /c "+ASCIIToEncodedBatch(commands[i]));
WshShell.Run("cmd /c "+ASCIIToEncodedBatch(commands[i]));
}
WScript.Echo((commands));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment