Changing content of a file

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.

Unfortunately, even with all the updates to cmd.exe, the fellas over at MS never figured people would do stuff like you want in pure batch script, so you'll need to write yourself a little C++ program (or use another shell interpreter) to do it.

In C++, a deque of lines is probably the easiest to modify.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <deque>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
  {
  deque <string> text;
  string s;

  // load the file
  ifstream inf( filename );  // You must supply the proper filename
  if (!inf) ...              // Check for errors here
  while (getline( inf, s ))
    text.push_back( s );
  if (!inf.eof()) ...        // and here
  inf.close();

  // modify the text
  text.insert(
    text.begin() + linenumber,  // again, you must supply the line number
    string( "This is a new line of text" )
    );

  // rewrite the new text to file
  ofstream outf( filename );
  if (!outf) ...
  for (deque <string> ::iterator
       line  = text.begin();
       line != text.end();
       ++line)
    outf << *line << '\n';
  outf.close();

  return 0;
  }

How you get the filename and line number to insert the new line(s) is, of course, up to you. You might also want to look at the deque::insert documentation for variations.

Hope this helps.
Last edited on
Topic archived. No new replies allowed.