MaskedPlant MaskedPlant - 1 month ago 6x
Ini Question

Search and Edit a file with a Batch file

I would like to be able to edit a txt file with a batch file. This is a bit complicated by a number of things.

First, the file name is and opens just fine in notepad but on the computers this will be run on; the file type of .properties is not associated with Notepad. If the file needs to it can make the association but I'd like to avoid that.

Second, the text that needs edited is a single line in the file. The first 9 characters on the line we want to edit are unique to that line and will remain the same after the edit. However, where that line is located in the file and what comes after it on that line will vary from machine to machine. The line can be moved in the file no problem if it needs to be removed and added into the end.

So far I found the code listed below found here and edited it slightly. Right now when I run the batch file (named replace.cmd or replace.bat) it just echoes "this file does not exist" and exits the prompt. I have verified that the file is in the location.

In the future I would like to be able to recycle this code to easily use it to edit any .ini or txt file by just changing file location and text to find and edit. The type of file that this can be run from needs to be .bat or .cmd because of the environment I will be using this in.

Thank you.

@echo off
setlocal enabledelayedexpansion

if not exist "%1" (echo this file does not exist...)&goto :Failed

for /f "tokens=* delims=" %%a in (%1) do (

set write=%%a
if "%%a"=="%2" set write=%3

echo !write!
(echo !write!)>>%~n1.replaced%~x1

replace "C:\Users\ME\Desktop\test.txt" "withquot=*" "withquot=it worked"


UPDATE 09/20/2011

Right now the file will echo "this file does not exist" and pause. So I removed the line
if not exist "%1" (echo this file does not exist...)&goto :Failed

and I get

invalid Switch - "withquot=it worked"

I have tried removing the "=" from the invalid switch and I get the same
invalid switch
I have tried using
to set the individual variables and changed the references in the file. I have tried moving the test file to the root of C and changing the reference.

The test file currently looks like this

test file

Nothing I seem to do will get this file to work, I must be missing something small or am going in the complete wrong direction with this file. I have tested the file on Vista and Windows 7. Is something wrong with the file that you can see?

Bottom line, I don't care how but I want to be able to edit a .txt file with a batch file, and be able to do a line replace.


I still really want to know how to do this in a batch file but I found a way using a vbs script from here. using a .cmd file or a .bat file I kept running into the issue of having a = in the find or replace messing everything up.

I changed the file from that location to Loop on Arguments 2 to X so that the format for the file is Find (0) Replace with (1) in location (2) and (3) and (4)...

Option Explicit

Const ForReading = 1
Const ForWriting = 2
Dim Puppet, strOld, strNew, i
Puppet = 0
strOld = WScript.Arguments.item(0)
strNew = WScript.Arguments.item(1)
i = 1

On Error Resume Next
Do While i <= WScript.Arguments.count
    i = i + 1
    Call update(WScript.Arguments.item(i), strOld, strNew)
    Puppet = Puppet Or (Err.Number = 0): Err.Clear
On Error GoTo 0

Sub update(strFile, strOld, strNew)
Dim objFSO, objFile, strText, strNewText
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, ForReading)
strText = objFile.ReadAll
strNewText = Replace(strText, strOld, strNew)
Set objFile = objFSO.OpenTextFile(strFile, ForWriting)
objFile.WriteLine strNewText
End Sub