2012-03-10 54 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