Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

How to programmatically comment out the entire content of multiple .m source files assuming they start with correct Mathematica syntax? I need it to be robust so the content of the file can be anything as long as it's valid .m source file.

share|improve this question
3  
Why would you want to comment out a whole source file? Isn't it a lot simpler to not load it? Also, comments are nestable, so have you tried just appending and prepending (* and *)? –  Szabolcs Mar 27 '13 at 19:38
    
It's hard for all people who have access to those files to know what files they need to exclude from loading. It's easy for me to comment out the content of source files that I don't want anyone to load temporarily. Do you mean manually doing that for each file? –  Problemaniac Mar 27 '13 at 20:07
3  
you can just read the file in as a string, prepend and append the appropriate characters and save it back –  acl Mar 27 '13 at 20:17

1 Answer 1

up vote 0 down vote accepted

As the comments of Szabolcs and acl tell you: read in the file contents as text, put the comment characters before-after the text and save it again as text. Packages are stored as plain text files without any front-end dependent cache information, so this method is robust and foolproof.

First I generate a package file that prints a message when loaded:

file = FileNameJoin@{$TemporaryDirectory, "temp.m"};
Export[file, "Print[\"Inside package file!\"]", "Text"];
Get@file
Inside package file!

Now import-export with comments. Notice that the Get call does not print anymore:

Export[file, "(*" <> Import[file, "Text"] <> "*)", "Text"];
Get@file

Checking the file contents:

FilePrint@file
 (*Print["Inside package file!"]*)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.