Groovy code to delete file based on string or size

Running FileBot from the console, Groovy scripting, shell scripts, etc
Post Reply
raulm753
Posts: 13
Joined: 30 Apr 2012, 16:47

Groovy code to delete file based on string or size

Post by raulm753 »

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.
User avatar
rednoah
The Source
Posts: 22923
Joined: 16 Nov 2011, 08:59
Location: Taipei
Contact:

Re: Groovy code to delete file based on string or size

Post by rednoah »

Somethink like this will do the job:

Code: Select all

// 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()
:idea: Please read the FAQ and How to Request Help.
raulm753
Posts: 13
Joined: 30 Apr 2012, 16:47

Re: Groovy code to delete file based on string or size

Post by raulm753 »

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

This is the whole script now

Code: Select all

// 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

Code: Select all

SevenZipNativeInitializationException: Failed to load 7z-JBinding: no mingwm10 in java.library.path
raulm753
Posts: 13
Joined: 30 Apr 2012, 16:47

Re: Groovy code to delete file based on string or size

Post by raulm753 »

Never mind about the sevenzip error I finally fixed it. I had an bad installation.
Post Reply