为目录

时间:2017-05-01 13:14:46

标签: php powershell batch-file

我学会了只合并phppowershell中的2个文件。但我无法对文件夹中的每个文件执行此操作。

我有成千上万的这样的文件。我简化了内容以举例说明:

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

“标题1(a).txt”

的内容
Apple
Lettuce
Life

“标题1(b).txt”

的内容
a fruid
a vegetable
a movie

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

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

3 个答案:

答案 0 :(得分: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"
)

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

答案 1 :(得分:0)

以下是如何在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" 
}

答案 2 :(得分:0)

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

在以下代码中,for /F loop读取一对文件* (a).txtinput redirection <读取* (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!" (
            > "!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 <读取了一对* (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