Changing content of a file using an exe

Hi Guys,

All i want to do is to change the content of a file. For instance, there is a batch file located at C:\Program Files\Test\test.bat

The content of bat file is as follows:
------------------------------------------------------------
rem Remove files left over from updating

IF EXIST .\updates.bat DEL .\updates.bat
IF EXIST .\erupdates.bat DEL .\erupdates.bat
IF EXIST .\jre\lib\ext\indicim.jar DEL .\jre\lib\ext\indicim.jar

.\jre\lib\security.jar;.\jre\lib\core.jar;.\jre\lib\server.jar;.\jre\lib\graphics.jar;.\jre\lib\jawas.jar;.\jre\lib\jaws.jar;.\jre\lib\xml.jar;.\jre\lib\charsets.jar -mx64M services.ttt.retfg.er.erMain

IF errorlevel 99 goto updatesok
goto end

:updatesok
XCOPY .\Updates\erupdates.bat . /E
IF EXIST .\erupdates.bat .\erupdates

:end
---------------------------------------------------------------

I just want to change this file to have following content (That is basically one more command inserted in between):

------------------------------------------------------------
rem Remove files left over from updating

IF EXIST .\updates.bat DEL .\updates.bat
IF EXIST .\erupdates.bat DEL .\erupdates.bat
IF EXIST .\jre\lib\ext\indicim.jar DEL .\jre\lib\ext\indicim.jar

.\jre\lib\security.jar;.\jre\lib\core.jar;.\jre\lib\server.jar;.\jre\lib\graphics.jar;.\jre\lib\jawas.jar;.\jre\lib\jaws.jar;.\jre\lib\xml.jar;.\jre\lib\charsets.jar -mx64M services.ttt.retfg.er.erMain

.\..\..\"Test Manager"\TMstart.exe

IF errorlevel 99 goto updatesok
goto end

:updatesok
XCOPY .\Updates\erupdates.bat . /E
IF EXIST .\erupdates.bat .\erupdates

:end
---------------------------------------------------------------


I am new to comp science so your ideas would be helpful a good deal. Personally i thought that it can be achieved using a batch script but wonder if it can be done using C++ as i want the final tool to be an .exe file which perform the above intended operation when executed.

Yes, you can use C++ to do this without too much difficulty.

You will need to use text streams to read and write the data on disk, see http://www.cplusplus.com/doc/tutorial/files.html for more info on them.

I would suggest a STL List of Strings to hold the information when you have read it from disk, see http://www.cplusplus.com/reference/stl/list/ fro more info on the List Template.

You then simply find the point in the list you want to insert the new line, then write the list back to disk (replacing the original).

For a few files, using Notepad is a lot quicker, but it is an interesting excersise to make a generic tool for this than can handle replacement in multiple files, automaticaly finding the insertion point, etc.

PS: Please delete the duplicate posts in the Windows Programming Forum and General Forum
Last edited on
Good grief.
Hi Faldrax.

Will keep your comments (about avoiding cross/duplicate posting) in mind from now onwards.
Topic archived. No new replies allowed.