Change the content of a file using batch or c++ programme

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.

I would take a look at File I/O operations.
http://www.cplusplus.com/doc/tutorial/files.html

Then look on how to manipulate the location of the file pointer; with that information you should be able to do what you want.
You know, I just can't figure out how people figure that cross-posting on the same forum will help their cause.

I'm inclined to go delete my response here: http://www.cplusplus.com/forum/general/5542/
Last edited on
Mmhm, I don't even bother to post on the spam, I just report it...
Hi Duoas, Firedraco.

Will keep your comments (about avoiding cross/duplicate posting) in mind from now onwards.
Good. It really ticks people off.

Were we able to help you solve the problem? Or are you still stuck?
Hi Duoas,

I wanted to do this either by batch file or c++. However, i just got my purpose solved using dos commands(batch file), i could well understood your responce in the other forum. Reading more about File I/O operation in C++..
Personally, I would've used notepad. But that's just me.
LOL. ;-)

For some reason, the guys at MS just can't fathom that anyone would still want to use cmd.exe to do this kind of thing, so some things are still impossible with pure cmd. Like reading blank lines from a file.

(Otherwise it wouldn't take much to write a batch script to do it... maybe 60 or 70 lines for general code; maybe a third that for your specific need.)

AFAIK.
XD, gotta love MS.
To be fair, Unix shells can't do it either. The difference is that on Unix you can generally expect utilities like sed to be present. You can get versions that work on DOS/Win32, but you simply cannot expect your end user to have it.
Topic archived. No new replies allowed.