使用批处理文件搜索和编辑文件 [英] Search and Edit a file with a Batch file

查看:85
本文介绍了使用批处理文件搜索和编辑文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够使用批处理文件来编辑txt文件.由于很多事情,这有点复杂.

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.

首先,文件名为frog.properties,并且在记事本中可以很好地打开,但是在计算机上可以运行; .properties的文件类型与记事本没有关联.如果文件需要它可以建立关联,但是我想避免这种情况.

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.

第二,需要编辑的文本是文件中的一行.我们要编辑的行的前9个字符对于该行是唯一的,并且在编辑后将保持不变.但是,该行在文件中的位置以及该行之后的内容将因计算机而异.如果需要删除该行并将其添加到末尾,则可以将该行移动到文件中.

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.

到目前为止,我发现下面列出的代码这里,并对其进行了稍微的编辑.现在,当我运行批处理文件(名为replace.cmd或replace.bat)时,它仅回显此文件不存在"并退出提示.我已验证该文件在该位置.

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.

将来,我希望能够通过仅更改文件位置和查找和编辑文本的方式来回收此代码,以轻松地将其用于编辑任何.ini或txt文件.可以运行该文件的类型必须是.bat或.cmd,因为我将在其中使用该环境.

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.

谢谢.

@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

更新 2011年9月20日

UPDATE 09/20/2011

现在,该文件将回显此文件不存在"并暂停.所以我删除了if not exist "%1" (echo this file does not exist...)&goto :Failed

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

我得到

invalid Switch - "withquot=it worked"
我尝试从无效开关中删除"=",并且得到了相同的invalid switch,我尝试使用set设置各个变量并更改了文件中的引用.我尝试将测试文件移至C的根目录并更改引用.

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.

测试文件当前如下所示

test file
withquot=didntwork
USING_LOCAL_SHARED_MEM=1

我似乎无能为力,无法使该文件正常工作,我肯定丢失了一些小东西,或者此文件的方向完全错误.我已经在Vista和Windows 7上测试了该文件.您看到的文件有问题吗?

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?

底线,我不在乎如何,但是我希望能够使用批处理文件编辑.txt文件,并能够进行一行替换.

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.

我将文件从该位置更改为在参数2上循环到X,以便文件格式为查找(0)",在位置(2)和(3)和(4)处替换为(1)...

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

这篇关于使用批处理文件搜索和编辑文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