17-Sep-04 (Created: 17-Sep-04) | More in 'CS-Java'

How to replace a string pattern in a directory worth of files

You need the following jars


jawk.jar
regex12.jar

Write a batch file as follows


set pattern1="abc"
set pattern2="xyz"
set tempfile=c:\temp.txt

dir /b /s c:\dir\*.jsp 
| java com.ai.jawk.CmdSubst @call gres.bat $1 %pattern1% %pattern2% %tempfile% 
> execjsp.bat

@call execjsp.bat

This is what it does

  1. List all jsp files in that directory hierarchy
  2. For each line execute "gres.bat" with 4 arguments
  3. gres.bat replaces patter1 with pattern2 in file $1 which is the first argument from the previous pipe. It uses a tempfile in the process
  4. All the executable commands are collected in "execjsp.bat"
  5. As a final step execute the command
  6. You will have an entire directory/sub-directory worth of files replaced with the pattern