2012-03-12 44 views
-3

嘿scripter我在这里在我的hta数组加载并保存到文本文件。现在我想改变这一点,并希望将该文件的第一行存储在此位置: strUserProfile & "/CAD_Kunde.txt"以及此位置的其余部分:i:\CAD_Kunden.txt。我的问题是我现在不怎么分割这个数组,因为这两个部分都需要在同一个函数中。用户Teemu对我的脚本进行了大部分更改。希望可以有人帮帮我。这里是我的代码更好的理解: 编辑:@All停止klicking投下来,我现在didnt如何解释我的问题,我的英语不是最好的javascript分割东西在不同位置保存在两个部分

<html> 
<head> 
<title>CATIA Starttool</title> 
<HTA:APPLICATION 
    ID="myCATIA" 
    APPLICATIONNAME="myCatia" 
    SCROLL="no" 
> 
//****** Check if CAD_Kunde.txt exists when not create file 
<SCRIPT Language="VBScript"> 
Option Explicit 
Set objShell=CreateObject("Wscript.Shell") 
strUserProfile=objShell.ExpandEnvironmentStrings("%USERPROFILE%") 
dim filesys, filetxt, objShell, strUserProfile 
Const ForReading = 1, ForWriting = 2, ForAppending = 8 
Set filesys = CreateObject("Scripting.FileSystemObject") 
If not filesys.FileExists(strUserProfile & "/CAD_Kunde.txt") Then 
    Set filetxt = filesys.OpenTextFile(strUserProfile & "/CAD_Kunde.txt", ForWriting, True) 
    filetxt.WriteLine ("1") 
    filetxt.WriteLine ("BEHR;B18 SP4;B18 SP7;0") 
    filetxt.WriteLine ("AUDI;B19 SP3;0") 
    filetxt.WriteLine ("BMW;B19 SP3;0") 
    filetxt.WriteLine ("BIT;B18 SP4;B19 SP3;B16 SP9;0") 
    filetxt.WriteLine ("CHRYSLER;B18 SP4;0") 
    filetxt.WriteLine ("CAE_64;B19 SP3;0") 
    filetxt.WriteLine ("CNHTC;B19 SP3;0") 
    filetxt.WriteLine ("DAF;B19 SP3;0") 
    filetxt.WriteLine ("DASSAULT;B18 SP7;B19 SP3;0") 
    filetxt.WriteLine ("DFCV;B19 SP3;0") 
    filetxt.WriteLine ("FORD;B18 SP4;0") 
    filetxt.WriteLine ("FREIGHTL;B19 SP3;0") 
    filetxt.WriteLine ("FUSO;B19 SP3;0") 
    filetxt.WriteLine ("HINO;B19 SP3;0") 
    filetxt.WriteLine ("HONDA;B19 SP3;0") 
    filetxt.WriteLine ("IVECO;B19 SP3;0") 
    filetxt.WriteLine ("MAN;B19 SP3;0") 
    filetxt.WriteLine ("NISSAN;B19 SP3;0") 
    filetxt.WriteLine ("MB;B19 SP3;0") 
    filetxt.WriteLine ("PACCAR;B19 SP3;0") 
    filetxt.WriteLine ("PORSCHE;B19 SP3;0") 
    filetxt.WriteLine ("PORSCHE_HYBRID_DX;B19 SP3;0") 
    filetxt.WriteLine ("PSA;B19 SP3;0") 
    filetxt.WriteLine ("RENAULT;B19 SP3;0") 
    filetxt.WriteLine ("RVI;B18 SP4;0") 
    filetxt.WriteLine ("SCANIA;B19 SP3;0") 
    filetxt.WriteLine ("TELCO;B19 SP3;0") 
    filetxt.WriteLine ("UD_TRUCK;B17 SP4;0") 
    filetxt.WriteLine ("VOLVO_CAR;B18 SP4;0") 
    filetxt.WriteLine ("VOLVO_TRUCK;B19 SP3;0") 
    filetxt.WriteLine ("V5_MM;B18 SP4;0") 
    filetxt.WriteLine ("V5_BM;B19 SP3;0") 
    filetxt.WriteLine ("V5_TEST;B19 SP9;0") 
    filetxt.WriteLine ("VW;B19 SP3;0") 
    filetxt.WriteLine ("") 
    filetxt.Close 
