2017-09-13 120 views
1

我喜歡把自己的錯誤「10號線和2字符無效過程調用或參數」我運行下面VBSobjFile.Write(現在)無效的過程調用或參數

10號線和2夏亞

Const ForAppending = 8 
Set objFSO = CreateObject("scripting.filesystemobject") 
GetLogPath = "C:\Users\MyName\Desktop\New Text Document.log" 
If objFSO.FileExists(GetLogPath) Then 
    set objFile = objFSO.OpenTextFile(GetLogPath) 
else 
    set objFile = objFSO.CreateTextFile(GetLogPath) 
End If 
set objFile = objFSO.OpenTextFile(GetLogPath , ForAppending) 
    objFile.Write(FormatDateTime(Now)) 
    objFile.WriteLine(" : ") 
    objFile.Close 

我的系統日期和時間設置如下。

Bulgaria Date and Time settings

但是當我跑在其他PC和工作包含英語日期和時間設置精細相同的腳本。

< 輸出> 2017年9月13日下午5時44分十五秒:

能否請您在此情況下幫助。

+0

爲什麼你甚至需要'FormatDateTime'?你有沒有嘗試在文件中編寫'Now'? –

+0

是的,我試過用「objFile.Write Now」和「objFile.Write(Now)」。和我觀察所有的日期和時間功能,如日期,日......同樣的問題...... [鏈接](https://www.w3schools.com/asp/asp_ref_vbscript_functions.asp) –

+0

我已經讀過VBScript可能運行的地方進入非英語語言環境的問題...嘗試通過連接datepart函數手動格式化日期和時間函數,如日,月和年 –

回答

0

@Gurman感謝您的有價值的解決方案。一些如何我無法實現您的解決方案。所以我寫了下面的函數(Function GetNow())來達到我的要求。

Const ForAppending = 8 
strComputer = "." 
Set objFSO = CreateObject("scripting.filesystemobject") 
GetLogPath = "C:\Users\MyName\Desktop\New Text Document.log" 
If objFSO.FileExists(GetLogPath) Then 
    set objFile = objFSO.OpenTextFile(GetLogPath) 
else 
    set objFile = objFSO.CreateTextFile(GetLogPath) 
End If 
set objFile = objFSO.OpenTextFile(GetLogPath , ForAppending) 
    objFile.Write(GetNow) 
    objFile.WriteLine(" : ") 
    objFile.Close 

Function GetNow() 

Set objWMIServiceNow = GetObject("winmgmts:\\" & strComputer & "\root\cimv2") 
Set colItems = objWMIServiceNow.ExecQuery("Select * from Win32_OperatingSystem") 

For Each objItem in colItems 
    dtmLocalTime = objItem.LocalDateTime 
    dtmMonth = Mid(dtmLocalTime, 5, 2) 
    dtmDay = Mid(dtmLocalTime, 7, 2) 
    dtmYear = Left(dtmLocalTime, 4) 
    dtmHour = Mid(dtmLocalTime, 9, 2) 
    dtmMinutes = Mid(dtmLocalTime, 11, 2) 
    dtmSeconds = Mid(dtmLocalTime, 13, 2) 
Next 

GetNow = dtmMonth & "/" & dtmDay & "/" & dtmYear & " [" & dtmHour & ":" & dtmMinutes & ":" & dtmSeconds & "]" 

End Function 

感謝所有的時間和支持:)

拉梅什

0

錯誤原因:不正確編碼格式

你所得到的錯誤,因爲你試圖寫入文本文件中的文本是在保加利亞的語言,而你的文本文件的編碼是ANSI(見圖片)默認爲。解決方案是將文件保存爲通用編碼,可以是Unicode或UTF-8。這些編碼將保加利亞字符映射到Unicode標準。您將不得不以Unicode格式打開/創建日誌文件,以便能夠編寫保加利亞字符。

測試的代碼:

Const ForAppending = 8 
Set objFSO = CreateObject("scripting.filesystemobject") 
GetLogPath = "C:\Users\Kira\Desktop\url.log" 

'The entire If condition of your code can be replaced with the following line of code: 
Set objFile = objFSO.OpenTextFile(GetLogPath,ForAppending,True,-1)  'The 3rd parameter "True" is for creating the textfile if the textfile is already not created and the 4th parameter is for specifying the Encoding format. -1 means Unicode; for ANSI, leave the last param blank. 

objFile.Write(FormatDateTime(Now)) 
objFile.WriteLine(" : ") 
objFile.Close 

OUTPUT:

enter image description here

如需更多幫助,請點擊HERE

編碼格式:

Encoding

+1

註釋和鏈接引用CreateTextFile方法;該代碼使用OpenTextFile。 OpenTextFile的第4個參數是* not *布爾值,但是是TriState。 –

+0

@ Ekkehard.Horner我的壞。我已更新鏈接和解決方案 – Gurman

相關問題