Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open two files, create variable with one and write to other 1

Status
Not open for further replies.

pellet

Programmer
Oct 23, 2008
55
US
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.
 
I would like to add that the info I am attempting to add in the .ini file is a number that will go at the end of "FileNumber=" which is a location in the .ini file. Currently I have "FileNumber=999" and I want to overwrite the 999 with my variable from the .cfg file but keep everything else in the .ini intact.
 
read the contents of the .ini file and use replace(data, "999", intVariable.

'assign variable with variable name
strVariableName = "MyVar"

'open the file for writing
set objFile = objFSO.OpenTextFile("File.ini", 2)

'store file contents
strContents = objFile.ReadAll

'replace FileNumber=
strContents = replace(strContents, "FileNumber=", "FileNumber=" & strVariableName)

'write the contents back and close
objFile.Write strContents
objFile.Close

Additionally, you can open the .cfg file for reading and get the variable name from there.

-Geates
 
Thanks Geates for the reply. I looked at your script and even though I thought I couldn't open a file for writing and do a ReadAll, I tried it anyway. I get an error as the file opens for writing and then I tried to do a ReadAll, I get an error message "Bad File Mode".

Any other ideas?
 
then open the file for reading and save the output to a new file.

'Open file.ini for reading (mode 1)
set objFile = objFSO.OpenTextFile("InputFile.ini", 1)

'Open a new file for writing (mode 2)
set objFile = objFSO.OpenTextFile("OutputFile.ini", 2)

You'' have to alter the code for fit your requirements.

-Geates
 
Hi Geates,

Is it possible to open a file for writing and do a readall on that file? That seems to be where I am coming up with the problem.

Thanks again for your assistance.
 
Unfortunately, I don't think there is a silver bullet for this issue in VBScript. However, it can be done by opening a file for reading, get contents, close the file, and re-open for writing.

set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = CreateObject("WScript.Shell")

'Open file for reading, read contents and close
set objFile = objFSO.OpenTextFile("C:\file.ext", 1, true)
strContents = objFile.ReadAll
objFile.Close

'Open file for writing,
set objFile = objFSO.OpenTextFile("C:\file.ext", 2)


-Geates
 
Thanks Geates, that's what I thought - and that is where I seem to be having the problem with taking the variable from the .cfg file and putting it in the .ini file. For some reason, I cannot think of how to find the line I need to find in the .ini file and write the contents of the .cfg file to the .ini file...
 
This example replaces specific content from one file and with the specific content of another file. It then outputs the result to a file. The entire example can be found here:

Code:
CONST READING = 1
CONST WRITING = 2

set objFSO = CreateObject("Scripting.FileSystemObject")
set objShell = WScript.CreateObject("WScript.Shell")

set objFile = objFSO.OpenTextFile (".\pellet.cfg", READING)
strCFG = objFile.ReadAll
strID = mid(strCFG, inStr(strCFG, "FileID=") + 7, 3) 'returns C11
objFile.Close

set objFile = objFSO.OpenTextFile (".\pellet.ini", READING)
strINI = objFile.ReadAll
strNumber = mid(strINI, inStr(strINI, "FileNumber=") + 11, 3) 'returns "I99"
objFile.Close

'----Not working below this line (It is now)-----

set objFile = objFSO.OpenTextFile(".\result.ini", WRITING, true)
strContents = replace(strINI, strNumber, strID) 'replace "I99" with "C11"

objFile.WriteLine strContents
objFile.Close

objShell.Run ".\result.ini"

-Geates
 
Geates,

Thanks a million! I was able to use some of your code and it worked! I really appreciate your help and expertise.

I looked at some of my code and the more I look at it compared to yours, I think I really over thought my script. I guess that's a tell tale sign of inexperience. :)

Thank you so much!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top