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"). – andre314 May 25 '17 at 17:44Exportwill append content to an already existing file. It doesn't. It replaces the content of a preexisting file with the new content. – m_goldberg May 25 '17 at 17:50@m_goldberg, there is a StringJoin which joins test (contains the imported value of the file) with the new value and exports it to the same file again, so I am joining previous value with the new one. I know that you can write stuff at the end of the file (>>>) but, in reality, I wanted to prepend the new values to the old ones (the new ones in the beginning of the file). I did not know how to do it so I simply StringJoined {new,old}.
– neuron May 26 '17 at 08:44