搜索文件名以在多行另一个文件变量并替换不同的变量文本 [英] Search filename with variable and replace different variable text in another file with multiple lines
问题描述
我有一个回答我的问题previously,但现在我已经意识到我需要更多的帮助。我有code,它几乎一切我希望它:
I had an answer to my question previously, but now I've realised that i need a bit more help. I have code that does almost everything I want it to:
@echo off
copy 2.txt 2.txt-backup
setlocal enableDelayedExpansion
>2.txt (
for /f "delims=" %%A in (2.txt-backup) do (
set "ln=%%A"
if "!ln:~0,6!" == "Class=" findstr /c:"ClassName=!ln:~6!" 1.txt >null && set "ln=Class=ShipDummy"
echo !ln!
)
)
请参阅本:
<一href=\"http://stackoverflow.com/questions/14992029/batch-i-would-like-to-search-with-a-variable-in-one-file-and-replace-that-vari\">Batch - 我想在一个文件中有一个变量来搜索和替换在另一个文件该变量的结果。
Please refer to this: Batch - I would like to search with a variable in one file and replace that variable result in another file
但现在我已经意识到我需要的1.txt是在其名称的变量,一系列文件的文件名,像没有确切的限制:
But now I've realized I need 1.txt to be a file name with a variable in its name, a series of files, with no exact limit like:
Log_0.txt
Log_0.txt
Log_1.txt
Log_1.txt
Log_2.txt
Log_2.txt
Log_3.txt
Log_3.txt
等等....
有可能只是Log_0.txt,或者也有可能通过Log_23.txt被Log_0.txt,或甚至更多...
There may be just Log_0.txt, or there may be Log_0.txt through Log_23.txt, or even more...
另外,我想另一行添加到CLASS = ShipDummy的置换文本为:
类ShipDummy
类型= 206
Also, I'd like to add another line to the replacement text of Class=ShipDummy to be: Class-ShipDummy Type=206
和将取代文本行只是下面类型= 206 CLASS = ShipDummy替换的文字。\\
And that would replace a line of text just below Class=ShipDummy replacement text with Type=206.\
推荐答案
这是不是你想要做什么?
Does this do what you want?
@echo off
copy 2.txt 2.txt-backup
setlocal enableDelayedExpansion
>2.txt (
for /f "tokens=1* delims=:" %%A in ('findstr /n "^" 2.txt-backup') do (
( echo !ln!| findstr "^Type=206$" >NUL && set ln=ln ) || (
set "ln=%%B"
if "!ln:~0,6!"=="Class=" (
findstr /c:"ClassName=!ln:~6!" Log_*.txt >null && (
echo Class=ShipDummy
set "ln=Type=206"
)
)
if #!ln!==# (echo;) else echo !ln!
)
)
)
这篇关于搜索文件名以在多行另一个文件变量并替换不同的变量文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!