2012-03-10 112 views
0

我试图编辑ini文件中的一行。 DeviceName = APPLE到DeviceName =“用户输入”。我通过互联网上的零零星星几乎找到了它。它的工作原理除了最终的结果是我的文件jwalk.ini在用户输入后有正确的输入,但ini文件已经重命名为.ini,ini之前没有jwalk。我肯定错过了什么。 jwalk.ini文件已经存在,我只需要用新的用户输入对它进行编辑,并保持命名相同的文件。脚本帮助需要编辑ini文件

我的脚本:

Const ForReading = 1 
Const ForWriting = 2 
Const OpenAsASCII = 0 
Const CreateIfNotExist = True 

Set objFSO = CreateObject("Scripting.FileSystemObject") 
' Open existing file for read access. 
strInput = "c:\MyFolder\jwalk.ini" 
Set objInput = objFSO.OpenTextFile(strInput, ForReading) 

' Prompt for device name. 
strDeviceName = InputBox("Enter devicename", "JWalk PC Name or Session Name") 

' Specify the new file name. 
strOutput = "c:\MyFolder\" & strComputer & ".ini" 

' Create new file with write access. 
Set objOutput = objFSO.OpenTextFile(strOutput, _ 
ForWriting, CreateIfNotExist, OpenAsASCII) 

' Process input file. 
Do Until objInput.AtEndOfStream 
' Read next line of the input file. 
strLine = objInput.ReadLine 
' Search for line to be modified. 
' Make search case insensitive. 
If (InStr(LCase(strLine), "devicename=") > 0) Then 
' Replace the line. 
' You could also modify the line. 
strLine = "devicename=" & strDeviceName 
End If 
' Write line to the output file. 
objOutput.WriteLine strLine 
Loop 

' Clean up. 
objInput.Close 
objOutput.Close 

' Delete the original file. 
objFSO.DeleteFile(strInput) 

任何想法?谢谢。

回答

1

如果你已经使用Option Explicit,你会被告知,

strOutput = "c:\MyFolder\" & strComputer & ".ini" 

使用未定义/未初始化变量strComputer。

1

这里要传递“将strComputer”作为一个变种,但从来没有设置它的值:

' Specify the new file name. 
strOutput = "c:\MyFolder\" & strComputer & ".ini" 

如果你试图让计算机名称,你可以这样考虑:

' Specify the new file name. 
strOutput = "c:\MyFolder\" & GetComputerName() & ".ini" 

Function GetComputerName() 
    Dim ob 
    Set ob = Wscript.CreateObject("Wscript.Network") 
    GetComputerName = ob.ComputerName 
    Set ob = nothing 
End Function