Search code examples
exceltextmerging-data

Trying to merge contents of several text files as a cell each in a spreadsheet


Essentially:

I have several (400) text files each with a numerical file name (ie 12345.txt).

Each text file contains some text(Long description style, plain text paragraphs, etc). I am trying to figure out if I could import all of these text files into a spreadsheet for export to CSV.

The sheet would have two columns: the filename without the extension (the 12345), and the second column would be the contents of the file (single cell per file).

Thanks.


Solution

  • assuming all these files are in the same folder and no other .txt files are in that folder you could do the following

    create 2 batch files

    the for command can only result in 1 single other command to be executed, calling a second batch file is a single command which can contain all other commands required

    test.bat

    for %%f in (*.txt) do CALL test2.bat "%%f" "results.csv"
    

    test2.bat

    set str=%1
    set resultfile=%2
    set str=%str:~1,-5%
    echo |set /p=%str%;>> %resultfile%
    type %1 >> %resultfile%
    echo ; >> %resultfile%
    

    Run the batch file

    Run the first batch file and all will be put in a csv called results.csv