objFile.Write(现在)无效的过程调用或参数

问题描述:

我喜欢把自己的错误“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函数手动格式化日期和时间函数,如日,月和年 –

@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 

感谢所有的时间和支持:)

拉梅什

错误原因:不正确编码格式

你所得到的错误,因为你试图写入文本文件中的文本是在保加利亚的语言,而你的文本文件的编码是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