$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