Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / Live Batch Output Redirection

Author
Message
Mr Kohlenstoff
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 29th Nov 2012 13:27 Edited at: 29th Nov 2012 14:46
Hi,

There's a small problem I can't really solve so far regarding the console output of batch files.

I wrote a DBP program that at one point creates a batch-file that calls the Scala interpreter (Scala is a language based on Java, but that doesn't really matter) with a file name used as argument, so this batch file leads to a windows console opening up and executing the command "scala <file>".

What I'd like to do now is to immediately print the output, that usually shows up in the console, in my DBP program. In theory this is pretty simple using standard redirection: "scala MyProgram >log.txt" for instance.
However, this method doesn't work the way I want it to, given that I can't access log.txt until the scala program terminated - but considering that it may run for quite a while, it would be of great advantage if I could access the output in real time.

Oddly enough, this is possible manually. I can for instance open the log.txt file while it's being modified with a standard text editor. However, not with DBP, since file exist("log.txt") keeps returning 0/false until the scala program terminates. Which is really kind of weird.

Does anybody have an idea how I could solve this problem?

Thanks in advance,
MrK



Edit: An alternative solution for me would be to redirect the output of "scala <file>" not only to a log-file, but also show it in the console in parallel. Unfortunately, this seems to be impossible in the windows standard console, since the "tee" command that would be used for such a task is not available there, nor is any suitable alternative.
I'm really mad at Scala and Windows right now. Oh yeah, and Dark Basic that keeps thinking an existing file does not exist for some inexplicable reason.

mr_d
DBPro Tool Maker
18
Years of Service
User Offline
Joined: 26th Mar 2007
Location: Somewhere In Australia
Posted: 29th Nov 2012 14:44
quick question: above where you have file exist(log.txt) always returns 0/false. can you confirm that log.txt has quotes around it in the parentheses? otherwise, no wonder it always returns 0 as it thinks you're passing some sort of (type) variable.

Mr Kohlenstoff
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 29th Nov 2012 14:48
Sorry, my bad. Yes, in the program I'm checking the value of 'file exist("log.txt")'. Also, the value returned by this statement turns to 1 immediately when the scala program has termintated, so the path and everything should be correct.

mr_d
DBPro Tool Maker
18
Years of Service
User Offline
Joined: 26th Mar 2007
Location: Somewhere In Australia
Posted: 29th Nov 2012 16:48 Edited at: 29th Nov 2012 17:11
Mr Kohlenstoff - try the attached to see if that helps
it should only be used for programs that have little to moderate amount of output as it opens and closes the output file as it appends each line to it which is not very efficient and certainly not best practice...

Attachments

Login to view attachments
Dar13
17
Years of Service
User Offline
Joined: 12th May 2008
Location: Microsoft VisualStudio 2010 Professional
Posted: 29th Nov 2012 17:45
DBPro thinks the file doesn't exist because it can't get exclusive access to it(because the Scala interpreter is writing to it).

I've been looking up ways you could grab the standard output from the commandline, but it's gonna be messy. Probably using named pipes created from within DBP(via manual user32 DLL calls). Have you tried just opening the file without checking for existence? It won't magically update for you, you'll have to reload it over some interval, but it just might let you load it.

Mr Kohlenstoff
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 29th Nov 2012 17:59
Quote: "Have you tried just opening the file without checking for existence? It won't magically update for you, you'll have to reload it over some interval, but it just might let you load it."


I thought about that, but haven't tried it yet. I don't really expect it to work, but you never know.

@mr_d thank you, that looks like it might work - there's not much output anyway, just occasional progress information and stuff like that, usually not more than 10 or so lines, so it suits the problem.

I haven't got much time left today, so I'll try those things tomorrow.

bitJericho
22
Years of Service
User Offline
Joined: 9th Oct 2002
Location: United States
Posted: 29th Nov 2012 18:16 Edited at: 29th Nov 2012 18:17
What you need is to implement standard streams in order to do this properly: http://en.wikipedia.org/wiki/Standard_streams

You might have to make a plugin for it. I couldn't find any that does this. Here's some info on an implementation in freebasic:

http://www.freebasic.net/forum/viewtopic.php?p=152043

Here's some commands that are in purebasic (for reference only):

http://purebasic.com/documentation/process/index.html

Visit my blog http://www.canales.me.
mr_d
DBPro Tool Maker
18
Years of Service
User Offline
Joined: 26th Mar 2007
Location: Somewhere In Australia
Posted: 1st Dec 2012 02:00
Mr Kohlenstoff, were you able to try it out? wanted to check whether it did actually do what you needed it to do as I was only able to perform my own rudimentary tests using default windows supplied program ping.exe and ipconfig.exe? Would be good to know that you ended up with good (or at least sufficient) results. Cheers.

Mr Kohlenstoff
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 4th Dec 2012 11:13
Sorry for the late response, I couldn't access my working computer for the last few days.

@Dar13: Using "open to read" returns a "file does not exist"-error in that situation, so this is sadly not an option.

@bitJericho: Sounds like a proper solution, but also seems like I would be spending more time on the standard streams implementation than on my actual software. In case I release the program at some point in the future this might come in handy - but for the time being I was hoping to find some quick hack.

@mr_d: It was a bit of a struggle to get it to work, but now it does, and it seems to be doing just what I wanted. For now this will suffice - thank you!

mr_d
DBPro Tool Maker
18
Years of Service
User Offline
Joined: 26th Mar 2007
Location: Somewhere In Australia
Posted: 4th Dec 2012 11:21
it good to hear that it finally worked for you, but would you mind giving some details as to what the problems were and how you solved them? it was my belief that it should have basically worked as needed as it was without doing anything extra or out of the ordinary.

Mr Kohlenstoff
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Germany
Posted: 4th Dec 2012 11:33 Edited at: 4th Dec 2012 11:35
Nothing wrong with the program, I just had to find a way to activate the Windows Script Host (which was just a change of a variable I had to make in the registry) and on top of that using something like "myClass.class" or "scala myClass" as path of the executable inside your "exec_program_and_write.bat" did not work out, so I had to create a new batch-file containing "scala myClass" and let your batch-file call this new one. Also I had to make some minor changes to the way my software handled the situation, but that was a matter of maybe 3 minutes. So really nothing serious.

mr_d
DBPro Tool Maker
18
Years of Service
User Offline
Joined: 26th Mar 2007
Location: Somewhere In Australia
Posted: 4th Dec 2012 13:44
ok, cool. sounds like a bit of pain that you managed to overcome. good going. thanks for the info - sounds like you had to do a least a little bit of searching and fiddling around to get around all those minor difficulties mentioned; some might have given up at any of those points - again, glad to hear you didn't and got it working. cheers.

Login to post a reply

Server time is: 2025-05-19 15:54:10
Your offset time is: 2025-05-19 15:54:10