问题描述
我需要使用,我们可以修改文件的内容,以创建一个批处理文件。
I need to create a batch file using which we can modify the content of a file.
例如
Testing.txt文件包含一行
说明= MAN_Human
改成
说明= MAN_Human_V2
Testing.txt file contains a lineDescription=MAN_Humanchange toDescription=MAN_Human_V2
仅在城域网的末尾添加(_V2)
only add (_V2) at the end of the MAN
所以,当我们打开这个文件,我们必须说明的名字,而不是MAN_Human_V2只MAN
So when we open the file we have description name MAN_Human_V2 instead MAN only.
我有多个文件,去努力。请帮助。
i have multiple files to work on. Please help.
推荐答案
您可以使用为
遍历文件的每一行动做,当你找到符合'说明',你追加'_V2到输出。
You can do it by using for
loop to move through each line of the file, and when you find the line with the 'Description', you append '_V2' to the output.
@echo off
for %%f in (*.txt) do (
for /f "tokens=1* delims=:" %%g in ('type "%%f" ^| findstr /n /v "BoGuSsTrInG"') do (
if "%%hx"=="x" (
echo.>>"%%~nf.newtxt"
) else (
for /f "tokens=1* delims==" %%i in ('echo.%%h') do (
if "%%i"=="Description" (
echo.%%i=%%j_V2>>"%%~nf.newtxt"
) else (
if "%%jx"=="x" (
echo.%%i>>"%%~nf.newtxt"
) else (
echo.%%i=%%j>>"%%~nf.newtxt"
)
)
)
)
)
)
ren *.txt *.oldtxt
ren *.newtxt *.txt
当然,你,而你处理的test.txt
文件要输出内容到一个新文件。然后你用新创建的替换原来的文件...
Of course, you have to output the content to a new file while you process the test.txt
file. Then you replace the original file with the newly created one...
您必须将TXT文件的内容复制到新文件重命名其中的任何回原来的名称之前完全。这是因为为
循环将再次接他们时,他们被修改后,如果立即将其重命名,其中一些将获得 _V2
追加了好几次。
You have to copy the contents of the TXT files to new files completely before you rename any of them back to their original names. This is because the for
loop will pick them up again after they are modified if you immediately rename them and some of them will get the _V2
appended several times.
我没有在最后删除 .oldtxt
文件,因为你永远不应该破坏你的原始文件,直到你知道这个过程的工作!
I did not remove the .oldtxt
files at the end, because you should never destroy your original files until you know that the process worked!
有一个解决方法,我发现这种意志preserve空行。解决方法管道的文本文件,以 FINDSTR
的内容,寻找不包含 BoGuSsTrInG
所有行。输出包括行号(得到它打印的空行的唯一途径)。行号有一个尾随冒号(:
),因此我们可以将输出到不同的变量拆分对结肠的内容。在标记= 1 *
的行号分成 %%摹
变量,该行的结果是在 %% ^ h
变量。我们可以测试 %% ^ h
,看它是否是空的,一个空行添加到新的文件。否则,我们处理 %% ^ h
和以前一样。
There is one workaround that I found that will preserve the blank lines. The workaround pipes the content of the text file to findstr
, searching for all lines that don't contain BoGuSsTrInG
. The output includes the line numbers (the only way to get it to print the blank lines). The line numbers have a trailing colon (:
), so we can divide the output into different variables splitting the content on the colon. The tokens=1*
split the line number into the %%g
variable, and the result of the line is in the %%h
variable. We can test %%h
to see if it is empty and add a blank line to the new file. Otherwise, we process %%h
as before.
这篇关于使用批处理文件修改文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!