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 frog.properties 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"
pause
:Failed
pause
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 set
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
withquot=didntwork
USING_LOCAL_SHARED_MEM=1
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.