2014-07-02 39 views
0

我得到一個語法錯誤,但我看不出爲什麼。 我目前正在寫一些腳本在vbs和im新的vbs。 由於這個腳本的行爲讓我困惑,我得到了我做錯了什麼的想法。 這裏是我的代碼:Vbs引發語法錯誤。我看不出爲什麼

Option Explicit 

Const read =1,overwrite=2,append=8 'constants for parameters of OpenTextFile command 

Dim fos,i,ofile,text,month,day,year,hour,minute,day2 

Set fos =CreateObject("Scripting.FileSystemObject") 
'fos.OpenTextFile("path",type) 

ofile =fos.OpenTextFile("C:\Users\martin\Desktop\txt\ReminderV1_0\daten\termin1.txt",1) 

'read a date from a txt file 
day=ofile.readline 
month=ofile.readline 
year=ofile.readline 
hour=ofile.readline 
minute=ofile.readline 
set ofile=ofile.close 

wscript.echo "abcde" 'i never get a messagebox "abcde" and i dont know why 

If day eqv "01" then day2="first" 'use of eqv or = seems to be meaningless 
ElseIf day="02" day2="second" 
ElseIf day="03" day2="third" 
Else day2=day & "-th" 
End if 

msgbox "2" 

text="the " & day2 & " of "& month & " " & year & " at "& hour &":"& minute &" o'clock" 

msgbox "3" 
msgbox text 

它應該閱讀從一個txt文件(名稱:「termin1」)的日期和時間所在的文件夾中,尋找這樣的:

01 
07 
2014 
19 
20 

我的天堂離得很遠。 如果我運行此我得到了以下錯誤消息: 對不起,翻譯不好,也許(我是德國人)

Line: 23 
token/char: 5 
Error: Syntaxerror 
Code: 800A03EA 
Source: Compilationerror in Microsoft VBScript 

因此,這將是我的第一個ELSEIF的「E」。 但是,因爲我在Youtube和幾個網站上查詢它的語法是正確的。 即使我在第20行(「abcde」)呼叫回聲,我從來沒有得到任何回聲。 我唯一的猜測是,我曾經使用任何對象錯誤或違反任何規則,我還不知道,因爲我缺乏經驗。

回答

1

有幾個問題與您的代碼。

  1. 單行語法(If foo Then bar)不支持ElseIf,即使它沒有,你還是不得不把整個語句在一行。在你的情況,你必須使用塊語法:

    If condition1 Then 
        ... 
    ElseIf condition2 Then 
        ... 
    ElseIf condition3 Then 
        ... 
    Else 
        ... 
    End If 
    
  2. eqv不是在VBScript有效的比較操作。使用=

  3. Day是一個內置函數,所以它不應該被用作變量。 Month,Year,HourMinute也是如此。改爲使用不同的變量名稱。

綜上所述,改變這種:

day=ofile.readline 
month=ofile.readline 
year=ofile.readline 
hour=ofile.readline 
minute=ofile.readline 
set ofile=ofile.close 

wscript.echo "abcde" 'i never get a messagebox "abcde" and i dont know why 

If day eqv "01" then day2="first" 'use of eqv or = seems to be meaningless 
ElseIf day="02" day2="second" 
ElseIf day="03" day2="third" 
Else day2=day & "-th" 
End if 

到這一點:

dayRead = ofile.ReadLine 
monthRead = ofile.ReadLine 
yearRead = ofile.ReadLine 
hourRead = ofile.ReadLine 
minuteRead = ofile.ReadLine 
ofile.Close 

Wscript.Echo "abcde" 

If dayRead = "01" Then 
    day2 = "first" 
ElseIf dayRead = "02" Then 
    day2 = "second" 
ElseIf dayRead = "03" Then 
    day2 = "third" 
Else 
    day2 = dayRead & "-th" 
End If 

和錯誤(S)將消失。

+0

非常非常有幫助謝謝。一切正在工作。至今 :) – user3797318

0

試試這個

If day = "01" then day2="first" 'use of eqv or = seems to be meaningless 
    ElseIf day="02" then day2="second" 
    ElseIf day="03" then day2="third" 
    Else day2=day & "-th" 
End if 
相關問題