End If 

</script> 

//****** Open last used environment and set it as default selection 
<script type="text/javascript"> 

// Reads textfile, file is created automatically, if not exist 
function readCustomers(){ 
var fso,iStream,cust,n,tarr; 
shell=new ActiveXObject('WScript.Shell'); 
userPath=shell.ExpandEnvironmentStrings('%UserProfile%'); 
customers={}; 
fso=new ActiveXObject('Scripting.FileSystemObject'); 
iStream=fso.OpenTextFile(userPath+'/CAD_Kunde.txt',1,true); 
cust=iStream.ReadLine(); 
for(n=0;!iStream.AtEndOfStream;n++){ 
    tarr=iStream.ReadLine().split(';'); 
    customers[n]=new Customer(tarr);   
} 
iStream.Close(); 
customers[0].selectionElement.selectedIndex=cust; 
alert(cust); 
    customers[cust].createReleaseOptions(cust); 
window.activeCustomer=cust; 
return; 
} 

// Customer (environment) constructor function 
function Customer(txt){ 
    var n,x=0; 
    this.selectionElement=document.getElementById('select1'); 
    this.customerName=txt[0]; 
    this.catiaRelease=[]; 
    for(n=1;n<txt.length-1;n++){ 
     this.catiaRelease[n-1]=txt[n]; 
     x++; 
    } 
    this.len=x; 
    this.defaultOption=parseInt(txt[txt.length-1]); 
    this.selected=false; 
    this.createCustomerOption(); 
} 

// Creates customer (environment) options 
Customer.prototype.createCustomerOption=function(){ 
    var opt; 
    opt=document.createElement('OPTION'); 
    opt.text=this.customerName; 
    opt.value=this.customerName; 
    this.selectionElement.add(opt); 
    return; 
} 

// Creates release options 
Customer.prototype.createReleaseOptions=function(idx){ 
    var target,n,opt; 
    target=document.getElementById('select2'); 
    target.length=0; 
    for(n=0;n<this.len;n++){ 
     opt=document.createElement('OPTION'); 
     opt.text=this.catiaRelease[n]; 
     opt.value=this.catiaRelease[n]; 
     target.add(opt);  
    } 
    target[this.defaultOption].selected=true; 
    //target[0].selected=true; // Move comment line above, if this option is used 
    window.activeCustomer=idx; 
    return; 
} 

// Saves textfile 
function saveCustomers(){ 
var fso,oStream,n,m,str='',cust=document.getElementById('select1');eos=cust.length; 
fso=new ActiveXObject('Scripting.FileSystemObject'); 
oStream=fso.OpenTextFile(userPath+'/CAD_Kunde.txt',2,true); 
oStream.WriteLine(cust.selectedIndex); 
for(n=0;n<eos;n++){ 
    str=''; 
    str+=customers[n].customerName+';'; 
    for(m=0;m<customers[n].len;m++){ 
     str+=customers[n].catiaRelease[m]+';'; 
    } 
    str+=customers[n].defaultOption; 
    oStream.WriteLine(str); 
} 
oStream.Close(); 
} 


function OpenAction(object) { 
    self.resizeTo(299,299); 
    OpenActionVB(); 
    readCustomers(); 

} 

function RunScript(object) { 
    saveCustomers(); 
    BetriebssystemKommando(object.select1.options[object.select1.selectedIndex].value, 
          object.select2.options[object.select2.selectedIndex].value, 
          select3=document.getElementById('OSName'), 
          object.select4.options[object.select4.selectedIndex].value, 
          object.select5.options[object.select5.selectedIndex].value, 
          object.select6.options[object.select6.selectedIndex].value); 
} 
+0

你可以修剪它吗? – 2012-03-12 14:10:18

+0

我已经把它修剪了下来。 – user1225282 2012-03-12 14:14:39

+0

这个问题目前的形式是无法回答的。一般来说,如果你的代码片段有一个滚动条,你做错了。 – jbabey 2012-03-12 14:27:21

