本帖最后由 pcl_test 于 2016-7-31 14:58 编辑
源码及源文件- /*&cls
- @echo off
- set "dstfd=$处理结果"
- if not exist "%dstfd%\" md "%dstfd%\"
- (dir /a-d/b "测试文本.txt"^|find /v "%dstfd%")|cscript -nologo -e:jscript "%~f0" "%dstfd%"
- pause&exit
- */
-
- //参数设置
- var delims = '[ \\s]+'; //指定列分隔符,默认以空格、制表符(tab)作为分割列,支持正则表达式
- var fillchr = ' '; //设置对齐填充字符,只支持单字节字符,默认为空格
- var p = 4; //设置对齐后两列间最短处填充字符的数量,默认4个
- var align = 0; //设置对齐格式,0为左对齐,1为右对齐,2为伪水平居中,3为左向两端对齐,4为右向两端对齐,默认左对齐
- var bl = 0; //设置是否保留空白行,0为删除,1为保留,默认不保留
-
-
- //预处理
- var fso = new ActiveXObject('Scripting.FileSystemObject'), space='';
- var dstfd = WSH.Arguments(0);
- var reg = new RegExp(delims);
- for(var i=0; i<200; i++)space += fillchr;
- if(fillchr==''||delims==''||!/^[0-4]$/.test(align)||!/^[01]$/.test(bl)||!/^[2-9]|1\d*$/.test(p)){WSH.echo('参数有误!');WSH.quit()}
-
-
- //主程序
- while(!WSH.StdIn.AtEndOfStream){
- var t={}, len=0;
- var line = WSH.StdIn.ReadLine();
- try{
- var txt = (fso.OpenTextFile(line, 1).ReadAll()+'\r\n').replace(/\s+$/, '');
- ReadFile();
- switch(align)
- {
- case 1:var txt = RigthAlign();break;
- case 2:var txt = CenterAlign();break;
- case 3:var txt = LJustifyAlign();break;
- case 4:var txt = RJustifyAlign();break;
- default:var txt = LeftAlign();
- }
- }catch(e){}
- var file = fso.GetFile(line);
- var f = dstfd +'\\'+ file.Name;
- var i = 1;
- while(fso.FileExists(f))f = dstfd+'\\'+fso.GetBaseName(file)+'_'+(i++)+'.'+fso.GetExtensionName(file);
- fso.CreateTextFile(f, 2).Write(txt);
- t=null;
- }
- WSH.Echo('Done');
-
-
- //读取文本内容和各种预统计
- function ReadFile(){
- var tmp = txt.split(/[ \s]*\r\n/);
- for(var i=0; i<tmp.length; i++){
- var tr = tmp[i].replace(/^[ \s]*|[ \s]*$/, '').split(reg);
- if(tr.length-1 >= len)len=tr.length-1;
- for(var j=0; j<tr.length; j++){
- var l=n=0;
- for(var k=0; k<tr[j].length; k++){
- var c = tr[j].charCodeAt(k)
- if((c>=0x0001 && c<=0x007e)||(c>=0xff60 && c<=0xcff9f)){
- l += 1;
- }else{
- n++;
- l += 2;
- }
- }
- if(align == 2)t['cell'+i+j.toString()] = l;
- if(n > 0)t[i+j.toString()] = n;
- if(!t['max'+j]){
- t['max'+j] = l;
- }else{
- if(l >= t['max'+j])t['max'+j] = l;
- }
- }
- }
- }
-
- //左对齐
- function LeftAlign(){
- var tmp = txt.split('\r\n'), m=0, s='';
- for(var i=0; i<tmp.length; i++){
- if(!/^[ \s]*$/.test(tmp[i])){
- var tr = tmp[i].replace(/^[ \s]*|[ \s]*$/, '').split(reg), line='';
- for(var j=0; j<tr.length-1; j++){
- if(t[m+j.toString()]){
- line += (tr[j]+space).slice(0, t['max'+j]-t[m+j.toString()]+p);
- }else line += (tr[j]+space).slice(0, t['max'+j]+p);
- }
- m++;
- s += line + tr[tr.length-1] + '\r\n';
- }else if(bl == 1)s += '\r\n';
- }
- return s;
- }
-
- //右对齐
- function RigthAlign(){
- var tmp = txt.split('\r\n'), m=0, s='';
- for(var i=0; i<tmp.length; i++){
- if(!/^[ \s]*$/.test(tmp[i])){
- var tr = tmp[i].replace(/^[ \s]*|[ \s]*$/, '').split(reg), line='';
- for(var j=0; j<tr.length; j++){
- if(t[m+j.toString()]){
- line += (space+tr[j]).slice(-t['max'+j]+t[m+j.toString()])+(j<tr.length-1?space.substr(0, p):'');
- }else line += (space+tr[j]).slice(-t['max'+j])+(j<tr.length-1?space.substr(0, p):'');
- }
- m++;
- s += line + '\r\n';
- }else if(bl == 1)s += '\r\n';
- }
- return s;
- }
-
- //伪水平居中
- function CenterAlign(){
- var tmp = txt.split('\r\n'), n=m=0, s='';
- for(var i=0; i<tmp.length; i++){
- if(!/^[ \s]*$/.test(tmp[i])){
- var tr = tmp[i].replace(/^[ \s]*|[ \s]*$/, '').split(reg), line='';
- for(var j=0; j<tr.length; j++){
- var a = (t['max'+j]-t['cell'+n+j.toString()])/2;
- var b = parseInt(a);
- if(b == a){
- if(a != 0){
- line += space.slice(0, b)+tr[j]+(j==tr.length-1?'':space.slice(0, b+p));
- }else line += tr[j]+(j==tr.length-1?'':space.slice(0, p));
- }else{
- line += space.slice(0, b+1)+tr[j]+(j==tr.length-1?'':space.slice(0, b+p));
- }
- }
- n++;
- s += line + '\r\n';
- }else if(bl == 1)s += '\r\n';
- }
- return s;
- }
-
- //左向两端对齐
- function LJustifyAlign(){
- var tmp = txt.split('\r\n'), m=0, s='';
- for(var i=0; i<tmp.length; i++){
- if(!/^[ \s]*$/.test(tmp[i])){
- var tr = tmp[i].replace(/^[ \s]*|[ \s]*$/, '').split(reg), line='';
- for(var j=0; j<tr.length; j++){
- if(j<len){
- if(tr.length>1 && j<tr.length-1){
- if(t[m+j.toString()]){
- line += (tr[j]+space).slice(0, t['max'+j]-t[m+j.toString()]+p);
- }else line += (tr[j]+space).slice(0, t['max'+j]+p);
- }else line += tr[j];
- }else{
- if(t[m+j.toString()]){
- line += (space+tr[j]).slice(-t['max'+j]+t[m+j.toString()]);
- }else line += (space+tr[j]).slice(-t['max'+j]);
- }
- }
- m++;
- s += line + '\r\n';
- }else if(bl == 1)s += '\r\n';
- }
- return s;
- }
-
- //右向两端对齐
- function RJustifyAlign(){
- var tmp = txt.split('\r\n'), m=0, s='';
- for(var i=0; i<tmp.length; i++){
- if(!/^[ \s]*$/.test(tmp[i])){
- var tr = tmp[i].replace(/^[ \s]*|[ \s]*$/, '').split(reg), line='';
- for(var j=0; j<tr.length; j++){
- if(tr.length>1){
- if(j<1){
- if(t[m+j.toString()]){
- line += (tr[j]+space).slice(0, t['max'+j]-t[m+j.toString()]+p);
- }else line += (tr[j]+space).slice(0, t['max'+j]+p);
- }else{
- if(t[m+j.toString()]){
- line += (space+tr[j]).slice(-t['max'+j]+t[m+j.toString()])+(j<tr.length-1?space.slice(0, p):'');
- }else line += (space+tr[j]).slice(-t['max'+j])+(j<tr.length-1?space.slice(0, p):'');
- }
- }else line += tr[j];
- }
- m++;
- s += line + '\r\n';
- }else if(bl == 1)s += '\r\n';
- }
- return s;
- }
复制代码
|