javascript将东西分成两部分保存在不同的位置

时间:2012-03-12 13:35:56

标签: javascript html vbscript hta

嘿脚本编写者我在我的hta中有一个加载并保存到文本文件的数组。现在我想更改此设置并希望将文件的第一行存储在此位置: strUserProfile & "/CAD_Kunde.txt"以及此位置的其余部分:i:\CAD_Kunden.txt。我的问题是我现在不知道如何分割这个数组,因为在同一个函数中需要这两个部分。用户Teemu对我的脚本进行了大部分更改。希望可以有人帮帮我。这是我的代码,以便更好地理解: 编辑:@all停止点击投票我现在没有解释我的问题,我的英语不是最好的

<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); 
} 

1 个答案:

答案 0 :(得分: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

应该为新环境或版本手动更新此文件(使用文本编辑器)。