Newsgroups: vmsnet.tpu
Subject: Replacing strings in multiple files..
From: "R.W.Bell" <bob_bell@uh.edu>
Date: 20 Nov 1992 14:11:13 -0600 (CST)
Reply-To: "R.W.Bell" <Bob_Bell@UH.EDU>
Organization: The Internet
Return-Path: <TPU-Mgr@SHSU.edu>
Errors-To: TPU-Mgr@SHSU.edu
X-Listname: Text Processing Utility (TPU) Language Discussion List
X-Vms-To: UHOU::IN%"info-tpu@odin.shsu.edu"
X-Vms-Cc: SINARRWB
Mime-Version: 1.0
Content-Transfer-Encoding: 7BIT


Someone wanted to replace a string in multiple file at DCL ??
Ken Selvia wrote this, works just dandy !!!

Enjoy..Bob Bell

---------------------------------

$EDIT/TPU/NOSECTION/COMMAND=SYS$INPUT/NODISPLAY
!
! This program is put into the public domain.
! You may do anything you want with it.
!
! Send comments or suggestions to UCS_KAS@SHSUODIN  (BITNET)
! Ken Selvia
! 5-APR-1988
!
PROCEDURE tpu$init_procedure
LOCAL next_file, search_string, replace_string,
      lower_string, total_files, total_strings, changed,
      search_size, case_match;
SET(FACILITY_NAME, "REPLACE");
SET(SUCCESS,OFF);
IF GET_INFO (COMMAND_LINE,"FILE_NAME") = "" THEN
    MESSAGE(" ");
    MESSAGE(" REPLACE - Multiple file search and replace utility.");
    MESSAGE(" ");
    MESSAGE(" Usage:    REPLACE file.name  (wildcards allowed)");
    MESSAGE("           SEARCH STRING");
    MESSAGE("           REPLACEMENT STRING");
    MESSAGE(" ");
    QUIT;
ENDIF;
MESSAGE ("Enter search and replace strings.");
MESSAGE ("The search is not case sensitive if search string is lower case.");
search_string := READ_LINE ("Enter search string: ");
replace_string := READ_LINE ("Enter replacement string: ");
IF LENGTH (search_string) = 0 THEN
        MESSAGE ("No search string given. Aborting...");
        QUIT;
ENDIF;
total_files := 0;
total_strings := 0;
lower_string := search_string;
CHANGE_CASE (lower_string,LOWER);
case_match := (lower_string = search_string);
search_size := LENGTH (search_string);
work_buffer := CREATE_BUFFER ("work");
POSITION (work_buffer);
LOOP
    ERASE (work_buffer);
    next_file := FILE_SEARCH (GET_INFO (COMMAND_LINE,"FILE_NAME"));
    EXITIF next_file = "";
    READ_FILE (next_file);
    POSITION (BEGINNING_OF (work_buffer));
    SET (OUTPUT_FILE,CURRENT_BUFFER,FILE_PARSE (next_file));
    changed := substitute(search_string,
                          replace_string,
                          search_size,
                          case_match);
    IF changed > 0 THEN
        total_files := total_files + 1;
        total_strings := total_strings + changed;
        WRITE_FILE (work_buffer);
    ENDIF;
    MESSAGE (FAO ("Replaced !ZL string!%S in file !AS",changed,next_file));
ENDLOOP;
SET(NO_WRITE, work_buffer);
MESSAGE (FAO ("Total files modified: !ZL", total_files));
MESSAGE (FAO ("Total strings replaced: !ZL", total_strings));
QUIT;
ENDPROCEDURE

PROCEDURE substitute(target,replacement,size,ignore_case)
local modified_count;
ON_ERROR
    RETURN (modified_count);
ENDON_ERROR
modified_count := 0;
LOOP
    IF ignore_case THEN
        POSITION (SEARCH (target ,FORWARD,NO_EXACT));
    ELSE
        POSITION (SEARCH (target,FORWARD,EXACT));
    ENDIF;
    ERASE_CHARACTER (size);
    COPY_TEXT (replacement);
    modified_count := modified_count + 1;
ENDLOOP;
ENDPROCEDURE
!    This line is only executed when compiling the TPU program.
     SAVE ("sys$disk:replace");
     QUIT;
$REPLACE :== "EDIT/TPU/SECT=''F$ENVI(""DEFAULT"")'REPLACE/NODISP/NOCOMM"
$EXIT
Newsgroups: vmsnet.tpu