# An easy way to access files/folders/directories with Julia in command line based on language: batch-script

**URL:** <https://discourse.julialang.org/t/an-easy-way-to-access-files-folders-directories-with-julia-in-command-line-based-on-language-batch-script/79873>\
**Category:** Internals & Design\
**Created:** [April 23, 2022, 4:15am UTC](https://discourse.julialang.org/t/an-easy-way-to-access-files-folders-directories-with-julia-in-command-line-based-on-language-batch-script/79873 "2022-04-23T04:15:11Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon29721930](https://avatars.discourse-cdn.com/v4/letter/a/58956e/32.png) [@anon29721930](https://discourse.julialang.org/u/anon29721930)\
**Post date:** [April 23, 2022, 4:15am UTC](https://discourse.julialang.org/t/an-easy-way-to-access-files-folders-directories-with-julia-in-command-line-based-on-language-batch-script/79873/1 "2022-04-23T04:15:11Z")

</div>

Hi all! Community Julia!

#### user stories

1. I would like as a user to list files, folders, directories like I would in shell script or bat-script in the Julia programming language, because it would be an easy way
2. I would like just 2 ls/dir commands that do the same thing but join Linux and Windows users. So… Normally you are a Windows user you find the prompt and type dir and if you are a Linux/Unix user generally you type ls

#### So… here “my solution”:

#### before

```julia
julia> pwd()
"/home/JuliaUser"

```

#### after

```julia
julia> julia --dir #=> background: pwd()
"/home/JuliaUser"

```

#### sample 1: before

```julia
julia> cd(readdir, "/home/JuliaUser/Projects/julia")
34-element Array{String,1}:
 ".circleci"
 ".freebsdci.sh"
 ".git"
 ".gitattributes"
 ".github"
 ⋮
 "test"
 "ui"
 "usr"
 "usr-staging"

```

#### sample 1.1: after

```julia
julia> julia ls --readdir "/home/JuliaUser/Projects/julia" #=> background: cd(readdir, "/home/JuliaUser/Projects/julia")
 ".circleci"
 ".freebsdci.sh"
 ".git"
 ".gitattributes"
 ".github"
 ⋮
 "test"
 "ui"
 "usr"
 "usr-staging"

```

#### sample 1.2: after

```julia
julia> julia dir --readdir "/home/JuliaUser/Projects/julia" #=> background: cd(readdir, "/home/JuliaUser/Projects/julia")
 ".circleci"
 ".freebsdci.sh"
 ".git"
 ".gitattributes"
 ".github"
 ⋮
 "test"
 "ui"
 "usr"
 "usr-staging"

```

#### sample 1.3: after

```julia
julia> julia dir "/home/JuliaUser/Projects/julia" #=> background: cd("/home/JuliaUser/Projects/julia")
 ".circleci"
 ".freebsdci.sh"
 ".git"
 ".gitattributes"
 ".github"
 ⋮
 "test"
 "ui"
 "usr"
 "usr-staging"

```

#### sample 1.4: after

```julia
julia> julia ls "/home/JuliaUser/Projects/julia" #=> background: cd("/home/JuliaUser/Projects/julia")
 ".circleci"
 ".freebsdci.sh"
 ".git"
 ".gitattributes"
 ".github"
 ⋮
 "test"
 "ui"
 "usr"
 "usr-staging"

```

### other case 1

```julia
julia> ./ #=> background: cd("/home/JuliaUser/Projects/julia")
 ".circleci"
 ".freebsdci.sh"
 ".git"
 ".gitattributes"
 ".github"
 ⋮
 "test"
 "ui"
 "usr"
 "usr-staging"

```

### other case 2

```julia
julia> "/home/JuliaUser/Projects/julia" #=> background: cd("/home/JuliaUser/Projects/julia")
 ".circleci"
 ".freebsdci.sh"
 ".git"
 ".gitattributes"
 ".github"
 ⋮
 "test"
 "ui"
 "usr"
 "usr-staging"

```

### other case 3

```julia
julia> "c://mytests//main.jl" #=> background: cd("c:/mytests/main.jl")
┌ Info:
└ Web Server starting at http://127.0.0.1:8888

```

#### my log

```julia
julia> ls
ERROR: UndefVarError: ls not defined

julia> dir
ERROR: UndefVarError: dir not defined

julia> path
ERROR: UndefVarError: path not defined

julia> dir
ERROR: UndefVarError: dir not defined

julia> julia main.jl
ERROR: syntax: extra token "routes" after end of expression
Stacktrace:
 [1] top-level scope
   @ none:1

julia> julia C:\mytests\main.jl
ERROR: syntax: extra token "C" after end of expression
Stacktrace:
 [1] top-level scope
   @ none:1

julia> C:\mytests\main.jl
ERROR: syntax: "\" is not a unary operator
Stacktrace:
 [1] top-level scope
   @ none:1

```

#### draft

```julia-auto
julia> dir c:/mytests/main.jl # call powershell with command dir(Windows)
julia> ls c:/mytests/main.jl # call powershell with command ls(Linux/Unix)
julia> dir "c:/mytests/main.jl" # call powershell with command dir(Windows)
julia> ls "c:/mytests/main.jl" # call powershell with command ls(Linux/Unix)

```

#### why

1. Makes life easier for those who came from shell script/batscript/python in Julia - Based on that, I made this proposal.
2. I know that each programming language has its concept and specificity, but… listing files, folders and directories in Batchscript is trivial, you can use in the command line something like ls/dir - it just works the easy way… Julia should list things like directories, files, folders in an easy way like batscript - this is my initial opinion as someone who is studying Julia. Based on that, I made this proposal.
3. I don’t have much knowledge in the Julia programming language, despite having taken programming courses in the Julia programming language, I have difficulties in understanding how Julia works… _this can help me in this sense to better understand how Julia works if have ls/dir_ because … my initial difficulty was to list files, folders and directories with the Julia programming language. (Note: Over time… I can learn how to use Julia through experience) - I mentioned this because other people might feel like I do, they might be beginners and have been through the same problem as me. Based on that, I made this proposal.
4. The point I want to make clear is from my experience as a beginner and someone interested in contributing to the Julia community. Based on that, I made this proposal.
5. The files are called directly via the command line which makes it easier in Julia … Based on that, I made this proposal

#### references

- [Filesystem · The Julia Language](https://docs.julialang.org/en/v1/base/file/)
- [Batch Script Tutorial](https://www.tutorialspoint.com/batch_script/index.htm)
- https:// www. atlassian .com/ agile/ project-management/user-stories

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [April 23, 2022, 6:40am UTC](https://discourse.julialang.org/t/an-easy-way-to-access-files-folders-directories-with-julia-in-command-line-based-on-language-batch-script/79873/2 "2022-04-23T06:40:44Z")

</div>

> [@PSA: Julia is not at that stage of development anymore](https://discourse.julialang.org/t/psa-julia-is-not-at-that-stage-of-development-anymore/44872):
>
> You heard about this brand new exciting programming language called Julia and have some suggestions for how it should work. Wonderful! We’re happy to have you here and help you learn the language and listen to your ideas! However, please be aware that “new” is a relative term. Yes, Julia is newer than, say, C, which was first created in 1972 and standardized in 1989. It is not, however, new in the sense that it is still being freely designed and changed in arbitrary ways. Julia development start…

Commandline switches are a somewhat expensive commodity to maintain, so I’m doubtful this would be sufficient for such a large change. It would also be breaking, as e.g. `julia> "/home/JuliaUser/Projects/julia"` already has the meaning of just returning the string in the REPL. Julia is not a shell after all.

---

<div class="post-metadata">

**Author:** ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)\
**Post date:** [April 23, 2022, 8:11am UTC](https://discourse.julialang.org/t/an-easy-way-to-access-files-folders-directories-with-julia-in-command-line-based-on-language-batch-script/79873/3 "2022-04-23T08:11:02Z")

</div>

Are you aware of the [`shell>` REPL mode](https://docs.julialang.org/en/v1/stdlib/REPL/#man-shell-mode) accessible via `;` ?

---

<div class="post-metadata">

**Author:** ![anon29721930](https://avatars.discourse-cdn.com/v4/letter/a/58956e/32.png) [@anon29721930](https://discourse.julialang.org/u/anon29721930)\
**Post date:** [April 23, 2022, 12:35pm UTC](https://discourse.julialang.org/t/an-easy-way-to-access-files-folders-directories-with-julia-in-command-line-based-on-language-batch-script/79873/4 "2022-04-23T12:35:35Z")

</div>

I tried …

### log 1

```julia-auto
C:\mytests\>julia ; powershell
ERROR: SystemError: opening file "C:\\mytests\\;": No such file or directory
Stacktrace:
  [1] systemerror(p::String, errno::Int32; extrainfo::Nothing)
    @ Base .\error.jl:174
  [2] #systemerror#68
    @ .\error.jl:173 [inlined]
  [3] systemerror
    @ .\error.jl:173 [inlined]
  [4] open(fname::String; lock::Bool, read::Nothing, write::Nothing, create::Nothing, truncate::Nothing, append::Nothing)
    @ Base .\iostream.jl:293
  [5] open
    @ .\iostream.jl:282 [inlined]
  [6] open(f::Base.var"#364#365"{String}, args::String; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Base .\io.jl:328
  [7] open
    @ .\io.jl:328 [inlined]
  [8] read
    @ .\io.jl:436 [inlined]
  [9] _include(mapexpr::Function, mod::Module, _path::String)
    @ Base .\loading.jl:1249
 [10] include(mod::Module, _path::String)
    @ Base .\Base.jl:418
 [11] exec_options(opts::Base.JLOptions)
    @ Base .\client.jl:292
 [12] _start()
    @ Base .\client.jl:495

```

### log 2

```julia-auto
julia> ; # upon typing ;, the prompt changes (in place) to: shell>

shell> dir
ERROR: IOError: could not spawn `dir`: no such file or directory (ENOENT)
Stacktrace!
.......

```

@Sukera @carstenbauer What do you think of this idea?

#### my idea

```julia-auto
julia> ; powershell 
julia> ; prompt 
julia> shell powershell # ; powershell 
julia> shell prompt # ; prompt 
julia> ; main.jl # call powershell with command dir(Windows)/ls(Linux/Unix)
julia> ; "./" # call powershell with command dir(Windows)/ls(Linux/Unix)
julia> ; ./ # call powershell with command dir(Windows)/ls(Linux/Unix)
julia> ; "c:/mytests/main.jl" # call powershell with command dir(Windows)/ls(Linux/Unix)
julia> ; c:/mytests/main.jl # call powershell with command dir(Windows)/ls(Linux/Unix)

```

### Main arguments

1. Any programming language must be accessible to people
2. Doing things like listing files, folders and folders should be easy in any programming language
3. A language must have some clear working purpose for developers
4. This might solve the log 1 and log 2 problem
5. If you don’t have these commands like dir/ls… then put the symbol ; to allow absolute or partial paths

#### references

- [The Julia REPL · The Julia Language](https://docs.julialang.org/en/v1/stdlib/REPL/#man-shell-mode)
- [https://www.cs.mcgill.ca/~rwest/wikispeedia/wpcd/wp/p/Programming\_language.htm](https://www.cs.mcgill.ca/~rwest/wikispeedia/wpcd/wp/p/Programming_language.htm)

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [April 23, 2022, 12:50pm UTC](https://discourse.julialang.org/t/an-easy-way-to-access-files-folders-directories-with-julia-in-command-line-based-on-language-batch-script/79873/5 "2022-04-23T12:50:22Z")

</div>

I don’t understand. What is it you want that `readdir` doesn’t do?

---

<div class="post-metadata">

**Author:** ![anon29721930](https://avatars.discourse-cdn.com/v4/letter/a/58956e/32.png) [@anon29721930](https://discourse.julialang.org/u/anon29721930)\
**Post date:** [April 23, 2022, 12:52pm UTC](https://discourse.julialang.org/t/an-easy-way-to-access-files-folders-directories-with-julia-in-command-line-based-on-language-batch-script/79873/6 "2022-04-23T12:52:56Z")

</div>

Hi! @gustaphe

1. I would just like a command line to list files, directories or folders… in the shell. just that. … something like ls/dir…
2. I tried …

```julia
C:\mytests\>julia ; powershell
ERROR: SystemError: opening file "C:\\mytests\\;": No such file or directory
Stacktrace:
  [1] systemerror(p::String, errno::Int32; extrainfo::Nothing)
    @ Base .\error.jl:174

```

and

```julia
julia> ; # upon typing ;, the prompt changes (in place) to: shell>

shell> dir
ERROR: IOError: could not spawn `dir`: no such file or directory (ENOENT)
Stacktrace!
.......

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [April 23, 2022, 1:02pm UTC](https://discourse.julialang.org/t/an-easy-way-to-access-files-folders-directories-with-julia-in-command-line-based-on-language-batch-script/79873/7 "2022-04-23T13:02:55Z")

</div>

So something like

```julia
#!/bin/julia
# juliadir.jl
function print_directory(directory)
    println.(readdir(directory))
    return nothing
end
print_directory(get(ARGS, 1, pwd()))

```

```bash
$ chmod +x juliadir.jl
$ ./juliadir.jl ~/.julia
artifacts
compiled
config
dev
environments
juliaup
logs
packages
pluto_notebooks
registries
scratchspaces

```

?

---

<div class="post-metadata">

**Author:** ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)\
**Post date:** [April 23, 2022, 1:08pm UTC](https://discourse.julialang.org/t/an-easy-way-to-access-files-folders-directories-with-julia-in-command-line-based-on-language-batch-script/79873/8 "2022-04-23T13:08:07Z")

</div>

I think the OP is on Windows. The “;” is very ineffectual on Windows.  
I would suggest to install Git, and run julia in the Git bash. Then all those commands  
that the OP would have liked to see would be available.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [April 23, 2022, 2:10pm UTC](https://discourse.julialang.org/t/an-easy-way-to-access-files-folders-directories-with-julia-in-command-line-based-on-language-batch-script/79873/9 "2022-04-23T14:10:42Z")

</div>

Okay I think we need some clarification. Does OP need something that runs _from_ a command line, or something that runs a shell _from_ julia? Because if it’s the former, my function works, but will be a bit less usable than simply aliasing `ls` to `dir` (It might be you need to run it as `$ julia juliadir.jl thedirectory` in Windows). If it’s the latter I’m back to not seeing the issue with `readdir`.

---

<div class="post-metadata">

**Author:** ![devel-chm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devel-chm/32/3572_2.png) [@devel-chm](https://discourse.julialang.org/u/devel-chm)\
**Post date:** [April 23, 2022, 5:28pm UTC](https://discourse.julialang.org/t/an-easy-way-to-access-files-folders-directories-with-julia-in-command-line-based-on-language-batch-script/79873/10 "2022-04-23T17:28:09Z")

</div>

The [Julia REPL shell mode](https://docs.julialang.org/en/v1.6/stdlib/REPL/#man-shell-mode) does work as a typical “unix-ish” shell mode on windows. On posix/unix systems, the shell mode in the REPL will run a shell while on windows it does what I’ll call an “execute mode” since there is not really a default shell on windows.

I haven’t figured out how to make shell mode on windows run a shell (even if it is in the PATH) but if you install some shell such as the git bash one or (my preferred option) use a cygwin install and add the cygwin bin directory to the end of your windows PATH. It is usually in someplace like `C:\cygwin64\bin`.

Then you will at least have the commands for the standard unix utilities from cygwin even if you have to run a shell/powershell/cmd by hand to get full shell capability.

It would be nice if the REPL shell mode allowed one to configure this to work on windows—if you have a shell in the PATH.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![anon29721930](https://avatars.discourse-cdn.com/v4/letter/a/58956e/32.png) [@anon29721930](https://discourse.julialang.org/u/anon29721930)\
**Post date:** [April 23, 2022, 11:20pm UTC](https://discourse.julialang.org/t/an-easy-way-to-access-files-folders-directories-with-julia-in-command-line-based-on-language-batch-script/79873/11 "2022-04-23T23:20:20Z")

</div>

@gustaphe @PetrKryslUCSD … Hi all! Here … my “solution” and you guys are right

```julia
#!/bin/julia
# batchscript.jl

# List files, directories, folders in shell
function filepath(directory)
    println.(readdir(directory))
    return nothing
end

# windows_specific_thing(a) or linux_specific_thing(a) or generic_thing(a)
function filesystem(parameter):
    if Sys.iswindows() || Sys.islinux()
        filepath(parameter)
    else
        return nothing # filepath(parameter)
    end
end

# list all files/directories/folders with symbol: ["./"] or ["dir"] or ["ls"] or [";"] or ["path"] in shell-julia 
function batchscript(parameter): 
    # Calling rdeadline() function
    string = readline() 
    if string == "dir" || string == "ls" || string == ";" || string == "./" || string == "--dir" || string == "--ls" || string == "-ls" || string == "-dir" || string == "--readdir" || string == "-readdir" || string == "readdir"
        # call function 'os'
        filesystem(parameter)
    else # or => "; powershell" || "; cmd"
        return nothing
        end
    end

# case 1: dir "~/.julia" || case 2: ls "~/.julia" || case 3: ; "~/.julia" || case 4: ./ "~/.julia"
batchscript(get(ARGS, 1, pwd())) # add path: "~/.julia"

```

### use cases

```julia-auto
julia> using batchscript
julia> ls "~/.julia"
julia> dir "~/.julia"
julia> "~/.julia"
julia> ./ "~/.julia"
julia> ; "~/.julia"
julia> -dir "~/.julia"
julia> --dir "~/.julia"
julia> -ls "~/.julia"
julia> --ls "~/.julia"
julia> -readdir "~/.julia"
julia> --readdir "~/.julia"
julia> readdir "~/.julia"

```

#### notes

1. current\_path: `dirname(pwd())` for `./` or `ls` or `dir` or `readdir` or `;`
2. list files/directories/folders: `println.(readdir(get(ARGS, 1, pwd()))) "~/.julia" `

#### references

- [GitHub - fredrikekre/jlpkg: A command line interface (CLI) for Pkg, Julia's package manager.](https://github.com/fredrikekre/jlpkg)
- [Handling Operating System Variation · The Julia Language](https://docs.julialang.org/en/v1/manual/handling-operating-system-variation/)

---

<div class="post-metadata">

**Author:** ![anon29721930](https://avatars.discourse-cdn.com/v4/letter/a/58956e/32.png) [@anon29721930](https://discourse.julialang.org/u/anon29721930)\
**Post date:** [April 23, 2022, 11:37pm UTC](https://discourse.julialang.org/t/an-easy-way-to-access-files-folders-directories-with-julia-in-command-line-based-on-language-batch-script/79873/12 "2022-04-23T23:37:34Z")

</div>

> [@gustaphe](#):
>
> Because if it’s the former, my function works, but will be a bit less usable than simply aliasing `ls` to `dir`

- Yeah… truth… that’s right.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [April 24, 2022, 5:14am UTC](https://discourse.julialang.org/t/an-easy-way-to-access-files-folders-directories-with-julia-in-command-line-based-on-language-batch-script/79873/13 "2022-04-24T05:14:38Z")

</div>

What I don’t understand it’s why you would do this with a `readline` and a bunch of string comparisons instead of just `readdir` directly?

A couple of tips anyway:

- Try to name functions and variables a bit more self-explanatorily, `batchscript`, `filesystem`, `parameter` and `filepath` are all a bit opaque
- Instead of chaining `x == "a" || x == "b" || ...`, you can use `x in ("a", "b", ...)`
- `readdir` is (to best effort) OS agnostic, so you don’t need to check
- Comments are good, but a comment that tells you which function is called on the next line doesn’t really add any information

---

<div class="post-metadata">

**Author:** ![anon29721930](https://avatars.discourse-cdn.com/v4/letter/a/58956e/32.png) [@anon29721930](https://discourse.julialang.org/u/anon29721930)\
**Post date:** [April 25, 2022, 5:37pm UTC](https://discourse.julialang.org/t/an-easy-way-to-access-files-folders-directories-with-julia-in-command-line-based-on-language-batch-script/79873/14 "2022-04-25T17:37:46Z")

</div>

@gustaphe thank you very much for the feedback … I will improve my code. You are awesome

---

<div class="post-metadata">

**Author:** ![anon29721930](https://avatars.discourse-cdn.com/v4/letter/a/58956e/32.png) [@anon29721930](https://discourse.julialang.org/u/anon29721930)\
**Post date:** [July 2, 2022, 2:57am UTC](https://discourse.julialang.org/t/an-easy-way-to-access-files-folders-directories-with-julia-in-command-line-based-on-language-batch-script/79873/15 "2022-07-02T02:57:13Z")

</div>

please, close topic
