【问题标题】:Making a for loop read 2 lines of a text file at a time使 for 循环一次读取 2 行文本文件
【发布时间】: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


【解决方案1】:

我不明白您的代码如何使用您的规范。例如,您的“将路径放入变量中的 for 循环”将所有路径 分配给同一个变量,因此当 for 结束时,该变量具有 最后一个值。另外,我不明白“AppName”字符串文字在第二个中用于findstr 时是从哪里获取的。最后,如果有一个文件在 input.txt 文件中没有对应的行怎么办?

另一种方法是将 input.txt 文件处理为一系列奇数/偶数行,然后处理以奇数行描述的每个文件;这可能会导致更简单的解决方案:

@echo off
setlocal EnableDelayedExpansion

rem Process odd and even lines from input.txt file:
set "FilePath="
for /F "delims=" %%a in (input.txt) do (
   if not defined FilePath (  rem Odd line...
      rem Assign each odd line to "FilePath" variable
      set "FilePath=%%a"
   ) else (  rem Even line...
      rem Process this file, if it exists
      if exist "!FilePath!" (
         rem Block to enclose output to new file
         > newfile (
            rem The first line in new file is this even line in input.txt file
            echo %%a
            rem Copy all lines from this file, excepting the first one
            more +1 "!FilePath!"
         )
         move /Y newfile "!FilePath!"
      )
      set "FilePath="
   )
)

【讨论】:

  • 好的,让我明确一下 AppName 方面。旧的 MenuSettings 文件中包含 AppName: "http://example1.com" 行。因此“搜索”变量在旧的 MenuSettings.txt 文件中查找 AppName,并将其替换为 input.txt 中的“新名称”变量的内容。
  • 好的。这意味着AppName: 字符串是常量,仅用于识别奇数行,对吗?你测试过我的代码吗?它将在 *.txt 文件的第一行中插入 whole 奇数行,因此您必须从奇数行中删除 AppName: 和引号...您是否希望我修改我的代码以便处理像现在这样奇怪的线条?
猜你喜欢
  • 2011-12-21
  • 2013-10-04
  • 2013-03-28
  • 1970-01-01
  • 1970-01-01
  • 2011-10-09
  • 1970-01-01
  • 2022-11-18
  • 1970-01-01
相关资源
最近更新 更多