Find and Replace from Multiple Files to New Names

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
fa6ab
Posts: 1
Joined: 27 Apr 2011 08:08

Find and Replace from Multiple Files to New Names

#1 Post by fa6ab » 27 Apr 2011 08:18

Using your great forum I have managed to to start amending some HTML files to amend a hard coded link using the following batch job from your site.

@echo off
REM -- Prepare the Command Processor --
SETLOCAL ENABLEEXTENSIONS
SETLOCAL DISABLEDELAYEDEXPANSION

::BatchSubstitude - parses a File line by line and replaces a substring"
::syntax: BatchSubstitude.bat OldStr NewStr File
:: OldStr [in] - string to be replaced
:: NewStr [in] - string to replace with
:: File [in] - file to be parsed
:$changed 20100115
:$source http://www.dostips.com
if "%~1"=="" findstr "^::" "%~f0"&GOTO:EOF
for /f "tokens=1,* delims=]" %%A in ('"type %3|find /n /v """') do (
set "line=%%B"
if defined line (
call set "line=echo.%%line:%~1=%~2%%"
for /f "delims=" %%X in ('"echo."%%line%%""') do %%~X
) ELSE echo.
)


However when I type the following at the cmd prompt I can only do this on one file at a time.
call andy.bat "\agent\" "\" andy*.htm> andy1.htm

this works and places all instances of andy .htm into andy1.htm with the search and replace completed. However I need each file to be saved with the same name but in a different folder or with a slightly different name ie andy1x.htm andy2x.htm etc. so that I have all my files amended and saved as new files.

I am new to batch code so need some help and I am aware there are many programs that can do this search and replace for me but due to restrictions at work I cannot install new software so need to try and do it in a .bat job.

If I am not clear I have say 2k files with a path andy\agent\web* and need to change it to andy\web* in each and every file. Ideally saving with the same name in a new folder each file or in the same folder with a slightly different name.

Due to my work system I cannot use perl.

Thanks in advance.

Post Reply