modify the file (a little triky one)

tamhas

ProgressTalk.com Sponsor
If you are modifying source code, why is there a requirement to not change the create date of the file? This seems like one of the most basic elements of version control.

If Unix, I have a shell script I wrote a million years ago that uses sed and sdfiff and will give you preview of the changes to be made side by side so that you can tell it is going to do the right thing. It will be a ***lot*** faster than doing this with Progress. Note that this won't preserve your create dates, but it is vastly safer because of the ability to preview.

BUT, if it is source code, you are limiting yourself greatly by treating it as text. There are a whole lot of ways to get in big trouble because you think you are changing a database field name, for example, but that also turns out to be a variable name, a loop name, part of a comment, etc. Not to mention, of course, the problem one can get into if the code includes any abbreviated fields, tables, or keywords. To do this properly you should use ProParse or ProRefactor so that you are operating on syntactically meaningful units.

BTW, a brief statement in the initial post that this what you wanted to do would have produced a different discussion.
 

ron

Member
As I think everyone has advised you - attempting to update a bunch of source files "in situ" is highly dangerous. You need to put your modified files in a different directory so you can check them and make certain that there have not been any "accidental" changes that you don't want.

When you are sure that the modified files are correct you can move them back to the original directory. The OS will set each file to have a new date of last change - but should preserve the original date of creation.

Now we get back to the issue of how to change the files. You can take the 4GL code Tom gave you and enhance it to process all files in a directory (or negotiate to have him do that for you). Or, as I mentioned before, I can give you a small Perl program that will do exactly what you want - but you have to download the Perl environment so that it will run.

Good luck!
Ron.
 

shireeshn

Member
no tom , we have two database's, now i want to move to one database and delete other, i want to find the old database(deleted) and replace with new database.

if we have strings to replace of different length's (replace old one with new one) and add the comment at the end of the line when modifed.

Can we able to do ? , can any one help me in this.

yes tamhas, ur correct, this variable can also be in the given field name ... etc but iam checking with lookup. that has solved my problem.

tamhas or ron, please provide me the script in Perl or some other languge, first i will check with 4GL and if doesn't work with it i will use ur script. (boz there are lot of process i should follow to get install the perl or other in my system)

Positive thing :- i really dont know how to read from same file and write to same file. really learnt good one from this thread. Thanks to tom ,Tamhas and Con.
 

ron

Member
Here is a Perl program to do what you want:

Code:
[FONT=Courier New]#! perl -w

my $string1 = "[B][COLOR=Red]coffee[/COLOR][/B]";
my $string2 = "[COLOR=Red][B]tea[/B][/COLOR]";
my $indir   = '[COLOR=Red][B]c:\Documents and Settings\ron\indir[/B][/COLOR]';
my $outdir  = '[COLOR=Red][B]c:\Documents and Settings\ron\outdir[/B][/COLOR]';

opendir(DH, $indir) or die "Cannot open input directory: $indir : $!";
my @allfiles = readdir DH; # Scoop all file names into list @allfiles
closedir DH;

foreach my $thisfile (@allfiles) {
  next if $thisfile =~ /^\./;         # Ignore ".*" files
  next unless -f "$indir\\$thisfile"; # Ignore unless regular file
  open INFILE,  "< $indir\\$thisfile"  or die "Cannot open input file: $!";
  open OUTFILE, "> $outdir\\$thisfile" or die "Cannot open output file: $!";
  my $count = 0;
  while (<INFILE>) {
    if (/$string1/) {
      s/$string1/$string2/;
      $count++
    }
    print OUTFILE "$_";
  }
  print("Number of records updated: $count   in file $thisfile\n");
  close INFILE  or die "Cannot close input file: $!";
  close OUTFILE or die "Cannot close output file: $!";
}[/FONT]
First, of course, you have to download the Perl environment. Then:

  • Put the above code in a file in your "Documents and Settings\<user>" directory as (say) mod_all_files.txt
  • Edit the code with Notepad and change the parts coloured red to suit you.
  • $string1 is the existing text you want to change.
  • $string2 is what you want the text to become.
  • $indir is the existing directory with your source files you want to modify.
  • $outdir is a new, empty directory where you want the modified files to go.
When you have Perl install and changed the program as above, open a command window and execute the program like this:

