2

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.

4
  • Possible duplicate of this.
    – Rajib
    Nov 29, 2013 at 6:30
  • @Rajib, I did not understand that that question wanted the titles interspersed with the combined text. Nov 29, 2013 at 6:33
  • 1
    Ah now i see you mean filename. Sorry I misunderstood "Title of file".
    – Rajib
    Nov 29, 2013 at 8:52
  • ah, I will edit to adhere to nomenclature, but leave the term in question for search terms. Nov 29, 2013 at 17:16

2 Answers 2

5

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.

5
  • That works tremendously. I simply navigated to the folder, right clicked the frame to paste your script, and hit <RET>. I can do a find replace to switch C:\txt` to **`. Nov 29, 2013 at 6:27
  • To clarify my comment for posterity, I navigated from within PowerShell. Nov 29, 2013 at 6:37
  • 1
    @BradyTrainor, if you only want the filename, but not the path, switch the line which is $_.FullName | Add-Content $outfile to read $_.Name | Add-Content $outfile.
    – Mitch
    Nov 29, 2013 at 17:07
  • Thank you Mitch. So that I might continue to stave off learning the code, how might I add a string such as " ** "? Nov 29, 2013 at 17:39
  • 2
    You can arbitrarily format things using the -f operator. See blogs.technet.com/b/heyscriptingguy/archive/2013/03/12/… for details. To make the filename line be ** filename, you would specify ("** {0}" -f $_.Name) | Add-Content $outfile
    – Mitch
    Nov 29, 2013 at 17:46
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.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .