【问题标题】:Merge two text line-by-line with for each text file in directory [closed]为目录中的每个文本文件逐行合并两个文本[关闭]
【发布时间】:2017-05-01 13:14:45
【问题描述】:

我学会了只合并phppowershell 中的两个文件。但我不能对文件夹中的每个文件都这样做。

我有成千上万个这样的文件。我简化内容举个例子:

“工作目录”
标题 1 (a).txt
标题 1 (b).txt
标题 2 (a).txt
标题 2 (b).txt
标题 3 (a).txt
标题 3 (b).txt
标题... (a).txt
标题... (b).txt

“Title 1 (a).txt”的内容

Apple
Lettuce
Life

“Title 1 (b).txt”的内容

a fruid
a vegetable
a movie

我想要输出文件: “标题 1.txt”

Apple is a fruid
Lettuce is a vegetable
Life is a movie

【问题讨论】:

  • 欢迎来到 StackOverflow。如果您展示您尝试过的内容以及遇到问题的地方,而不是寻找某人为您编写解决方案,那么您更有可能收到回复。如果您使用 PowerShell,一些起点将是 Get-ChildItemGroup-ObjectGet-ContentForEach-Object cmdlet。您可以使用Get-Help xxxx -Full 获取这些信息,其中xxxx 是 cmdlet 的名称
  • 谢谢@BenH。我很高兴来到这里。我真的是初学者。我只知道 cmd 中的 for 循环,我尝试了几个小时但删除了我尝试过的内容。我不能把它放在一起。
  • 如果您提供了在合并两个您也标记的语言的文件时承认学习的内容,这将有所帮助。

标签: php powershell batch-file


【解决方案1】:
@echo off
setlocal EnableDelayedExpansion

rem Process all files that ends in " (a).txt" from current folder
for %%a in ("* (a).txt") do (
   set "name=%%~Na"

   rem Read file A from redirected Stdin
   < "%%a" (

      rem Read file B via a FOR /F command
      for /F "usebackq delims=" %%b in ("!name:~0,-4! (b).txt") do (

         rem For each line in file B, read a line from file A
         set /P "lineA="

         rem Echo both input lines in same line...
         echo !lineA! %%b
      )
   rem ... into the output file
   ) > "!name~0,-4!.txt"

   rem Optional: remove both input files
   REM del "%%a" "!name:~0,-4! (b).txt"
)

这个程序没有任何错误检查,以使其更简单......

【讨论】:

    【解决方案2】:

    以下是如何在 PowerShell 中执行此操作的示例:

    #Get the all the files in the folder
    $Files = Get-ChildItem C:\ExamplePath -File
    #Use a ForEach loop to get just the titles, split on ( and trim the extra space
    $FileTitles = ForEach ($File in $Files ) {
        ($file.basename.split("("))[0].trim()
    }
    #Group the titles (Select-Object -unique would also work here)
    $GroupedTitles = ($FileTitles | Group-Object).Name
    #Loop over each group
    ForEach ($Title in $GroupedTitles) {
        #Filter down to just the two files matching the title
        $ToJoin = $Files | Where-Object {$_.basename -like "$Title*"}
        #From TheMadTechnician answer with extra space between the joined lines
        $File1 = Get-Content $ToJoin[0].Fullname
        $File2 = Get-Content $ToJoin[1].Fullname
        $File3 = @()
    
        For($a=0;$a -lt $File1.count;$a++){
            $File3+=$File1[$a].trim() + " " +$File2[$a].trim()
        }
        #Create the output path using a subexpression $() so that a variable property can be evaluated inside of double quotes
        $File3 | Out-File "$($ToJoin[0].Directory.fullname)\$Title.txt" 
    }
    

    【讨论】:

    • 亲爱的@BenH 我已经在数千个文件中对其进行了测试。结果是完美的。感谢您的耐心并帮助它改进。内联 cmets 非常好。
    【解决方案3】:

    虽然你没有表现出任何自己的努力,但我想提供纯粹的解决方案,因为手头的任务并不是那么微不足道。


    在下面的代码中,一对文件* (a).txtfor /F loop 读取,* (b).txtinput redirection &lt; 读取:

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    
    for %%F in (".\* (a).txt") do (
        set "FLOC=%%~dpF"
        set "FEXT=%%~xF"
        set "NAME=%%~nF"
        set "FILE1=%%~F"
        setlocal EnableDelayedExpansion
        set "NAME=!NAME:~,-4!"
        set "FILE2=!FLOC!!NAME! (b)!FEXT!"
        if exist "!FILE2!" (
            if not exist "!FLOC!!NAME!!FEXT!" (
                > "!FLOC!!NAME!!FEXT!" (
                    for /F "delims= eol=|" %%E in ("!FILE2!") do (
                        for /F usebackq^ delims^=^ eol^= %%L in ("!FILE1!") do (
                            endlocal
                            set "LINE1=%%L"
                            set "LINE2=" & < "%%E" set /P LINE2=""
                            setlocal EnableDelayedExpansion
                            echo(!LINE1! is !LINE2!
                        )
                    )
                )
            ) else (
                >&2 echo ERROR: File "!FLOC!!NAME!!FEXT!" already exists^^!
            )
        ) else (
            >&2 echo ERROR: File "!FILE2!" cannot be found^^!
        )
        endlocal
    )
    
    endlocal
    exit /B
    

    在以下代码中,input redirection &lt; 读取一对文件 * (a).txt* (b).txt

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    
    for %%F in (".\* (a).txt") do (
        set "FLOC=%%~dpF"
        set "FEXT=%%~xF"
        set "NAME=%%~nF"
        set "FILE1=%%~F"
        setlocal EnableDelayedExpansion
        set "NAME=!NAME:~,-4!"
        set "FILE2=!FLOC!!NAME! (b)!FEXT!"
        if exist "!FILE2!" (
            if not exist "!FLOC!!NAME!!FEXT!" (
                for /F %%N in ('^< "!FILE1!" find /C /V ""') do set "NUM=%%N"
                9< "!FILE1!" 8< "!FILE2!" > "!FLOC!!NAME!!FEXT!" (
                    for /L %%I in (1,1,!NUM!) do (
                        set "LINE1=" & <&9 set /P LINE1=""
                        set "LINE2=" & <&8 set /P LINE2=""
                        echo(!LINE1! is !LINE2!
                    )
                )
            ) else (
                >&2 echo ERROR: File "!FLOC!!NAME!!FEXT!" already exists^^!
            )
        ) else (
            >&2 echo ERROR: File "!FILE2!" cannot be found^^!
        )
        endlocal
    )
    
    endlocal
    exit /B
    

    【讨论】:

    • 哇。感谢您提供批处理文件解决方案。每个人也应该看到这一点。它甚至可能更广泛。实际上,文件名更复杂,不像我的简化示例,即它们不是定期排序的 (a) 和 (b)。我会尽量改进你的代码。如果我匹配列表中的奇数和偶数文件,那就太好了。
    • 不客气!因此,您应该已经发布了文件的实际命名方式。无论如何,你可以尝试一下,如果卡住了,你可以问另一个问题,因为你展示了你的尝试并提供了真实的示例数据......
    猜你喜欢
    • 2013-10-03
    • 2017-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-05
    • 2017-01-29
    • 2018-02-25
    • 1970-01-01
    相关资源
    最近更新 更多