Search and Edit a file with a Batch file

别等时光非礼了梦想. 提交于 2019-12-24 11:35:48

问题


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.


回答1:


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
Loop
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
objFile.Close
strNewText = Replace(strText, strOld, strNew)
Set objFile = objFSO.OpenTextFile(strFile, ForWriting)
objFile.WriteLine strNewText
objFile.Close
End Sub



回答2:


This is very easy. Ever heard of adding >NUL to the end of

pause

? well, >NUL creates a file named NUL. simply type

echo (text) >C:\Users\%userprofile%\Desktop\myfile.txt

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.

(echo First line.
echo Second line.
echo Third line.) >C:\Users\%userprofile%\Desktop\myfile.txt

what it will look like:

First Line.
Second Line.
Third Line.

now for batch files:

(echo @echo off
echo echo Second line.
echo pause) >C:\Users\%userprofile%\Desktop\myfile.bat

this will make it look like this when editing:

@echo off
echo Second Line.
pause

and will run like this:

Second Line.
Press any key to continue...

When setting variables, keep in mind that if, say you type

set /p "%password%=?"

to go in the new file, it will take off the %% and make it just "password". to fix this, instead type:

set /p "%%password%%=?"

and it will show up properly. if you set the variable outside of building the new batch file, say

set /p "%password%=?"

and then typed

if not %password%==? goto whatever

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.



来源:https://stackoverflow.com/questions/7423316/search-and-edit-a-file-with-a-batch-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!