Renaming all subdirectories using Ruby -


i have nested folder structure on windows 7 machine. windows refuses delete directories names long. want rename subfolders 2 in hope short enough deleted. script:

@count = 0  dir.glob("**/*") |file|  #find src files in current folder , subfolders   if file.directory?(file)     file.rename(file, file.dirname(file) + file::separator + "2")     @count += 1   end end  puts @count 

when script runs, instead of renaming sub-directories, changes 1 more sub-directory, gradually going 1 level deeper each time. i.e., output running script @ moment is:

c:\>renamer.rb 30 c:\>renamer.rb 31 c:\>renamer.rb 32 

i'm confused why happening , appreciate input.

am taking correct approach? assume ruby's recursive directory deletion methods fail. however, when try , execute

require "fileutils" fileutils.remove_dir ("2", force = true) 

i error

syntax error, unexpected ',', expecting ')' fileutils.remove_dir ("2", force = true)                           ^ syntax error, unexpected ')', expecting end-of-input fileutils.remove_dir ("2", force = true)                                     ^ 

the problem dir.glob("**/*") returns array this:

['folder', 'folder/sub', 'folder/sub/sub'] 

now when do:

file.rename(file, file.dirname(file) + file::separator + "2") 

it rename folder, when reaches folder/sub, doesn't exist anymore, because have renamed folder 2: 2/sub instead of folder/sub. solution reverse array. starts renaming process on deepest level , works way top level:

dir.glob("**/*").reverse.each |file|   # rest of code can stay same end 

as second problem, instead of:

fileutils.remove_dir ("2", force = true) 

you should use:

fileutils.remove_dir("2", true) 

first of all, make sure there no space between remove_dir , (. that's what's causing error.

also force name of parameter , default it's false. that's why see force = false in api. if want force true can pass true function, show above.


Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -