I am writting with the hope of getting some help when using "SystemOpen".
Here is a simple example:
dir = "C:\\test2\\";
batFile = StringJoin[dir, "test", ".bat"];
txtFile = StringJoin[dir, "test", ".txt"]
Export[
batFile
, StringJoin["echo %date% %time%>", dir, "log.txt"]
, "Text"
];
Button["Press to Run"
, Export[txtFile, DateString[]]
; SystemOpen[ batFile ]
; Pause[5]
; test = Import[txtFile, "Text"]
; Export[
txtFile
, StringJoin[test, "\n", DateString[]]
]
]
First line -> we define the working directory.
Second line -> We create a .bat file in the selected directory. When running it will print the current time in a log.txt file in the same directory.
Third line -> We create a button.
When we press the button:
- it exports the initial time in a "test.txt" file.
- Then it should run the .bat file and save the same time.
- Then it should pause for 5 seconds.
- Finally, it will read the initial time in "test.txt" and
- append a new line with the new current time.
We should see that the log.txt file has the initial time. On the other hand, it has the ending one!!!
Mathematica clearly does all the operations and leaves SystemOpen
for the end (I tried other ideas such as loops or similar, always to find that SystemOpen
executes at the end!!!).
Is this a bug? can it be solved? I really want to use SystemOpen
as I know that it works like charm except for this issue.
Thanks to george2079 (SystemOpen), he realized that this happens when using a Button
.
For example:
Do[
Export[txtFile, DateString[]]
; SystemOpen[batFile]
; Pause[5]
; test = Import[txtFile, "Text"]
; Export[
txtFile
, StringJoin[test, "\n", DateString[]]
]
, {i, 1, 1}
]
will work fine. My program has several menus and buttons and I need that button. Does someone know how to bypass this problem using "systemopen"?
Thanks in advance.
Method-> "Queued"
to the Button. (the default is"Preemptive"
). $\endgroup$Export
will append content to an already existing file. It doesn't. It replaces the content of a preexisting file with the new content. $\endgroup$