Hi essess. I'm not really sure what you're looking for, but, if I'm not wrong, printing output to the console window does not actually execute any commands. The console window is simply a CUI for program text output, and not the operating systems' command processor. The results that you're getting are simply echoes, and not executions.
To run a program, you'd have to use PureBasic's
RunProgram() function:
Code: Select all
RunProgram("calc.exe")
RunProgram("notepad.exe", "someFile.txt", "c:\") ;someFile.txt should be in c:\
However, to execute command line instructions, you'd have to run the command processor itself, and then pass those instructions as parameters:
Code: Select all
;the /k directive keeps the command prompt alive after execution
RunProgram("cmd", "/k dir", "c:\")
The same goes for redirection operators; since they are functions of the command processor, they can only be invoked through it:
Code: Select all
;the /c directive kills the command prompt upon completion
RunProgram("cmd", "/c dir > cDirFile.txt", "c:\")
The following short example demonstrates how the same is achieved with program output, and also illustrates the dangers of recursive program execution if not properly structured. If your original example had worked, it would have recursively executed itself in an endless loop:
Code: Select all
OpenConsole()
PrintN(ProgramFilename())
p.s = ProgramParameter()
If p = "": p = "1": EndIf
ConsoleTitle("Call #" + p)
PrintN("Select:" + #CRLF$ + "1. to demonstrate recursive execution" +
#CRLF$ + "2. to test output redirection" +
#CRLF$ + "3. to test input & output redirection" +
#CRLF$ + "0. to terminate")
run.s = Input()
Select Val(run)
Case 1
p = Str(Val(p) + 1)
PrintN("Notice the incremental count in the console title...")
RunProgram("c:\console.exe", p, "")
Case 2
PrintN("Program output saved to file c:\outputFile.txt")
PrintN("Any key to terminate...")
RunProgram("cmd", "/c c:\console.exe > c:\outputFile.txt", "")
Case 3
PrintN("sort utility received input from c:\unsortedFile.txt" +
#CRLF$ + "and saved output to c:\sortedFile.txt")
PrintN("Any key to terminate...")
RunProgram("cmd", "/c sort < c:\unsortedFile.txt > c:\sortedFile.txt", "")
Input()
EndSelect
CloseConsole()
It also works directly from the IDE, but the program name and working directory have to be changed to PureBasic_Compilation#.exe and the default compilation folder respectively.
Like I said, I'm not really sure about your requirements, but hope it helps.
