数据库的处理方式- @set @n=0//&cscript.exe -e:jscript "%~f0"&exit /b
-
- folder = "C:\\Kate Green\\txt"; //txt文件所在目录
- flag = true;
-
- fso = new ActiveXObject("Scripting.FileSystemObject");
- tmpdir = fso.GetSpecialFolder(2)+"\\"+fso.GetTempName();
- fso.CreateFolder(tmpdir);
- //把文件转换成标准的csv文件,后面作为数据库处理
- fc = new Enumerator(fso.GetFolder(folder).files);
- for (; !fc.atEnd(); fc.moveNext()) {
- var fi = fc.item();
- if (fi.Path.slice(-4).toLowerCase()==".txt") {
- var s = fi.OpenAsTextStream().ReadAll();
- s = s.replace(/^\s+|\s*\n\s*/g,'\r\n').replace(/ +/g,',');
- //让ADO把GR,SP这两列的数据类型识别成文本,以免ADO自动对数字进行优化去掉后面的零
- if (flag) s = s.replace(/^.*\n/g,
- function(s){return s+new Array(21).join(s.replace(/\bGR\b|\bSP\b/g,'#'));});
- var ts = fso.OpenTextFile(tmpdir+'\\'+fi.Name, 2, true);
- ts.write(s); ts.close();
- }
- }
- //使用ADO处理文本数据
- strConnect = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=' + tmpdir
- + ';Extended Properties="text;HDR=yes;FMT=Delimited;";';
- AdoCnn = new ActiveXObject("ADODB.Connection");
- AdoCnn.ConnectionString = strConnect;
- AdoCnn.Open();
- AdoRsSchema = AdoCnn.OpenSchema(20);//adSchemaTables=20
- s = "GR,SP\r\n";
- AdoRs = new ActiveXObject("ADODB.Recordset");
- while (!AdoRsSchema.EOF) {
- AdoRs.Open("SELECT [GR],[SP] FROM ["+AdoRsSchema("table_name")+"]", AdoCnn, 3);
- if (flag) AdoRs.Move(20);
- s += AdoRs.GetString(2, AdoRs.RecordCount, ",", "\r\n");
- AdoRs.Close();
- AdoRsSchema.MoveNext();
- }
- fso.OpenTextFile('result.csv', 2, true).write(s);
- fso.DeleteFolder(tmpdir);
复制代码
|