# How to modify a file opened by NotebookOpen [closed]

I need to run a file several times, each time with a different value for a certain variable. I use the following script to automate the process, but I have no idea how to make changes in the file after it's opened and then evaluated. Any guidance and help is highly appreciated.

Do[
nb1 = NotebookOpen["/Users/toor/Desktop/myfile.nb"];
SelectionMove[nb1, All, Notebook] SelectionEvaluate[nb1], {i, 1, 10}];


## closed as off-topic by MarcoB, user9660, Kuba♦, m_goldberg, ÖskåJun 12 '16 at 18:45

This question appears to be off-topic. The users who voted to close gave these specific reasons:

• "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – MarcoB, Community, Öskå
• "This question cannot be answered without additional information. Questions on problems in code must describe the specific problem and include valid code to reproduce it. Any data used for programming examples should be embedded in the question or code to generate the (fake) data must be included." – Kuba, m_goldberg
If this question can be reworded to fit the rules in the help center, please edit the question.

• Perhaps the most natural way to do this would be to wrap the code present in that notebook in a function that takes those parameters that you need to change as arguments. Once your function definition is evaluated, then you can use e.g. Table, Map, or similar list manipulation constructs to pass it the various values of the parameters you need to run. Look at the Functions and Programs language overview, specifically the sections "Functions and Procedures" and "Repetitive Operations". – MarcoB Jun 8 '16 at 0:11
• Check the notebook manipulation tutorial will be helpful to you. – Wjx Jun 8 '16 at 0:47
• If have to use this method instea of @MacroB 's method, I think I can give you an answer. – Wjx Jun 8 '16 at 0:58
• I think you don't need to change the variable in the opened Notebook. If you are in the same Kernel then just add the definition of the new value of your parameter in the Do loop before evaluating the opened Notebook. – Algohi Jun 8 '16 at 5:08
• Seemingly yes~ Changing directly in the kernel will be far more direct. – Wjx Jun 8 '16 at 9:44

Just as an example, I created two files under the same folder, one named "run.nb" which is the file I'll want to evaluate for multiple times, another is "run1.nb" that controls the running process.

In "run.nb", I wrote:

A = 1;
AppendTo[B, A^2];


So what I'll change each time is the input value of A and What I would love to collecto is the value of B.

In "run1.nb", the following code can do this job.

B = {};
Do[nb = NotebookOpen[NotebookDirectory[] <> "run.nb"];
SelectionMove[nb, Before, CellContents];
SelectionMove[nb, Next, Word, 3];
SelectionMove[nb, All, Word];
NotebookDelete[nb];
NotebookWrite[nb, i];
SelectionMove[nb, All, Notebook];
NotebookEvaluate[nb];
NotebookClose[nb], {i, 3}]
B


The main idea is that after opening a notebook, I will move the selection to designed point using SelectionMove, in this case is that 1 in A=1. Then replace this part using NotebookDelete and NotebookWrite. Finally evaluate the program in run.nb with NotebookEvaluate and collect the result.