1.

Solve : Replace a specific string all files in a folder?

Answer»

I have the following requirement.I need to replace a specific string in all the files with the servername specified in the input parameter of the batch file.
I have around 20 files and have to run the batch in various servers.

Requirement:
For each file in the folder
Find String
Replace with the original server name

Please help. Quote

in all the files with the servername specified in the input parameter of the batch file

I'm guessing that by input parameter you're referring to a command line argument.

CODE: [Select]echo off
setlocal enabledelayedexpansion
for /f %%x in ('dir /a:-d /s /b') do (
  for /f "tokens=* delims=" %%i in (%%x) do (
    SET input=%%i
    set input=!input:^<servername^>=%1!
    echo !input! >> "%%x.chg"
  )
)

The CHANGED files have .chg appended to the original file name. Batch file is designed to run in the same directory where the files are LOCATED. This can be changed, you never gave any specifics.

Pass original server name as a command line argument. If the original server name contains brackets, the code needs to be adjusted. Again you provided no specifics.

Good luck.


Discussion

No Comment Found