3

I would like to combine a handful of text files, but with titles (EDIT: filenames). Ideally, something like

* a filename 
contents of file
... 
* another filename 
contents of file 
... 

etc... 

I am in windows (not DOS), but have access to powershell, pandoc, emacs, cygwin, or anything else you recommend. (Clearly I'm a newb trying out org-mode.)

I can easily put them all in one folder. But I would like to avoid typing the name of each file. If a bat file is recommended, I have never used one, but am willing to learn.

Hennes
  • 65,804
  • 7
  • 115
  • 169

2 Answers2

6

I am sure there is something more clever, but here is a powershell script will combine all files:

$files = (dir *.txt)
$outfile = "out.txt"

$files | %{
    $_.FullName | Add-Content $outfile
    Get-Content $_.FullName | Add-Content $outfile
}

Is it efficient? Not terribly... but it will work in a pinch.

Mitch
  • 1,215
1

Inspired by the structure of Mitch's script, I've written a version for Unix-based environments, such as GNU/Linux and OS X:

find -regex '.*\.\(docx?\|org\|rtf\|te?xt\)$' | while read file
do
    echo "* $file" >> target-file.org
    cat "$file" | pandoc -t org >> target-file.org
done

(If you don't want to install pandoc, simply remove the pipe and command, | pandoc -t org.)

This script will find all files in the current directory and its subdirectories which have file extensions as described (.docx, etc).

For example, if the list includes fileA.text and fileB.rtf in subdirectory subd/, targetfile.org will receive lines such as:

* ./subd/fileA.text
<fileA's contents converted to an org file by pandoc>
* ./subd/fileB.rtf
<fileB's contents converted to an org file by pandoc>

I think this will leave target-file.org in a pretty good state for improving from within Emacs, without the script being too complicated. (Especially if you include the pandoc step.)