0

My aim is to search for a string contained within multiple .config files that sits inside a directory.

The folder structure is like this: C:\Parent_Folder\child_folder\

The child folder contains many different folders containing a .config file within each folder.

I am trying to create a .bat file that will allow me to search all of these folders and then replace a certain string with a new string.

I have found a way to search all files and print this to a document showing me where the changes are requires using:

findstr /i /s "DATABASE1" *.config > c:\temp\Changes.txt

However when it comes to creating a batch file to search the directory and replace all these files with a new string I am coming across the file path does not exist.

I have had a look at the following similar issues: Batch script to replace specific string in multiple files

Find and replace string in multiple files within a folder using windows Batch script

The code I am currently using is:

@echo off
setlocal enableextensions disabledelayedexpansion

set "dir1=C:\Parent_Folder\child_folder\"
set "search=database1"
set "replace=db2"

for /f "delims=" %%i in ("%dir1%\*.config") do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    >>"%configFile%" echo(!line!
    endlocal
)

@pause

The error I keep coming across is that the specified path can not be found, I thought it may be due to environment variables so I added the path in there and displayed the echo %PATH% in the .bat file and the path does exist however I keep getting the error stating it does not.

So I am not sure if it is actually something else here causing the issue.

Any advice would be great.

1 Answers1

0

You probably want [SS64]: FOR /R.

Small example:

script00.bat:

@echo off

setlocal enableextensions disabledelayedexpansion

set _ROOT_DIR=".\dir0"

for /R %_ROOT_DIR% %%f in (*.config) do (
    echo file: %%f
    rem Do the rest
)

Output:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q068684288]> tree /f /a
Folder PATH listing for volume SSD0-WORK
Volume serial number is AE9E-72AC
E:.
|   script00.bat
|
\---dir0
    +---dir0
    |       file.config
    |
    +---dir1
    |       file.config
    |
    \---dir2
            file.config


[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q068684288]> script00.bat
file: e:\Work\Dev\StackOverflow\q068684288\dir0\dir0\file.config
file: e:\Work\Dev\StackOverflow\q068684288\dir0\dir1\file.config
file: e:\Work\Dev\StackOverflow\q068684288\dir0\dir2\file.config
CristiFati
  • 38,250
  • 9
  • 50
  • 87