Find and Replace Text in a File with CMD

Tech

This is How To find and replace text in a file via command line:

1. specify the directory of the file:
cd PATH\TO\FOLDER-NAME
2. searching and Replacing Text in a File with this code:
@echo off
echo Const ForReading = 1 > replace.vbs
echo Const ForWriting = 2 >> replace.vbs
echo. >> replace.vbs
echo. >> replace.vbs
echo strFileName = Wscript.Arguments(0) >> replace.vbs
echo strOldText = Wscript.Arguments(1) >> replace.vbs
echo strNewText = Wscript.Arguments(2) >> replace.vbs
echo. >> replace.vbs
echo. >> replace.vbs
echo Set objFSO = CreateObject("Scripting.FileSystemObject") >> replace.vbs
echo Set objFile = objFSO.OpenTextFile(strFileName, ForReading) >> replace.vbs
echo. >> replace.vbs
echo. >> replace.vbs
echo strText = objFile.ReadAll >> replace.vbs
echo objFile.Close >> replace.vbs
echo strNewText = Replace(strText, strOldText, strNewText) >> replace.vbs
echo. >> replace.vbs
echo. >> replace.vbs
echo objFile.Close >> replace.vbs
echo Set objFile = objFSO.OpenTextFile(strFileName, ForWriting) >> replace.vbs
echo objFile.Write strNewText >> replace.vbs
echo objFile.Close>> replace.vbs
cscript replace.vbs "YOURFILENAME.TXT" "THISWORD" "THATWORD"
echo.
pause

put your YOURFILENAMEand THISWORDto search for and replace with THATWORD.

Notes:
you can use %CD% as current path.
you can use %USERPROFILE% as user profile path.
Remember that for folders with spaces in the name or folders larger than 8 characters in length, it is a good idea to put quotation marks around the path as in the figure above or you may get an error.
If the file path or name has a space in it (e.g., “hi there”), you will place the file path or name in quotations (e.g. “D:\hi there”).

Sharing is caring!

DZ4Team

DZ4Team is a Web development team, We offer all types of scripts, installing, supporting and hosting. Contact Us: https://fb.com/DZ4TeamSupport

https://dz4team.com

Leave a Reply