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.
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)...
This is very easy. Ever heard of adding >NUL to the end of
? well, >NUL creates a file named NUL. simply type
this also works with creating a batch file. "()" are not needed. to add more lines, simply type it like this: "()" are needed in this case.
what it will look like:
now for batch files:
this will make it look like this when editing:
and will run like this:
When setting variables, keep in mind that if, say you type
to go in the new file, it will take off the %% and make it just "password". to fix this, instead type:
and it will show up properly. if you set the variable outside of building the new batch file, say
and then typed
INSIDE the new batch file, it would set the password in one file, and then it would still equal it in the next, because it set it before it made the new file. If this isn't clear enough, contact me, and ill send you a few examples of apps I've made in the past using this.