Code:
[FONT=Courier New][B]Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\ron>[COLOR=Blue]perl -w mod_all_files.txt[/COLOR]
Number of records updated: 8   in file grog.txt
Number of records updated: 2   in file infile.txt
Number of records updated: 0   in file kill.txt[/B][/FONT]
OF COURSE, you should test this first with a few files!

When you have successfully run the program you should inspect the modified files carefully to make quite sure that they have been changed exactly as you want. When you are satisfied, move the changed files back to the original directory. Windows should keep the original date of creation for each file - but change all the dates last changed.

Cheers!
Ron.
 

rusguy

Member
no tom , we have two database's, now i want to move to one database and delete other, i want to find the old database(deleted) and replace with new database.
Can't you just change the logical name of a new db at a client startup to be the same as the old database name and run the current source code without all these modifications?
 

tamhas

ProgressTalk.com Sponsor
yes tamhas, ur correct, this variable can also be in the given field name ... etc but iam checking with lookup. that has solved my problem.

Check carefully ... or you will have a real mess. Take lots of backups and make lots of checks.

You haven't explained why you care about the date stamp.

What platform is this on?
 

ron

Member
Hello Shireesh.

You said you also wanted to append a comment on each source statement that was modified. I overlooked that. The new source, below, will do that too. Just set the comment string you want - and the preferred offset (how far along to the right you want the comment). If you want the comment to "float" (always just to the right of the last character on the line) then set the offset to 1.

NOW - please note carefully that THIS change (adding the comment) makes it 100% mandatory that you do not update the files in situ. You really have to output to a different directory - and carefully check before you copy the files back to the original directory. I say this because source lines can be complex in many ways. You may end-up with a comment put into an inappropriate place.

Code:
[FONT=Courier New]#! perl -w

my $string1 = "[B][COLOR=Red]coffee[/COLOR][/B]";
my $string2 = "[B][COLOR=Red]tea[/COLOR][/B]";
my $indir   = '[COLOR=Red][B]c:\Documents and Settings\ron\indir[/B][/COLOR]';
my $outdir  = '[COLOR=Red][B]c:\Documents and Settings\ron\outdir[/B][/COLOR]';
my $coment  = '[COLOR=Red][B]this is your comment[/B][/COLOR]';
my $offset  = [COLOR=Red][B]40[/B][/COLOR];

opendir(DH, $indir) or die "Cannot open input directory: $indir : $!";
my @allfiles = readdir DH; # Scoop all file names into list @allfiles
closedir DH;

foreach my $thisfile (@allfiles) {
  next if $thisfile =~ /^\./;         # Ignore ".*" files
  next unless -f "$indir\\$thisfile"; # Ignore unless regular file
  open INFILE,  "< $indir\\$thisfile"  or die "Cannot open input file: $!";
  open OUTFILE, "> $outdir\\$thisfile" or die "Cannot open output file: $!";
  my $count = 0;
  while (<INFILE>) {
    chomp;
    my $thisoffset;
    if (/$string1/) {
      s/$string1/$string2/;
      $count++;
      if (length() > $offset-1) {
        $thisoffset = length() + 2;
      } else {
        $thisoffset = $offset;
      }
      $_ .= substr(" " x 80, 0, $thisoffset - length) . "/* $coment */";
    }
    print OUTFILE "$_\n";
  }
  print("Number of records updated: $count   in file $thisfile\n");
  close INFILE  or die "Cannot close input file: $!";
  close OUTFILE or die "Cannot close output file: $!";
}[/FONT]
I guess - like others - I'm curious as to why you want to preserve the date of file creation.

Ron.
 

shireeshn

Member
Yes Tamhas , I will do good testing, then i will take a log of each line to a file, so that i can check manually wht got modified also and i will take back up of all the files. Thanks for consern.


Ron, thanks for support me.


Thanks for all ur support. really good to see nice positive response from progress talk members.


Mainly active partisipents like **** TomBascom, Tamhas, Ron... etc *****.
 

shireeshn

Member
I have tested every thing is working fine.
Can some one help me in this, i have little problem given code

Different string legths or adding string at the last :
====================================
i have tried a lot to add the strings of different lengths, when iam adding this the next line value is getting eatten up. so when read next line iam getting value of eatten up line. we can do same thing by take up a temp file first and load copy whole thing to temp file and then writteing to originall file and reading from temp file.

can any one know reading from same file and writting to same file with different strings lengths or adding string at the last ?????????????????
 
Top