You could use the DocStrip program to do this. That would still be an entirely TeXnical solution!
Split up your document as you've used to, however, instead of using \input use DocStrip to yank together all the files in the correct order. (Of course, you can still use \input to source out text that occurs at multiple places.)
DocStrip allows you to selectively include/exclude portions of code. For your listings, you could write
A common operating system is basically coded like this:
%<*listings>
\begin{lstlisting}[language={[Sharp]C}]
try
{
do_weired_stuff();
}
catch (Exception e)
{
show_blue_screen();
}
\end{lstlisting}
%</listings>
Now, if you tell DocStrip to \generate{\file{doc.tex}{\from{doc.dtx{listings}}} it will write the complete code as shown above to doc.tex (assuming the sample is in the file doc.dtx of course). If the listings is omitted, everything between <*listings> and </listings> will be skipped. This way you can make DocStrip yank together two documents and then \lstinputlisting the latter at the end of the former.
I have made a simple demonstration basically featuring the structure of your post. I don't seem to be able to upload it here but you can get it from here.
Unfortunately, I couldn't find a solution for the cookies yet. Perhaps if someone else has an idea: please post it!
subsection_a.texthere is a (say)lstlistingsenvironment that you want to exclude from the inclusion? Since it's close to Christmas, do you want cookies with that too?:)Okay, seriously, how about just attaching the source code to your output PDF rather than including it in a typeset form? – Werner Dec 12 '11 at 03:26