Liberty BASIC Community Forum
Liberty BASIC Programming Discussions >> Tips and Information >> Getting feedback from cmd.exe
http://libertybasic.conforums.com/index.cgi?board=tips&action=display&num=1465209618

Getting feedback from cmd.exe
Post by Mick on Jun 6th, 2016, 05:40am

Hi all back again, having lost all my old e-mails!!! :(

Trying again to get data from a dos input by way of :-


IM$= "ipconfig/all"

run "cmd.exe /c "; chr$( 34); IM$; chr$( 34)

but can't for the life of me remember how to retain the dos output as a string, or even just save it as a txt file to read from later stages in the code.

many thanks in anticipation of assistance here.



Re: Getting feedback from cmd.exe
Post by Rod on Jun 6th, 2016, 08:59am

You need to pipe the data a file. This code reads %appdata% and outputs the dos result to a file > is the character than redirects the output to the named file.


Code:
'all of these files will be created in DefaultDir$
open "test.bat" for output as #test
#test "echo %APPDATA% > test.txt"
close #test
run "test.bat /q" ,hide
open "test.txt" for input as #test
line input #test, appdata$
close #test
appdata$=trim$(appdata$)+"\"
'use this variable in place of %APPDATA%
print appdata$



print DefaultDir$
result = mkdir( appdata$;"test")

open appdata$;"test\test.dat" for output as #1
#1 "hello"
close #1
open appdata$;"test\test.dat" for input as #1
i$=input$(#1,lof(#1))
 



More info here http://www.lagmonster.org/docs/DOS7/pipes.html
Re: Getting feedback from cmd.exe
Post by Mick on Jun 6th, 2016, 10:27am

Thanks Rod, I'll take a look at that code again, but as I remember it it was much simpler and along the lines of :-


IM$= "ipconfig/all"
run "cmd.exe /c "; chr$( 34); IM$; chr$( 34) > test$
print test$
end

Unfortunately test$ doesn't seem to contain a string sad It's been a long while and I'm struggling to find my disks containing my old codes so am needing to start again sad




Re: Getting feedback from cmd.exe
Post by tenochtitlanuk on Jun 6th, 2016, 11:48am

Following works on my Linux machines- watch for the space before '/all'.
Any use?
Code:
IM$     = "ipconfig /all"

cmd$    = IM$ + " > test.txt"

run cmd$

open "test.txt" for input as #fi
    f$ =input$( #fi, lof( #fi))
close #fi

print f$
end

 

Re: Getting feedback from cmd.exe
Post by Mick on Jun 6th, 2016, 1:37pm

Oddly, No, runs the cmd side of the script no problems, test.txt remains blank cry

it'll be a simple error somewhere, I'll just go find a hammer and "fix" this aging lappy!!!
Re: Getting feedback from cmd.exe
Post by cundo on Jun 7th, 2016, 09:07am

Code:
IM$= "ipconfig /all"
run "cmd.exe /c "; chr$( 34); IM$; chr$( 34) ;" > test.txt"
end
 

Test is a text file. Must be between quotes including the > symbol.
With tenoch's print.
Code:
IM$= "ipconfig /all"
run "cmd.exe /c "; chr$( 34); IM$; chr$( 34) ;" > test.txt"
open "test.txt" for input as #fi
    f$ =input$( #fi, lof( #fi))
close #fi

print f$
end
 

Re: Getting feedback from cmd.exe
Post by Mick on Jun 7th, 2016, 10:35am

Fantastic Cundo, That has me back on track.

Kind regards and may all your Gosubs Return smiley


Re: Getting feedback from cmd.exe
Post by tenochtitlanuk on Jun 7th, 2016, 10:51am

That fails for me, cundo...
Why use cmd.exe to run ipconfig anway, rather than run it directly?
Never did like DOS commands or their successor command line ones...
Re: Getting feedback from cmd.exe
Post by Mick on Jun 7th, 2016, 10:59am

It wasn't specifically for the IPconfig aspect, merely the obtaining usable data from any cmd prompt and placing in a file to be used later.

I've always had a fondness for Dos and batch files, just been so long now I'm considering a liberty based help sheet for syntax and switch reminders etc.
Re: Getting feedback from cmd.exe
Post by Mystic on Jun 7th, 2016, 11:14am

This helped me remove some "lazy" code from me in one of my programs. I was just creating a batch file and running the batch file. smiley

Thanks!
Re: Getting feedback from cmd.exe
Post by Mick on Jun 7th, 2016, 11:49am

yes, tenochtitlanuk it does work, but for me, only on the second pass, for some reason one can't immediately open a file only just written in the same code :S


Re: Getting feedback from cmd.exe
Post by cundo on Jun 7th, 2016, 9:41pm

Try adding a SLEEP, or a file exist routine. Maybe both.