//v1.1.1 //- min version added //- prepared for automatic updates via script wizard //v1.2 //- converted to js //- added EOL column to detect line endings (quite a basic implementation) //- added Encoding column, core logic stolen from user qiuqiu (viewtopic.php?f=35&t=26945) //- added qiuqiu's list of file extensions and some others as well //- native url (data.url) added //v1.3.1 - 2012-12-07 //- added "FirstLine" column //v1.3.2 //- added "R" rating column //v1.3.3 - 2017-11-20 //- fix for uppercase file extension being ignored //- XLog added //todo //- if no BOM found, search first x-bytes of file for existing utf encoding/characters // to also detect non-BOM utf files. /////////////////////////////////////////////////////////////////////////////// function OnInit(data){ /////////////////////////////////////////////////////////////////////////////// //uid added via script wizard, do not change once published var uid = "3A5F8B2E-F7B9-4B25-BC0D-EF21F367A907"; //resource center url added via script wizard var url = "http://resource.dopus.com/viewtopic.php?f=35&t=22835"; data.name = "Column.File: FileInfo"; data.desc = "Columns providing details for plain text files."; if (DOpus.Version.AtLeast("12.0.8")) data.url = url; data.copyright = "tbone"; data.version = "1.3.3"; data.min_version = "11.1"; data.default_enable = true; data.config.Extensions = "" + ".csv.rdf.url.lng.dcf.dop.omd.torrent.inc.tpl.reg.btm.ncl.m3u.pls.cue.pys" + ".ps1.psm1.asp.aspx.asax.ascx.ashx.bat.cmd.c.h.cs.cpp.hpp.cc.c++" + ".css.ini.inf.pas.dproj.bdsproj.dpr.dpk.dfm.fmx.nfm.xfm.lfm.e.groovy" + ".html.htm.shtml.hta.jsl.java.jav.jsp.js.jsx.jse.json.pl.pm" + ".plex.php.php4.phtml.py.pyw.rb.rbx.erb.resx.sql.tcl.txt.config" + ".vbs.frm.vb.bas.xml.dtd.xhtml.xsl.xslt.wpl.xsd.xs.cfg.readme.guide.ans.ascii"; var col = data.AddColumn(); col.name = "Lines"; col.header = "Lines"; col.label = "FI: Lines"; col.method = "Column_Lines"; col.justify = "right"; col.type = "number"; col.autogroup = true; var col = data.AddColumn(); col.name = "EOL"; col.header = "EOL"; col.label = "FI: EOL"; col.method = "Column_EOL"; col.justify = "right"; col.type = "text"; col.autogroup = true; var col = data.AddColumn(); col.name = "Encoding"; col.header = "Encoding"; col.label = "FI: Encoding"; col.method = "Column_Encoding"; col.justify = "right"; col.type = "text"; col.autogroup = true; var col = data.AddColumn(); col.name = "FirstLine"; col.header = "FirstLine"; col.label = "FI: First Line"; col.method = "Column_FirstLine"; col.justify = "left"; col.type = "text"; var col = data.AddColumn(); col.name = "Rating"; col.header = "R"; col.label = "FI: Rating"; col.method = "Column_Rating"; col.justify = "center"; col.type = "text"; col.defwidth = "25px"; col.autorefresh = true; col.autogroup = true; } //global scope, so object will not be created for each item var objFS = new ActiveXObject("Scripting.FileSystemObject"); /////////////////////////////////////////////////////////////////////////////// function Column_Rating(data){ /////////////////////////////////////////////////////////////////////////////// var value = null; var meta = data.item.metadata; value = (meta.other && typeof meta.other.rating != "undefined") ? meta.other.rating : null; /*if (value === null) { var meta = DOpus.FSUtil.GetMetadata(data.item.realpath); //meta.other != "none" && value = (meta.other && typeof meta.other.rating != "undefined") ? meta.other.rating + " GetMeta" : null; }*/ if (value === null) value = ""; else value = value+''; if (value != "" && value.substring(0,1) == "0") value = ""; data.value = value; if (value == "") data.sort = 6; } /////////////////////////////////////////////////////////////////////////////// function Column_Encoding(data){ /////////////////////////////////////////////////////////////////////////////// if (!IsValidTextFile(data.item)) { Log("E ["+data.item.name+"] no valid text file", "d"); return; } var result = GetEncoding(data.item.realpath, data.item.size); if (!result.err) data.value = result.encoding; if (result.err) Log("E ["+data.item.name+"] " + error.msg); } /////////////////////////////////////////////////////////////////////////////// function Column_Lines(data){ /////////////////////////////////////////////////////////////////////////////// if (!IsValidTextFile(data.item)) return; var objTS = objFS.OpenTextFile(data.item); if (objTS.AtEndOfStream){ data.value = 0; return; } objTS.ReadAll(); data.value = objTS.Line; } /////////////////////////////////////////////////////////////////////////////// function Column_EOL(data){ /////////////////////////////////////////////////////////////////////////////// if (!IsValidTextFile(data.item)) return; data.value = GetEOL(data.item.realpath); } /////////////////////////////////////////////////////////////////////////////// function Column_FirstLine(data){ /////////////////////////////////////////////////////////////////////////////// if (!IsValidTextFile(data.item)) return; var content = ""; var fso = new ActiveXObject("Scripting.FilesystemObject"); var file = fso.OpenTextFile( data.item.realpath, 1, -2); // Read, UseDefaultEncoding if (!file.AtEndOfStream) content = file.ReadLine(); file.Close(); data.value = content; return; } /////////////////////////////////////////////////////////////////////////////// function GetEOL(filePath){ /////////////////////////////////////////////////////////////////////////////// var objTS = objFS.OpenTextFile(filePath); if (objTS.AtEndOfStream){ objTS.Close(); return ""; } var content = objTS.ReadAll(); objTS.Close(); if (0); else if (content.indexOf("\x0D\x00\x0A\x00")!=-1) return "PC (CRLF)"; //UTF16 LE (Notepad Unicode) else if (content.indexOf("\x00\x0D\x00\x0D")!=-1) return "PC (CRLF)"; //UTF16 BE else if (content.indexOf("\x0D\x0A")!=-1) return "PC (CRLF)"; else if (content.indexOf("\x0D\x00")!=-1) return "OS-9 (CR)"; //UTF16 LE (Notepad Unicode) else if (content.indexOf("\x00\x0D")!=-1) return "OS-9 (CR)"; //UTF16 BE else if (content.indexOf("\x0D")!=-1) return "OS-9 (CR)"; else if (content.indexOf("\x0A\x00")!=-1) return "UNIX (LF)"; //UTF16 LE (Notepad Unicode) else if (content.indexOf("\x00\x0A")!=-1) return "UNIX (LF)"; //UTF16 BE else if (content.indexOf("\x0A")!=-1) return "UNIX (LF)"; return ""; } /////////////////////////////////////////////////////////////////////////////// function GetEncoding(filePath, size){ /////////////////////////////////////////////////////////////////////////////// var result = { err:false, msg:"", encoding:""}; if (0); else if (typeof size != "undefined" && size == 0){ result.msg = "empty"; result.err = true; } else if (typeof size != "undefined" && size < 4) { result.msg = "too small"; result.err = true; } if (result.err) return result; var file = DOpus.FSUtil.OpenFile(filePath); if (file.Error){ result.msg = "cannot open file"; result.err = true; return result; } var blob = DOpus.Create.Blob(0,0,0,0); //prefill with 4 zero bytes var bytesRead = file.Read(blob, 4); file.Close(); if (bytesRead<4){ result.msg = "too small"; result.err = true; return result; } if (0); else if (blob(0)==0xEF && blob(1)==0xBB && blob(2)==0xBF && blob(3)!=0x00) result.encoding = "UTF-8 BOM"; else if (blob(0)!=0x00 && blob(1)==0x00 && blob(2)!=0x00 && blob(3)==0x00) result.encoding = "UTF-16 LE NoBOM"; else if (blob(0)==0x00 && blob(1)!=0x00 && blob(2)==0x00 && blob(3)!=0x00) result.encoding = "UTF-16 BE NoBOM"; else if (blob(0)==0xFF && blob(1)==0xFE && blob(2)!=0x00 && blob(3)==0x00) result.encoding = "UTF-16 LE BOM"; else if (blob(0)==0xFE && blob(1)==0xFF && blob(2)==0x00 && blob(3)!=0x00) result.encoding = "UTF-16 BE BOM"; else if (blob(0)==0xFF && blob(1)==0xFE && blob(2)==0x00 && blob(3)==0x00) result.encoding = "UTF-32 BE BOM"; else if (blob(0)==0x00 && blob(1)==0x00 && blob(2)==0xFE && blob(3)==0xFF) result.encoding = "UTF-32 LE BOM"; else result.encoding = "?"; return result; } /////////////////////////////////////////////////////////////////////////////// function IsValidTextFile(doItem){ /////////////////////////////////////////////////////////////////////////////// if (doItem.is_dir) return false; else if ((doItem.ext+"").length == 0) { Log("E ["+doItem.name+"] extension missing?", "d"); return false; } //else if ((Script.config.Extensions+"").search(new RegExp("\\"+doItem.ext+"(\.|$)","gi"))==-1) { else if ((Script.config.Extensions+"").indexOf((doItem.ext+"").toLowerCase())==-1) { Log("E ["+doItem.name+"] extension unknown", "d"); return false; } return true; } /////////////////////////////////////////////////////////////////////////////// function Log(text, lvl, ind){ //XLog v0.45 (allow early usage, but warn (Script-object cannot be typeof'ed)) var u,lvs={o:0,x:1,e:2,w:3,i:4,n:5,t:6,d:7,a:8},i=["","X","E","W","I","","","",""],d=DOpus; if (typeof XLog==(u="undefined")){var v=d.Vars; if (v.Exists("XLog") && typeof XLogForce==u) {XLog=v.Get("XLog"); }} if (typeof XLog==u){try{var c=Script.Config;if(typeof c.XLog!=u){XLog=c.XLog;}}catch(e){d.Output("XLog: Early call, log-level=all.",1);XLog=8;}} if (typeof XLog==u){XLog="normal";} if(XLog.paused===undefined){ if(XLog===true) var L=5; else if(!isNaN(parseInt(XLog,10))) var L=XLog*1; else var L=lvs[(""+XLog).toLowerCase().substring(0,1)]; XLog={paused:false,I:0,L:L};} lvl=(lvl==undefined?5:lvs[lvl.substring(0,1).toLowerCase()]); if (!(lvl && XLog.L && !XLog.paused && (lvl<=XLog.L))){ return;} var pad = (XLog.I==0?"":new Array(XLog.I+1).join(" ")); if (i[lvl])pad = i[lvl]+(!pad?" ":pad.substring(1)); if (text!="-"){if (d.Version.AtLeast("11.13.1")) d.Output(pad+text,((lvl==1||lvl==2)?1:0)); else d.Output(pad+text);} ind=(ind!==undefined?ind:0);XLog.I+=ind;if(XLog.I<0)throw new Error("XLog indent went sub-zero."); } /////////////////////////////////////////////////////////////////////////////// function OnAboutScript(data){ //v0.1 var cmd = DOpus.Create.Command(); if (!cmd.Commandlist('s').exists("ScriptWizard")){ if (DOpus.Dlg.Request("The 'ScriptWizard' add-in has not been found.\n\n"+ "Install 'ScriptWizard' from [resource.dopus.com].\nThe add-in enables this dialog and also offers "+ "easy updating of scripts and many more.","Yes, take me there!|Cancel", "No About.. ", data.window)) cmd.RunCommand('http://resource.dopus.com/viewtopic.php?f=35&t=23179');} else cmd.RunCommand('ScriptWizard ABOUT WIN='+data.window+' FILE="'+Script.File+'"'); } //MD5 = "2eb962f78e271b812b4bd19ab53b9984"; DATE = "2017.11.20 - 20:13:13"