回答

0

这再次:)。

<SCRIPT type="text/javascript"> 
// ** CREATE CUSTOMER-OBJECT 
function Customer(txt){ 
    var n,x=0; 
    this.selectionElement=document.getElementById('select1'); 
    this.customerName=txt[0]; 
    this.catiaRelease=[]; 
    for(n=1;n<txt.length;n++){ 
     this.catiaRelease[n-1]=txt[n]; 
     x++; 
    } 
    this.len=x; 
    this.selected=false; 
    this.createCustomerOption(); 
} 

// ** CREATE CUSTOMER OPTIONS 
Customer.prototype.createCustomerOption=function(){ 
    var opt; 
    opt=document.createElement('OPTION'); 
    opt.text=this.customerName; 
    opt.value=this.customerName; 
    this.selectionElement.add(opt); 
    return; 
} 

// ** CREATE RELEASE OPTIONS 
Customer.prototype.createReleaseOptions=function(idx){ 
    var target,n,opt; 
    target=document.getElementById('select2'); 
    target.length=0; 
    for(n=0;n<this.len;n++){ 
     opt=document.createElement('OPTION'); 
     opt.text=this.catiaRelease[n]; 
     opt.value=this.catiaRelease[n]; 
     target.add(opt); 
    } 
    target[0].selected=true; 
    window.activeCustomer=idx; 
    return; 
} 

// *** SAVE DEFAULT SELECTION FOR CURRENT USER ONLY 
function saveCustomers(){ 
    var fso,oStream,n,m,str='',cust=document.getElementById('select1'),eos=cust.length,shell,userPath; 
    shell=new ActiveXObject('WScript.Shell'); 
    userPath=shell.ExpandEnvironmentStrings('%UserProfile%'); 
    fso=new ActiveXObject('Scripting.FileSystemObject'); 
    oStream=fso.OpenTextFile(userPath+'/DEFCATIASEL.txt',2,true); // * SAVE THE DEFAULT SELECTION 
    oStream.WriteLine(cust.selectedIndex); 
    oStream.Close(); 
    return; 
} 

// *** READ ENVIRONMENT FROM SERVER AND DEFAULT SELECTION FROM CURRENT USER FILE 
function readCustomers(){ 
    var fso,iStream,cust,n,tarr,shell,userPath; 
    customers={}; 
    shell=new ActiveXObject('WScript.Shell'); 
    fso=new ActiveXObject('Scripting.FileSystemObject');  
    userPath=shell.ExpandEnvironmentStrings('%UserProfile%'); 
    iStream=fso.OpenTextFile(userPath+'/DEFCATIASEL.txt',1,true); // * READ THE DEFAULT SELECTION 
    try{ 
     cust=iStream.ReadLine(); 
    } catch(e){cust=0;} 
    iStream.Close(); 
    iStream=fso.OpenTextFile('YOUR_SERVER_PATH/CAD_Kunde.txt',1,false); // * READ ENVIRONMENTS AND RELEASES FROM SERVER 
    for(n=0;!iStream.AtEndOfStream;n++){ 
     tarr=iStream.ReadLine().split(';'); 
     customers[n]=new Customer(tarr); 
    } 
    iStream.Close(); 
    customers[0].selectionElement.selectedIndex=cust; // * SELECTS THE DEFAULT ENVIRONMENT 
    customers[cust].createReleaseOptions(cust); 
    window.activeCustomer=cust; 
    return; 
} 
</SCRIPT> 

对于该系统,您必须从CAD_Kunde.txt中删除“发布号码”。所以,你会使用看起来像这样的文件:

BEHR;B18 SP4;B18 SP7 
AUDI;B19 SP3 
BIT;B18 SP4;B19 SP3;B16 SP9 

该文件应手动更新(使用文本编辑器)的新环境或释放。

+0

谢谢Teemu它正在做我想要的东西。我为我添加了一个防止错误的部分,该部分在userprofile中不存在时创建DEFAULTSEL.txt。是的,那就是我希望服务器上的CAD_kunde.txt添加全局新环境并在需要时释放。我已将你的答案标记为正确答案。 – user1225282 2012-03-13 07:24:12