Wojtek Turowicz
Wojtek Turowicz

Reputation: 4131

Delete folder content apart from one subfolder with Ruby

what would be the best way of purging folder's contents apart from one subfolder?

Example:

input:

Folder_A
| Folder_B
| DoNotDeleteMe
| Folder_C
| Some_File

result:

Folder_A
| DoNotDeleteMe

Is there any FileUtils command that can help me with that? Wildcards?

Upvotes: 1

Views: 185

Answers (1)

Wojtek Turowicz
Wojtek Turowicz

Reputation: 4131

I went for:

Dir.foreach(myPath) do |item|
  next if item == '.' or item == '..' or item == mySubDir
  FileUtils.rm_rf File.join(myPath, item)
end

Upvotes: 1

Related Questions