# Re-naming several folders

**URL:** https://discourse.julialang.org/t/re-naming-several-folders/33841
**Category:** New to Julia
**Created:** [January 27, 2020, 11:55am UTC](https://discourse.julialang.org/t/re-naming-several-folders/33841 "2020-01-27T11:55:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![samantp](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@samantp](https://discourse.julialang.org/u/samantp)
#### Post date: [January 27, 2020, 11:55am UTC](https://discourse.julialang.org/t/re-naming-several-folders/33841/1 "2020-01-27T11:55:54Z")

</div>

Hi,  
I am just getting started with Julia so apologies if this is too simple a question.  
I have a series of directories (folders) with names as follows

6-ep2ddifftraDYNDIST-69284  
7-ep2ddifftraDYNDISTMIX-61648  
8-bfsfbsjahfa  
…  
etc.

Basically, a number, followed by a hyphen, followed by some gibberish (the gibberish has no consistency in length, value, or anything).  
I want to rename all of these folders to just the number,  
so for example  
6-ep2ddifftraDYNDIST-69284 becomes 6  
7-ep2ddifftraDYNDISTMIX-61648 becomes 7

etc.

So, how would I go about doing this? I am quite new so I don’t know which packages to use or even where to begin finding this out. I can loop through the folders and do something to the effect of name=StringName[0] but I am not sure where to find the packages to do this,

Thanks in advance!

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [January 27, 2020, 12:21pm UTC](https://discourse.julialang.org/t/re-naming-several-folders/33841/2 "2020-01-27T12:21:53Z")

</div>

You don’t need packages for this, just loop over the folders and [`mv`](https://docs.julialang.org/en/v1/base/file/#Base.Filesystem.mv) them:

```julia
julia> readdir()
3-element Array{String,1}:
 "6-ep2ddifftraDYNDIST-69284"   
 "7-ep2ddifftraDYNDISTMIX-61648"
 "8-bfsfbsjahfa"                

julia> for f in readdir()
           mv(f, string(first(f)))
       end

julia> readdir()
3-element Array{String,1}:
 "6"
 "7"
 "8"

```

---

<div class="post-metadata">

### Author: ![samantp](https://avatars.discourse-cdn.com/v4/letter/s/f475e1/32.png) [@samantp](https://discourse.julialang.org/u/samantp)
#### Post date: [January 27, 2020, 12:48pm UTC](https://discourse.julialang.org/t/re-naming-several-folders/33841/3 "2020-01-27T12:48:26Z")

</div>

thank you! I think that should work, but what do I do in the case that the folder number is 10 or greater? Would the string(first(f)) not have a problem with this as it only accesses the first entry? (alternately, is there a way to add leading zeros)

thanks

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [January 27, 2020, 12:58pm UTC](https://discourse.julialang.org/t/re-naming-several-folders/33841/4 "2020-01-27T12:58:00Z")

</div>

If the pattern is always digits followed by a `-` then garbage you can [`split`](https://docs.julialang.org/en/v1/base/strings/#Base.split) by the `-`:

```julia
julia> readdir()
3-element Array{String,1}:
 "1-abc"  
 "10-abc" 
 "100-abc"

julia> for f in readdir()
           n = first(split(f, '-'))
           mv(f, n)
       end

julia> readdir()
3-element Array{String,1}:
 "1"  
 "10" 
 "100"

```

If you want to pad with leading zeros you can use [`lpad`](https://docs.julialang.org/en/v1/base/strings/#Base.lpad):

```julia
julia> readdir()
3-element Array{String,1}:
 "1-abc"  
 "10-abc" 
 "100-abc"

julia> for f in readdir()
           n = lpad(first(split(f, '-')), 3, '0')
           mv(f, n)
       end

julia> readdir()
3-element Array{String,1}:
 "001"
 "010"
 "100"

```

This also seems like a job for a [regex](https://docs.julialang.org/en/v1/base/strings/#Base.@r_str) depending on your needs and what pattern the files actually have.
