# How to use subprocess

**URL:** <https://racket.discourse.group/t/how-to-use-subprocess/3982>\
**Category:** Questions & Answers\
**Tags:** system\
**Created:** [October 24, 2025, 9:35pm UTC](https://racket.discourse.group/t/how-to-use-subprocess/3982 "2025-10-24T21:35:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndreMayers](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@AndreMayers](https://racket.discourse.group/u/AndreMayers)\
**Post date:** [October 24, 2025, 9:35pm UTC](https://racket.discourse.group/t/how-to-use-subprocess/3982/1 "2025-10-24T21:35:55Z")

</div>

In the following code, st-out is always `#f`. Why?  
I tried several variants of the following code.

```scheme
(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

```scheme
(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)
      )))

```

---

<div class="post-metadata">

**Author:** ![bakgatviooldoos](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/bakgatviooldoos/32/1381_2.png) [@bakgatviooldoos](https://racket.discourse.group/u/bakgatviooldoos)\
**Post date:** [October 25, 2025, 1:20am UTC](https://racket.discourse.group/t/how-to-use-subprocess/3982/2 "2025-10-25T01:20:26Z")

</div>

Hi, @AndreMayers.

From the docs, we see the following:

> The [subprocess](https://docs.racket-lang.org/reference/subprocess.html#%28def._%28%28quote._~23~25kernel%29._subprocess%29%29) 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:

```scheme
(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!

---

<div class="post-metadata">

**Author:** ![AndreMayers](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@AndreMayers](https://racket.discourse.group/u/AndreMayers)\
**Post date:** [October 25, 2025, 1:00pm UTC](https://racket.discourse.group/t/how-to-use-subprocess/3982/3 "2025-10-25T13:00:30Z")

</div>

[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 ?

---

<div class="post-metadata">

**Author:** ![bakgatviooldoos](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/bakgatviooldoos/32/1381_2.png) [@bakgatviooldoos](https://racket.discourse.group/u/bakgatviooldoos)\
**Post date:** [October 25, 2025, 4:35pm UTC](https://racket.discourse.group/t/how-to-use-subprocess/3982/4 "2025-10-25T16:35:03Z")

</div>

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:

```scheme
#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)

```

```scheme
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

.
.
.

```

---

<div class="post-metadata">

**Author:** ![AndreMayers](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@AndreMayers](https://racket.discourse.group/u/AndreMayers)\
**Post date:** [October 25, 2025, 5:48pm UTC](https://racket.discourse.group/t/how-to-use-subprocess/3982/5 "2025-10-25T17:48:37Z")

</div>

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)
