Re-naming several folders

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!

You don’t need packages for this, just loop over the folders and mv them:

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"
2 Likes

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

If the pattern is always digits followed by a - then garbage you can split by the -:

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:

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 depending on your needs and what pattern the files actually have.

1 Like