简体   繁体   中英

How to write all *.txt files in a directory into one file with source file name in each line in output file?

The code respectively Windows command line below combines all the texts in one text file.
But I want to include text file name in each line of the text using the command line below.

for %f in (*.txt) DO type "%f" >> C:\Test\output.txt

How can this be achieved?

This batch file can be used for this task.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "OutputFile=C:\Test\output.txt"
del "%OutputFile%" 2>nul
for %%I in ("*.txt") do for /F usebackq^ delims^=^ eol^= %%A in ("%%I") do >>"%OutputFile%" echo %%~nxI;%%A
endlocal

This batch file writes into file C:\\Test\\output.txt all non-empty lines of all *.txt files found in current directory with file name and file extension, but without file path, and a semicolon at beginning of each line.

The current directory should not be C:\\Test on running the batch file above. Otherwise the batch file below with an additional IF would be needed to prevent processing the output file having the same file extension as the files to read and write with file name into each line.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "OutputFile=C:\Test\output.txt"
del "%OutputFile%" 2>nul
for %%I in ("*.txt") do if /I not "%%~fI" == "%OutputFile%" for /F usebackq^ delims^=^ eol^= %%A in ("%%I") do >>"%OutputFile%" echo %%~nxI;%%A
endlocal

The batch file below could be used for reading in also empty lines and write them into output file with file name and semicolon.

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "OutputFile=C:\Test\output.txt"
del "%OutputFile%" 2>nul
for %%I in ("*.txt") do if /I not "%%~fI" == "%OutputFile%" (
    for /F delims^=^ eol^= %%A in ('%SystemRoot%\System32\findstr.exe /N "^" "%%I" 2^>nul') do (
        set "Line=%%A"
        setlocal EnableDelayedExpansion
        >>"%OutputFile%" echo %%~nxI;!Line:*:=!
        endlocal
    )
)
endlocal

Modify echo %%~nxI;%%A to echo %%A;%%~nxI respectively echo %%~nxI;!Line:*:=! to echo !Line:*:=!;%%~nxI to have the file name after a semicolon at end of each line in output file.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • del /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • set /?
  • setlocal /?

See How to read and print contents of text file line by line? with the full explanation of the used code. It can be seen easily that Windows command processor is designed for executing commands and applications and not for processing text files.

Here's an alternative method which relies on to do the majority of the work.

At the prompt:

For /F "Tokens=1-2*Delims=:" %A In ('FindStr /N "^" *.txt 2^>Nul')Do @(Echo(%A:%C)>>"C:\Test\output.txt"

Double up the % characters if running it from a .

The code is untested, it may have issues with files whose lines begin with : characters, so may not be suitable for all files.

I expect that Select-String will do what you are seeking. Here is a .bat file script that implements the same code.

powershell -NoLogo -NoProfile -Command ^
    "$logfile = 'C:\src\t\master.txt';" ^
    "if (Test-Path -Path $logfile) { Remove-Item -Path $logfile };" ^
    "Select-String -Pattern '^'  -Path 'y.txt' -Exclude $logfile |" ^
        "ForEach-Object { '{0}:{1}' -f ((Split-Path $_.Path -Leaf), $_.Line) } |" ^
        "Out-File -FilePath 'C:\src\t\master.txt' -Encoding ascii"

Alternatively, a .ps1 script file could be created and called from a .bat script file.

=== doit.ps1

$logfile = 'C:\src\t\master.txt'
if (Test-Path -Path $logfile) { Remove-Item -Path $logfile }

Select-String -Pattern '^'  -Path 'y.txt' -Exclude $logfile |
    ForEach-Object { '{0}:{1}' -f ((Split-Path $_.Path -Leaf), $_.Line) } |
    "Out-File -FilePath 'C:\src\t\master.txt' -Encoding ascii"

=== doit.bat

powershell -NoLogo -NoProfile -File '%~dp0doit.ps1'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM