# How to automate Mathematica scripts? - I [duplicate]

I would like to use the Windows Task Scheduler, or Visual Cron, to start a Mathematica notebook or .m file to do some system maintenance tasks: copy files from one place to another, etc. How do I set up the notebook/file so it will execute when started without asking any questions?

I answered this question myself here.

-

## marked as duplicate by Kuba, Oleksandr R., Sjoerd C. de Vries, R. M.♦Aug 5 '13 at 16:44

I'll add an obligatory "Mathematica is the wrong tool for this job" note. Also, AFAIK, it doesn't preserve/you can't modify file permissions when copying from one place to another natively — something I consider very critical when it comes to system maintenance tasks (maybe not for what you have in mind). – R. M. Jul 1 '13 at 19:39
I use Mathematica in notebook form to do this kind of thing and it works nicely. It's easy to use FileNames, Select, Union, Complement, StringReplace, etc. to handle the logic of what I want to implement. I don't need to change the file permissions. – George Wolfe Jul 1 '13 at 19:46
Perhaps useful: mathematica.stackexchange.com/q/9936/131 – Yves Klett Jul 1 '13 at 20:33
I suppose Run will always enable you to fill in the gaps. – Jacob Akkerboom Jul 2 '13 at 18:34
I've just found this are you familiar with this? It seems it is all you need. – Kuba Aug 5 '13 at 11:00

I can't help with the Windows part it, but I would imagine this is a two step operation. First, invoke Mathematica -- which you can do form the command line (or cron, presumably), as shown by the instructions here, and have Mathematica open the desired notebook.

Once started, you can ensure that the commands will be run by using initialization cells, which automatically evaluate when the notebook is opened. Instructions for that are here.

-
This is helpful. Lot's of little details to work out here. I'll post what I end up doing. In the meantime, if anyone has anything to add, I be grateful. I'm surprised that this isn't routine. – George Wolfe Jul 2 '13 at 17:50

I hope someone will post here an answer based on InitializationCells because I wasn't able to make them work for me.

This is quick walkaround:

DynamicModule[{},
Framed["dir created"]
, Initialization :> (
CreateDirectory[FileNameJoin[{"C:", "Users", "Kuba", "Desktop", "dir1"}]];
CreateDirectory[FileNameJoin[{"C:", "Users", "Kuba", "Desktop", "dir2"}]];
)
]


Evaluate this code (Probably You will have to change "Kuba" in path :)). Save it in trusted directory. Create task via Windows Task Scheduler to open this file.

Works for me.

Big disadvantage is that You have to evaluate code with all procedures.

Quick solution Put conditional construct in Initialization of this DynamicModule. It can check if there is directory called "ENABLED" in it's directory. There will not be such during first evaluation. Create manually directory "ENABLED" after saving main notebook.

-