使用cmd将多个文件串联成一个文件

时间:2018-10-19 09:59:24

标签: batch-file cmd

我有多个文件,例如file-1.txt,file-2.txt,file-3.txt ......和anotherfile-1.txt,anotherfile-2.txt,anotherfile-3 .txt ......等 我需要将每组文件合并为一个。输出应类似于file.txt(包含来自file-1.txt,file-2.txt,file-3.txt ..的文本)和anotherfile.txt(包含来自anotherfile-1.txt,anotherfile-2的文本.txt,另一个文件-3.txt)

我现在要做的只是将所有文本放入一个文件中。下面的代码

for /r C:\pdftxt\OCR\txt_tes %i in (*.txt) do type "%i" >> C:\pdftxt\OCR\folded\output.txt

非常感谢您的帮助

3 个答案:

答案 0 :(得分:1)

假定

  • 文件名中只有一个破折号
  • 后继号码是连续的1..n

:: Q:\Test\2018\10\19\SO_52890092.cmd
@Echo off&SetLocal EnableExtensions EnableDelayedExpansion

Set "Base=C:\pdftxt\OCR\txt_tes"
pushd "%Base%"

:: Get file names and largest trailing number
for /F "tokens=1-3 delims=-." %%A in (
  'Dir /B *-*.txt ^| findstr.exe /I ".*-[0-9]*.txt$" '
) do (
  if defined filename[%%~nA] (
    if !filename[%%~nA]! lss %%B  set filename[%%~nA]=%%B
  ) else set filename[%%~nA]=%%B
)
Echo found:
set filename[
Echo:
:: iterate filenames and then file numbers
For /f "tokens=2* delims=[]=" %%A in ('set filename[ 2^>Nul') do (
  Type nul > "%%A.txt"
  For /L %%L in (1,1,%%B) Do Type "%%A-%%L.txt" >> "%%A.txt"
  Rem Type "%%A.txt"
)
popd

在我的A:\驱动器上输出带有示例文件的示例文件,其中仅包含自己的文件名。
(并且在Type前面没有Rem)

> Q:\Test\2018\10\19\SO_52890092.cmd
found:
filename[anotherfile]=3
filename[file]=3

a:\anotherfile-1.txt
a:\anotherfile-2.txt
a:\anotherfile-3.txt
a:\file-1.txt
a:\file-2.txt
a:\file-3.txt

答案 1 :(得分:0)

我可能会用以下代码解决您的任务(请参见说明):

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "_SOURCE=C:\pdftxt\OCR\txt_tes"  & rem // (path to directory containing the source files)
set "_TARGET=C:\pdftxt\OCR\folded"   & rem // (path to directory containing the target files)
set "_EXT=.txt"                      & rem // (file name extension including the leading dot)
set "_SUB=#"                         & rem // (if not empty, search sub-directories of source too)
set "_TEMP=%TEMP%\%~n0_%RANDOM%.tmp" & rem // (temporary file to collect prefixes)

rem // Define option for `dir` command to regard sub-directories or not:
if defined _SUB (set "SUB=/S") else (set "SUB=")
rem // Create empty temporary file to append to later:
> "%_TEMP%" rem/
rem // Walk through directory (tree) and search for matching files:
for /F "delims= eol=|" %%F in ('dir /B %SUB% /A:-D "%_SOURCE%\*-*%_EXT%"') do (
    rem // Split file name at first hyphen (ignoring leading hyphens):
    for /F "delims=-" %%E in ("%%~nxF") do (
        rem /* Store file name part before first hyphen into temporary file,
        rem    avoiding duplicates (disregarding the case): */
        > nul findstr /X /I /C:"%%E" "%_TEMP%" || >> "%_TEMP%" echo(%%E
    )
)
rem // Change into the source (root) directory:
pushd "%_SOURCE%" && (
    rem // Walk through temporary file, which is a list of partial file names:
    for /F "usebackq delims= eol=|" %%L in ("%_TEMP%") do (
        rem // Create an empty target file to append to later:
        > nul copy /Y nul "%_TARGET%\%%L%_EXT%"
        rem /* Search for all files whose names begin with the currently
        rem    iterated partial file name plus a hyphen: */
        for /F "delims= eol=|" %%K in ('dir /B %SUB% /A:-D "%%L-*%_EXT%"') do (
            rem // Append content matching file to target file:
            > nul copy /Y "%_TARGET%\%%L%_EXT%"+"%%K" "%_TARGET%\%%L%_EXT%" /B
        )
    )
    rem // Restore previous directory:
    popd
)
rem // Clean up temporary file:
del "%_TEMP%"

endlocal
exit /B

答案 2 :(得分:0)

谢谢你们。这帮助了

var linkafterpreload = "link 1";

window.addEventListener("load", _ => {
    var newscript = document.createElement("script");
    newscript.src = linkafterpreload ;
    document.body.appendChild("newscript")
})