Strip plugins folder of old version

My plugin has changed name.
I need help coding (in the new one) to remove old folder and rb file from plugins folder.
(I do not wish to load fileutils.rb)
I guess I have to start with
path = (File.dirname FILE) # plugins
oldf = File.join(path, ‘OLD’)
then I know I have to delete everything in the folder (with foreach ?)
then delete the folder itself.
then the rb file.

I did try to understand the ruby doc chapter on directories and files. To no avail.
I need an example to understand.

I would keep the file names, even id the display name has changed, to avoid messing with this. Or perhaps make both versions function side by side without clashes, even if it makes the old one redundant (and users are recommended to remove it).

You can also use FileUtils.rm_rf("dir/to/remove") but I personally find such methods slightly scary. One of the scariest horror stories I’ve heard was about an empty/invalid variable being interpreted as the filesystem root by a similar method. :fearful:

fileutils is the easiest way, in all recent SketchUp versions…

You have the basis of what’s needed, but you have not formatted the you posted in <…/> so __FILE__ looks like FILE etc

When you newly named RB loads it needs to do a search for outdated RB and folder-name.
Let’s assume the RB file etc is referenced as OLD
Assembled from the Plugins folder path and a name…

path=File.dirname(__FILE__)
oldf=File.join(path, 'OLD.rb')

then

if File.exist?(oldf)
  File.delete(oldf)
end

Similarly for the subfolder exists…

olddir=File.join(path, 'OLD')
if File.exist?(olddir)
  files=Dir.entries(olddir)
  files.each{|f|
    next if f=='.' || f=='..'
    File.delete(File.join(olddir, f))
  }
  File.delete(olddir)
end

If there are nested subfolders inside the ‘OLD’ subfolder, then you need to empty those, and then delete them as you proceed, finally deleting the main ‘OLD’ subfolder…
this means trapping for file types - directory / folder - etc before clearing any contents…
But since you know your file/folder structure it should be relatively straightforward to set up a ‘list’ of things to process in a logical order…

Remember that the OLD file will have first loaded in the session when you first run your newly named files so a dialog prompting for a SketchUp restart after [if] your tidying has run up would also be in order…

That’s interesting.
Well I guess I will try fileutils after all.

Below is the code I’m using.
Works fine on MAC. Not so on pc.

module Mc
path = File.dirname(FILE).freeze # = plugins
oldf = File.join(path, ‘Mc_old.rb’)
olddir = File.join(path, ‘Mc_old’)
messa = (‘CLEANING NEEDED.’) + “\n” +
(‘PLEASE QUIT AND RELAUCH SketchUp AGAIN after.’)

if File.exist?(oldf)
UI.messagebox(messa)
File.delete(oldf)
messagdone = true
end #if

if File.exist?(olddir)
UI.messagebox(messa) unless messagdone
::FileUtils.rm_rf(olddir)
end #if

end #module

Here is the error message on pc:

  Error loading file Extname.rb_
  Error: #<NameError: uninitialized constant Extname::FileUtils>_
  C:/Users/pc1/AppData/Roaming/SketchUp/SketchUp2018/SketchUp/Plugins/Extname.rb:54:in `<module:Mc>'
_ C:/Users/pc1/AppData/Roaming/SketchUp/SketchUp2018/SketchUp/Plugins/Extname.rb:35:in `<top (required)>'

So looks like FileUtils has a difficulty

where is your require for FileUtils ??

john

1 Like

Thought it was useless since it worked fine without it on Mac.
Pc needs it ?
I tested adding it in the part that did not work:
if File.exist?(olddir)
require ‘FileUtils.rb’
FileUtils.rm_rf(olddir)
end #if

It works.!
But once added, console yields many lines like the following:
/Applications/SketchUp 2018/SketchUp.app/Contents/Frameworks/Ruby.framework/Versions/2.2/lib/ruby/2.2.0/fileutils.rb:1680: warning: previous definition of METHODS was here

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.