Board index Scripting and Automation Groovy code to delete file based on string or size

Groovy code to delete file based on string or size

Running FileBot from the console, Groovy / FileBot scripting, shell scripts, etc


Posts: 12
Hi,

First of all FileBot is the best!!! OK then I want to delete files where the name includes a certain string. This is to be used when a folder include a sample video.
If that is not possible then I would like to delete files where the size is bellow X number of MB. I'm using the sorty.groovy script. I added a anime entry that was easy.
Next I want to add some code after the extract code to delete files that are sample videos.
I figure I could use the file.has or something, but i have no idea about groovy code. Thank you in advance.

rednoah User avatar
The Source

Posts: 2080
Location: 北京

Somethink like this will do the job:
// if name contains "sample" and filesize is less than 20 MB
def selection = args.getFiles{ it.name =~ "sample" && it.length() < 20 * 1024 * 1024}
selection.each{ println "delete $it" }

// delete all
selection*.delete()
FileBot is free software. Please help support FileBot by writing a review or considering a donation.
Image


Posts: 12
Thank you I added that code to the general format in sorty.groovy script

This is the whole script now

// PERSONALIZED SETTINGS
def episodeDir    = "G:/Media Files/Completed/TV"
def episodeFormat = "G:/Media Files/TV/{n}/Season {s}/{n} - {s00e00} - {t}"
def movieDir      = "G:/Media Files/Completed/Movies"
def movieFormat   = "G:/Media Files/Movies/{n} ({y})/{n} ({y})"
def animeDir      = "G:/Media Files/Completed/Anime"
def animeFormat   = "G:/Media Files/Anime/{n}/Season {s}/{n} - {s00e00} - {t}"

// XBMC ON LOCAL MACHINE
def xbmc = [] // (use [] to not notify any XBMC instances about updates)



// ignore chunk, part, par and hidden files
def incomplete(f) { f.name =~ /[.]incomplete|[.]chunk|[.]par$|[.]dat$/ || f.isHidden() }

//Remove Sample Video
[episodeDir, movieDir, animeDir].getFolders{ !it.hasFile{ incomplete(it) } && it.hasFile{ it.isVideo() } }.each{ dir ->
   println "Deleting Sample Video Processing $dir"
   def files = dir.listFiles{ it.isVideo() }
   // if name contains "sample" and filesize is less than 20 MB
   //def selection = args.getFiles{ it.name =~ "sample" && it.length() < 20 * 1024 * 1024}
   def selection = dir.getFiles{ (it.name =~ "sample" || it.name =~ "Sample") && it.length() < 500 * 1024 * 1024}
   selection.each{ println "delete $it" }

   // delete all
   selection*.delete()
}

// extract completed multi-volume rar files
[episodeDir, movieDir, animeDir].getFolders{ !it.hasFile{ incomplete(it) } && it.hasFile{ it =~ /[.]rar$/ } }.each{ dir ->
   // extract all archives found in this folder
   def paths = extract(folder:dir)
   
   // delete original archive volumes after successful extraction
   if (paths != null && !paths.isEmpty()) {
      dir.listFiles{ it =~ /[.]rar$|[.]r[\d]+$/ }*.delete()
   }
}


/*
 * Fetch subtitles and sort into folders
 */

//TV
episodeDir.getFolders{ !it.hasFile{ incomplete(it) } && it.hasFile{ it.isVideo() } }.each{ dir ->
   println "Processing $dir"
   def files = dir.listFiles{ it.isVideo() }
   
   // fetch subtitles
   //files += getSubtitles(file:files)
   
   // sort episodes / subtitles
   rename(file:files, db:'TVRage', format:episodeFormat)
}

//Movies
movieDir.getFolders{ !it.hasFile{ incomplete(it) } && it.hasFile{ it.isVideo() } }.each{ dir ->
   println "Processing $dir"
   def files = dir.listFiles{ it.isVideo() }
   
   // fetch subtitles
   //files += getSubtitles(file:files)
   
   // sort movies / subtitles
   rename(file:files, db:'TheMovieDB', format:movieFormat)
}

//Anime
animeDir.getFolders{ !it.hasFile{ incomplete(it) } && it.hasFile{ it.isVideo() } }.each{ dir ->
   println "Processing $dir"
   def files = dir.listFiles{ it.isVideo() }
   
   // fetch subtitles
   //files += getSubtitles(file:files)
   
   // sort episodes / subtitles
   rename(file:files, db:'anidb', format:animeFormat)
}


// make XBMC scan for new content
xbmc.each { host ->
   telnet(host, 9090) { writer, reader ->
      // API call for latest XBMC release
      def msg = '{"id":1,"method":"VideoLibrary.Scan","params":[],"jsonrpc":"2.0"}'
      
      // API call for XBMC Dharma-Release or older
      // def msg = '{"id":1,"method":"VideoLibrary.ScanForContent","params":[],"jsonrpc":"2.0"}'
            
      writer.println(msg)
   }
}


Now I having a problem with the extractor

I get this error
SevenZipNativeInitializationException: Failed to load 7z-JBinding: no mingwm10 in java.library.path


Posts: 12
Never mind about the sevenzip error I finally fixed it. I had an bad installation.


Return to Scripting and Automation