1.

Solve : find and replace with batch file?

Answer»

I am working ona large web project (34,000 directories with 5 files in each directory) Each directory has a number for its name (a zipcode)

I need to open each file and replace a text string (zipcode) with the name of the directory.

If need be, i can do a seperate batch file for each directory. Any suggestions?
Try with this code:

Code: [SELECT]@echo off

REM
REM GLOBALS
REM

setlocal enabledelayedexpansion
set pathToDirs=Z:\files
set tmpfile=%TMP%\abc.123.aux
if exist %tmpfile% DEL %tmpfile%

REM
REM LOOP
REM

for /f %%d in ('dir %pathToDirs% /b') do (
echo in process %%d
for /f %%f in ('dir %pathToDirs%\%%d /b') do call :REPLACE_TEXT %pathToDirs%\%%d\%%f zipcode %%d
)
goto :eof

REM
REM Subroutine to replace a string for another in a file:
REM
REM SINTAX:
REM REPLACE_TEXT file oldString newString
REM

:REPLACE_TEXT
for /f "delims=" %%x in ('type %1') do (
set tmp=%%x
echo !tmp:%2=%3! >> %tmpfile%
)
move /y %tmpfile% %1 > NUL

You need to change the lines:
set pathToDirs=Z:\files
It must be point to the directory where you have the 34.000 directories

and
call :REPLACE_TEXT %pathToDirs%\%%d\%%f zipcode %%d
I ASSUME that all the files have the word "zipcode" and that you need to change this word with the directory name (e.g. 28123).

Good luck

Wow,

the code looks great. I will try it this afternoon.

I GREATLY appreciate your assistance!

Carl



Discussion

No Comment Found