In the following code, st-out is always #f. Why?
I tried several variants of the following code.
(set st-in (open the output file "out-file.txt"))
(set st-out (open input file "in-file.txt"
))
(define-values (newprocess stdout stdin stderr )
(sub-process st-in st-out #f "C: WINDOWS system32 cmd.exe" "type" "C: Users Andre tobdeleted costco.txt"))
stdout
(printf "stdout: n~a" (port->string stdout))
I want to do the same thing as
(define command-cmd
(string-append
" "C: WINDOWS system32 cmd.exe "
" /c"
" type"
" C: Users Andre tobdeleted costco.txt" ))
(define resul-brut-cmd
(with-output-to-string
(lambda ()
(system command-cmd)
)))
Hi, @AndreMayers.
From the docs, we see the following:
The subprocess procedure returns four values:
- a subprocess value representing the created process;
- an input port piped from the process’s standard output, or #f if stdout was a port;
- an output port piped to the process’s standard input, or #f if stdin was a port;
- an input port piped from the process’s standard error, or #f if stderr was a port or 'stdout.
So, because you provide ports for stdout and stdin, the corresponding values returned by subprocess are false. Compare to the example they give at the end of that section:
(define-values (sp out in err)
(subprocess #f #f #f "/bin/ls" "-l"))
(printf "stdout:\n~a" (port->string out))
(printf "stderr:\n~a" (port->string err))
(close-input-port out)
(close-output-port in)
(close-input-port err)
(subprocess-wait sp)
Happy hacking!
[quote="bakgatviooldoos, post:2, topic:3982"]
"/bin/ls"
May be I don't understand something more profound.
Where can I see the result of "/bin/ls" ?
The following code is almost the same as yours but for Windows 11
(define-values (sp out in err)
(subprocess #f #f #f
"C: WINDOWS system32 cmd.exe"
"dir" ))
(printf "stdout:\n~a" (port->string out))
(printf "stderr:\n~a" (port->string err))
(close-input-port out)
(close-output-port in)
(close-input-port err)
(subprocess-wait sp)
Where I can I see the directory list ?
Apologies if I was being confusing, the example I used, is for a Linux machine.
Hmm, I am not sure what is the matter with cmd.exe. It's probably just that I am being stupid about how ports work here because I can read from the port, but it seems to hang when I try your code.
I am also running Windows 11 on my machine. If I use powershell.exe instead, it seems to work:
#lang racket
(define-values (sp out in err)
(subprocess
#f #f #f
"C:/WINDOWS/System32/WindowsPowerShell/v1.0/powershell.exe"
"dir"))
(printf (port->string out))
(printf (port->string err))
(close-input-port out)
(close-output-port in)
(close-input-port err)
(subprocess-wait sp)
Directory: C:\Users\CHRISTIAAN-BRAND\racket\racket-discourse
Mode LastWriteTime Length Name
---- ------------- ------ ----
dar--l 2025/05/08 14:46 advent-2024
d----- 2025/07/19 00:37 ddrake-wires-lang
-a---- 2025/10/25 03:01 323 andremayers-subprocess.bak
-a---- 2025/10/25 18:22 272 andremayers-subprocess.rkt
-a---l 2024/11/06 15:48 207 andy-generics.bak
-a---l 2024/11/06 16:05 207 andy-generics.rkt
-a---- 2025/07/23 02:04 5642 booklet.txt
-a---- 2025/09/09 22:49 429 damien-mattei-defform.bak.bak
-a---- 2025/09/10 00:49 429 damien-mattei-defform.bak.scrbl
-a---l 2024/11/23 15:13 635 damien-mattei-regex-hashbang.rkt
.
.
.
Thank you. You show me that it works on the standard output port. It help me to pinpoint the problem. I am shy to tell it. I didn't close outport port. Here is the code that works.
(current-directory "C:\Users\Andre\tobedeleted")
(define st-in (open-output-file "out-file.txt"))
(define st-out (open-input-file "in-file.txt" #:mode 'text ))
(define-values (sp out in err)
(subprocess
st-in
st-out
#f
"C:/WINDOWS/System32/WindowsPowerShell/v1.0/powershell.exe"
"dir"))
(printf (port->string st-out))
(printf (port->string err))
(close-input-port st-out)
(close-output-port st-in)
(close-input-port err)
(subprocess-wait sp)
1 Like