Vbscript 编辑ini文件所需的脚本帮助
我正在尝试编辑ini文件中的一行。DeviceName=苹果到DeviceName=“用户输入”。我几乎从互联网上的零零碎碎中找到了它。它可以工作,但最终结果是在用户输入后使用正确的条目生成我的文件jwalk.ini,但ini文件已重命名为just.ini,在ini之前没有jwalk。我一定错过了什么。文件jwalk.ini已经存在,我只需要用新的用户输入编辑它,并保持文件名不变 我的剧本:Vbscript 编辑ini文件所需的脚本帮助,vbscript,Vbscript,我正在尝试编辑ini文件中的一行。DeviceName=苹果到DeviceName=“用户输入”。我几乎从互联网上的零零碎碎中找到了它。它可以工作,但最终结果是在用户输入后使用正确的条目生成我的文件jwalk.ini,但ini文件已重命名为just.ini,在ini之前没有jwalk。我一定错过了什么。文件jwalk.ini已经存在,我只需要用新的用户输入编辑它,并保持文件名不变 我的剧本: Const ForReading = 1 Const ForWriting = 2 Const Open
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)
有什么想法吗?谢谢。如果您使用了
选项Explicit
,您会被告知
strOutput = "c:\MyFolder\" & strComputer & ".ini"
使用未定义/未初始化的变量strComputer。此处您将“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