Ruby – How To Convert all Folder, Subfolders, Files To Lowercase

In this post, we will show you how to convert all folders, subfolders, and files to Lowercase using Ruby.

The below are code that show how to do this and we separate it into difference cases, so choose which one that suits your needs.

 

Case 1: You want to convert all folders, subfolders, and files to Lowercase

In this case, you want to convert everything to lowercase including folders, subfolders, and files.

# encoding: utf-8

require 'fileutils'

Dir.glob("./**/*").each do |file|
	File.rename(file, file.downcase)
end


 

Case 2: You only want to convert all folders, subfolders to Lowercase

There is a bit difference from the case 1, in this case 2, you only want to convert folders and subfolders to lowercase, without converting files.

# encoding: utf-8

require 'fileutils'

Dir.glob("./**/*").each do |file|
	if File.directory?(file)
		File.rename(file, file.downcase)
	end
end

 

Case 3: You only want to convert all files to Lowercase

In this case, you only want to convert all files to lowercase, without converting folders and subfolders.

# encoding: utf-8

require 'fileutils'

Dir.glob("./**/*").each do |file|
	if File.file?(file)
		File.rename(file, file.downcase)
	end
end

 

Case 4: You only want to convert all parent folders to Lowercase

In this case, you only want to convert all parent folders to lowercase, without converting subfolders and files.

# encoding: utf-8

require 'fileutils'

Dir.glob("./**").each do |file|
	if File.directory?(file)
		File.rename(file, file.downcase)
	end
end

5 1 vote
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x