This is requirement is similar to How to search and replace a string in multiple text files (within a directory) with Windows CMD? Part 2
I want to perform Find and Replace on all HTML files inside a folder and it's sub-folders.
Here I am taking 2 inputs from 1 of which is file path & another is folder name in that file path for some reason.
set /p INPUT1= Enter Source file path:
set /p INPUT2= Enter Source folder name (**Without any Space**):
Here let's assume (below folders can change):
INPUT1= D:\!Test\
INPUT2= Folder1
Then path is D:\!Test\Folder1\
Inside Folder1 there are some more folders like:
....Folder1\A\Common\
....Folder1\B\Common\
....Folder1\C\Common\
In above folder **A** as well as folder **Common** have html files.
I have coded this in .cmd file as below:
set /p INPUT1= Enter Source file path:
set /p INPUT2= Enter Source folder name (**Without any Space**):
REM Replace text FindTextX.html with ReplaceTextA.html in folder A, ReplaceTextB.html in folder B, ReplaceTextC.html in folder C.
Set "OldStringA=FindTextX.html"
Set "NewStringA=ReplaceTextA.html"
Set "NewStringB=ReplaceTextB.html"
Set "NewStringC=ReplaceTextC.html"
@ECHO OFF &SETLOCAL
cd %INPUT1%\%INPUT2%\A\
for %%x in (*.html) do call:process "%%~x"
goto:eof
:process
set "outFile=%~n1%~x1"
(for /f "skip=2 delims=" %%a in ('find /n /v "" "%~1"') do (
set "ln=%%a"
Setlocal enableDelayedExpansion
set "ln=!ln:*]=!"
if defined ln (
set "ln=!ln:%OldStringA%=%NewStringA%!"
)
echo(!ln!
endlocal
))>"%outFile%"
REM exit /b
REM Come out of A folder
cd..
REM Go inside of B folder
cd B
for %%x in (*.html) do call:process1 "%%~x"
goto:eof
:process1
set "outFile=%~n1%~x1"
(for /f "skip=2 delims=" %%a in ('find /n /v "" "%~1"') do (
set "ln=%%a"
Setlocal enableDelayedExpansion
set "ln=!ln:*]=!"
if defined ln (
set "ln=!ln:%OldStringB%=%NewStringB%!"
)
echo(!ln!
endlocal
))>"%outFile%"
.......So on for folder **C** .....
exit /b
However this does not work. I get empty html file inside folder A, B & C. However it does not work on Sub-folder Common which is inside each A, B and C folders. I want this script to replace text in HTML files under folder A, B and C as well as text inside their sub-folders. Note: I do not want any additional BAT files like FART, REPLACER, .... etc. Your help is appreciated.
bash? Or is Cygwin available? Things will be easier if so. – iBug Feb 02 '17 at 09:54I need this for Windows machine."... I say: "It's a good thing that PowerShell is Windows native, much more robust than native Windows commands for many tasks, works just fine from CMD script natively, and is much simpler to code (less code) for certain tasks than a slew of native Windows commands within a ton of FOR loops"... You could probably tackle this with a much simpler PowerShell script but if you must be stuck in the box of CMD and native Windows commands, welcome to CMD and get to troubleshooting your logic. You can easily run PowerShell within CMD too by the way. – Vomit IT - Chunky Mess Style Feb 03 '17 at 05:56