Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change directory using os:cmd/1

Tags:

erlang

I am trying to change the directory in the command line from a gen_server using

os:cmd("cd d:\temp").

but nothing happense, the return is just an empty list and I remain in the same directory. any ideas?

like image 625
Damian Avatar asked Dec 18 '22 06:12

Damian


2 Answers

Try using file:set_cwd(Dir) to change your current dir.

like image 118
Prikrutil Avatar answered Dec 30 '22 03:12

Prikrutil


cmd() runs a sub-shell, which you're telling to change directory, then the sub-shell exits, having changed nothing about its parent process's environment.

You want to use cd() instead, if you're in the shell, or file:set_cwd() at runtime within an Erlang program.

Another option, if you want to run another program and have its working directory be different from the one Erlang is using is to pass the {cd, Dir} tuple to open_port().

like image 28
Warren Young Avatar answered Dec 30 '22 04:12

Warren Young