Groovy: بحث عن الملفات وفقًا للمعايير، نسخ التحديد
Publié le 19 July 2019
غرض هذا البرنامج النصي هو البحث عن الملفات،
من المجلد الأصل؛ استرداد الاختيار
في القائمة. أخيرًا، نسخ هذه القائمة من الملفات
هذا في جروفي في دليل الوجهة.
findCopyTo.groovy
#!/usr/bin/env groovy
@Grab("commons-io:commons-io:2.6")
import groovy.io.FileType
import org.apache.commons.io.FileUtils
/**
* un separateur OS independant
* @return
*/
static String getSep() {
System.getProperty("file.separator")
}
/**
* la liste des fichiers dont le titre contient un item
* de la liste des motifs
*
* @param path
* @param motifs
* @return la liste des chemins complet de fichiers
*/
static List<String> findFilesContainingMotifs(
String path,
List<String> motifs) {
List<String> filePathResultList = new ArrayList<>()
new File(path)
.eachFileRecurse(FileType.FILES) {
if (it.name.toLowerCase().contains('cucumber')) {
filePathResultList.add it.path
}
}
filePathResultList
}
/**
* copy vers la liste des fichiers
* @param files
* @param to
*/
static void copyFilesTo(List<String> files, String to) {
File toDirDest = new File(to)
files.each { String filepath ->
File file = new File(filepath)
//si le dernier caractere de to n'est pas un separateur
// alors ajoute le a la chaine
(to.substring(to.length() - 1) == sep) ?: to.concat(sep)
if (to + file.name != filepath) {
FileUtils.copyFileToDirectory(file, toDirDest)
file.delete()
}
}
}
/**
* la list des arguments
*/
String userName = System.getProperty("user.name")
List<String> motifs = ['cucumber']
String path = "/media/$userName/320/Books/"
String to = "/media/$userName/320/Books/bdd/"
/**
* lappel aux methodes
*/
copyFilesTo(findFilesContainingMotifs(path, motifs), to)
//affiche moi les fichiers déplacés
findFilesContainingMotifs(path, motifs).each {
println it
}