Hello everyone,
First of all, I would like to thank everyone here for helping me learn more and more about scripting every day. I appreciate the time and dedication from all of you to help myself and others in creating scripts and helping me understand vbscript.
Currently I am trying to write a script that opens up a .cfg file that has certain data in it that I want to capture and write to an .ini file. The script works to the point of me being able to echo the info I want out of the .cfg file and I can also echo the area in the .ini file that I want overwritten with the variable created from the .cfg file. The problem is, I cannot seem to write the info from the .cfg file to where I need it to go in the .ini file. I have attached my script for your review. I am sure it is something simple, but for some reason I am running into a road block. Any assistance would be greatly appreciated. Thanks in advance.
-----------------------------------------------------------
'DEFINE CONSTANTS
Const ForReading = 1
Const ForWriting = 2
'DEFINE VARIABLES
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShl = WScript.CreateObject("WScript.Shell")
Set objFileCfg = objFSO.OpenTextFile ("C:\config.cfg", ForReading)
Set objFileIni = objFSO.OpenTextFile ("C:\test.ini", ForReading )
Dim strCfg, strIni
'LOOP THE CONFIG FILE
Do Until objFileCfg.AtEndOfLine
strData = ""
strSearchString = objFileCfg.ReadLine
'SEARCH THE FILE FOR ID NUMBER
intMobNum = InStr(strSearchString, "File id=")
'PARSE OUT THE NEEDED INFO
If intMobNum <> 0 Then
intMobNum = intMobNum + 11
strCfg = Mid(strSearchString, intMobNum, 3)
WScript.Echo strCfg
End If
Loop
objFileCfg.Close
'LOOP THE .INI FILE
Do Until objFileIni.AtEndOfLine
strData = ""
strDeleteString = objFileIni.ReadLine
'SEARCH THE FILE FOR ID NUMBER
intUnitNum = InStr(strDeleteString, "FileNumber=")
'PARSE OUT THE NEEDED INFO
If intUnitNum <> 0 Then
intUnitNum = intUnitNum + 11
strIni = Mid(strDeleteString, intUnitNum, 3)
End If
Loop
WScript.Echo strIni
objFileIni.Close
Set objFileIni = Nothing
'----Not working below this line-----
Set objFileIni = objFSO.OpenTextFile("C:\test.ini", ForWriting)
strNewContents = Replace(strDeleteString, strIni, strCfg)
objFileIni.WriteLine strNewContents
objFileIni.Close
-----------------------------------------------------------
When I try to write to the .ini file, the variable from strCfg writes to the .ini file and deletes everything else.
First of all, I would like to thank everyone here for helping me learn more and more about scripting every day. I appreciate the time and dedication from all of you to help myself and others in creating scripts and helping me understand vbscript.
Currently I am trying to write a script that opens up a .cfg file that has certain data in it that I want to capture and write to an .ini file. The script works to the point of me being able to echo the info I want out of the .cfg file and I can also echo the area in the .ini file that I want overwritten with the variable created from the .cfg file. The problem is, I cannot seem to write the info from the .cfg file to where I need it to go in the .ini file. I have attached my script for your review. I am sure it is something simple, but for some reason I am running into a road block. Any assistance would be greatly appreciated. Thanks in advance.
-----------------------------------------------------------
'DEFINE CONSTANTS
Const ForReading = 1
Const ForWriting = 2
'DEFINE VARIABLES
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshShl = WScript.CreateObject("WScript.Shell")
Set objFileCfg = objFSO.OpenTextFile ("C:\config.cfg", ForReading)
Set objFileIni = objFSO.OpenTextFile ("C:\test.ini", ForReading )
Dim strCfg, strIni
'LOOP THE CONFIG FILE
Do Until objFileCfg.AtEndOfLine
strData = ""
strSearchString = objFileCfg.ReadLine
'SEARCH THE FILE FOR ID NUMBER
intMobNum = InStr(strSearchString, "File id=")
'PARSE OUT THE NEEDED INFO
If intMobNum <> 0 Then
intMobNum = intMobNum + 11
strCfg = Mid(strSearchString, intMobNum, 3)
WScript.Echo strCfg
End If
Loop
objFileCfg.Close
'LOOP THE .INI FILE
Do Until objFileIni.AtEndOfLine
strData = ""
strDeleteString = objFileIni.ReadLine
'SEARCH THE FILE FOR ID NUMBER
intUnitNum = InStr(strDeleteString, "FileNumber=")
'PARSE OUT THE NEEDED INFO
If intUnitNum <> 0 Then
intUnitNum = intUnitNum + 11
strIni = Mid(strDeleteString, intUnitNum, 3)
End If
Loop
WScript.Echo strIni
objFileIni.Close
Set objFileIni = Nothing
'----Not working below this line-----
Set objFileIni = objFSO.OpenTextFile("C:\test.ini", ForWriting)
strNewContents = Replace(strDeleteString, strIni, strCfg)
objFileIni.WriteLine strNewContents
objFileIni.Close
-----------------------------------------------------------
When I try to write to the .ini file, the variable from strCfg writes to the .ini file and deletes everything else.