Please find the below code:
current_dir = Dir.pwd
Dir.chdir('C:\Documents and Settings\Peter\My Documents\userdata\Downloaded Files')
current_dir = Dir.pwd
Dir.entries(current_dir)[2..-1].each do |dir|
puts dir
puts "-------"
Dir.entries(dir)[2..-1].each do |d|
#file_loc_dir =
#File.rename(d, dir.concat(d))
puts File.realdirpath(d ,dir)
#puts dir.concat(d)
end
puts "-------"
end
Output:
C:\Documents and Settings\peter\My Documents\userdata\Ruby\Scripts>FileNamere
naming.rb
D07141
-------
C:/Documents and Settings/peter/My Documents/userdata/Downloaded Files/D07141
/Water lilies.jpg
C:/Documents and Settings/peter/My Documents/userdata/Downloaded Files/D07141
/Winter.jpg
-------
D07141101
-------
C:/Documents and Settings/peter/My Documents/userdata/Downloaded Files/D07141
101/Water lilies.jpg
C:/Documents and Settings/peter/My Documents/userdata/Downloaded Files/D07141
101/Winter.jpg
Confusion:
While puts dir gives D07141 and D07141101, File.realdirpath(d ,dir) gives the expanded directory name. Could you please clarify the logic?
From the documentation:
(http://apidock.com/ruby/v1_9_3_125/File/realdirpath/class)
It is expanded because that is what the method does. You aren’t calling
putson the dir inside of the#entriescall, you are calling it on the return value ofFile.realdirpath, which, as discussed above, expands the passed in entry.