【发布时间】:2016-02-25 17:26:45
【问题描述】:
我有一组文件夹,每个文件夹都有一个同名的 txt 文件。他们的路径是
C:\Test\Salford_xxx\MenuSettings.txt
C:\Test\Salford_xxx\MenuSettings.txt
C:\Test\Salford_xxx\MenuSettings.txt
其中 xxx 是一个随机的 3 位数字。我希望使用名为 input.txt 的文本文件更改每个文件的第一行,该文本文件具有替换每个文件第一行的路径和行。看起来像这样。
C:\TEST\SALFORD_001\MENUSETTINGS.TXT
AppName: "This needs replacing 1"
C:\TEST\SALFORD_011\MENUSETTINGS.TXT
AppName: "This needs replacing 2"
C:\TEST\SALFORD_345\MENUSETTINGS.TXT
AppName: "This needs replacing 3"
C:\TEST\SALFORD_761\MENUSETTINGS.TXT
AppName: "This needs replacing 4"
C:\TEST\SALFORD_768\MENUSETTINGS.TXT
AppName: "This needs replacing 5"
C:\TEST\SALFORD_999\MENUSETTINGS.TXT
AppName: "This needs replacing 6"
我编写了一个 for 循环,将路径和替换放入变量中,它有效:
for /F "delims=" %%a in ('findstr /R /I "Salford" Input.txt') do (set "FilePath=%%a" echo %FilePath%)
for /F "delims=" %%b in ('findstr /R /I "AppName" Input.txt') do (set "NewName=%%b" echo %NewName%)
AppName 是始终位于第一行的单词,因此用于搜索。 这是用于替换每个文件的行的脚本。
set "search=Appname"
set "replace=%NewName%"
set "newfile=NewOutput.txt"
(for /f "delims=" %%i in (%FilePath%) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%search%=%replace%!"
echo(!line!
endlocal
))>"%newfile%"
move "%newfile%" "%FilePath%"
但是,循环继续到 Salford_999 文件夹中的最后一项,并仅编辑该文件。我怎样才能让它读取 input.txt 的前两行,进行替换,然后循环到接下来的两行等等?
谢谢
【问题讨论】:
-
我不确定您的问题是如何读取两行 input.txt。我没有看到任何代码可以确保您只是尝试编辑 MENUSETTINGS.txt。这似乎是你的问题。
-
input.txt 文件中包含 MenuSettings.txt 引用,确定就这么多吗?它是每个文件夹中唯一的文本文件。
标签: regex variables batch-file for-loop replace