private void findByDate(String param){ SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy"); SimpleDateFormat df2 = new SimpleDateFormat("dd-MMM-yyyy"); LastModifiedFileComparator c = new LastModifiedFileComparator(); Date file = new Date(); String d = df.format(file); sorted.clear(); List<File> l1 = new ArrayList<>(); toSort.stream().filter((image) -> ( df2.format(new File(image).lastModified()).equals(d))).forEach((image) -> { l1.add(new File(image)); }); List<File> f = c.sort(l1); f.forEach(x ->{ sorted.add(x.getAbsolutePath()); }); }
/** * Keep only the 10 most recent resources packs, delete the others */ private void deleteOldServerResourcesPacks() { try { List<File> list = Lists.newArrayList(FileUtils.listFiles(this.dirServerResourcepacks, TrueFileFilter.TRUE, (IOFileFilter)null)); Collections.sort(list, LastModifiedFileComparator.LASTMODIFIED_REVERSE); int i = 0; for (File file1 : list) { if (i++ >= 10) { LOGGER.info("Deleting old server resource pack {}", new Object[] {file1.getName()}); FileUtils.deleteQuietly(file1); } } } catch (IllegalArgumentException illegalargumentexception) { LOGGER.error("Error while deleting old server resource pack : {}", new Object[] {illegalargumentexception.getMessage()}); } }
public File getTheNewestFile(File directory, String extension) { File newestFile = null; if (directory == null || !directory.exists() || !directory.isDirectory()) { return newestFile; } FileFilter fileFilter = new WildcardFileFilter("*." + extension); File[] files = directory.listFiles(fileFilter); if (files.length > 0) { Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE); newestFile = files[0]; } return newestFile; }
public ArrayList<FileItem> getFilesItemsInCurrentDirectory() { Operations op = Operations.getInstance(mContext); Constants.SORT_OPTIONS option = op.getmCurrentSortOption(); Constants.FILTER_OPTIONS filterOption = op.getmCurrentFilterOption(); if (mFileNavigator.getmCurrentNode() == null) mFileNavigator.setmCurrentNode(mFileNavigator.getmRootNode()); File[] files = mFileNavigator.getFilesInCurrentDirectory(); if (files != null) { mFiles.clear(); Comparator<File> comparator = NameFileComparator.NAME_INSENSITIVE_COMPARATOR; switch(option) { case SIZE: comparator = SizeFileComparator.SIZE_COMPARATOR; break; case LAST_MODIFIED: comparator = LastModifiedFileComparator.LASTMODIFIED_COMPARATOR; break; } Arrays.sort(files,comparator); for (int i = 0; i < files.length; i++) { boolean addToFilter = true; switch(filterOption) { case FILES: addToFilter = !files[i].isDirectory(); break; case FOLDER: addToFilter = files[i].isDirectory(); break; } if (addToFilter) mFiles.add(new FileItem(files[i])); } } return mFiles; }
private void func_183028_i() { List<File> list = Lists.newArrayList(FileUtils.listFiles(this.dirServerResourcepacks, TrueFileFilter.TRUE, (IOFileFilter)null)); Collections.sort(list, LastModifiedFileComparator.LASTMODIFIED_REVERSE); int i = 0; for (File file1 : list) { if (i++ >= 10) { logger.info("Deleting old server resource pack " + file1.getName()); FileUtils.deleteQuietly(file1); } } }
/** * This method is called when invoke <b>execute()</b>.<br /> * Do not invoke manually. Use: <i>new Daemon().execute("");</i> * * @param params blank string. * @return null if all is going ok. */ @Override protected String doInBackground(String... params) { while (folder.listFiles().length > 0) { // Select only files, no directory. File[] files = folder.listFiles((FileFilter) FileFileFilter.FILE); // Sorting following FIFO idea. Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE); if (files[0].getName().endsWith(".csv") || files[0].getName().endsWith(".txt")) { String commandsList = ""; try { commandsList = f.getStringFromFile(files[0].getPath()); } catch (Exception e) { e.printStackTrace(); } Log.e(TAG, "Lista comandi: " + commandsList); Interpreter in = new Interpreter(deviceController); in.doListCommands(commandsList); files[0].delete(); } else { Log.e(TAG, "Error: There is no csv|txt files or is not a file but a directory."); } } return null; }
/** * Find latest file by last modified. * * @param fileNamePattern the file name pattern * @param directory the directory * @param recursive the recursive * @return the string */ public static String findLatestFileByLastModified( final String directory, final String fileNamePattern, final boolean recursive ) { // check that file name is not null or blank and is a valid pattern if ( StringUtils.isBlank( fileNamePattern ) || !FileUtil.isValidPattern( fileNamePattern ) ) { logger.warn( "In findFileByLastModified, file name '{}' is either null or blank or is not a valid pattern.", fileNamePattern ); return null; } // check that directory is not null or empty (string) and exists. if ( !FileUtil.isDirExists( directory ) ) { logger.warn( "In findLatestFileByLastModified, directory '{}' is either null or empty or does not exist.", directory ); return null; } // find files matching pattern in the directory File[] files = FileUtil.findFiles( directory, fileNamePattern, recursive ); if ( files == null || files.length == 0 ) { logger.warn( "In findLatestFileByLastModified, no files found matching pattern '{}' in directory '{}'.", fileNamePattern, directory ); return null; } // Sort the array to get the newest file come first Arrays.sort( files, LastModifiedFileComparator.LASTMODIFIED_REVERSE ); String fileName = files[0].getAbsolutePath(); logger.debug( "In findLatestFileByLastModified, latest file in the directory '{}' " + "matching the file name pattern '{}' is '{}'.", directory, fileNamePattern, fileName ); return fileName; }
@HandlesEvent("listWatchedFolders") public Resolution listWatchedFolder() { ArrayList<HashMap<String, String>> folders = new ArrayList<HashMap<String, String>>(); // CHECK THIS Does import have / will have sub directories, or just ingest dir? String watchedDir = this.getIngestDirectory(true); File dir = new File(watchedDir); HashMap<String, String> root = new HashMap<String, String>(); root.put("name", "ROOT"); folders.add(root); int rootcount = 0; if (!dir.exists()) { folders.get(0).put("count", String.valueOf(rootcount)); return new StreamingResolution(MIME_JS, new flexjson.JSONSerializer().serialize(folders)); } File[] folderList = dir.listFiles(); // Sort folders by their modification date, descending order Arrays.sort(folderList, LastModifiedFileComparator.LASTMODIFIED_REVERSE); for (File file : folderList) { if (file.isDirectory()) { HashMap<String, String> entry = new HashMap<String, String>(); entry.put("name", file.getName()); int count = file.listFiles().length; entry.put("count", String.valueOf(count)); folders.add(entry); } else { rootcount++; } } folders.get(0).put("count", String.valueOf(rootcount)); return new StreamingResolution(MIME_JS, new flexjson.JSONSerializer().serialize(folders)); }
/** * Returns the latest annotation file in a directory. * * @param directory directory to search for annotation files * @return Last modified annotation file or null if no annotation file * exists in the directory * @throws IOException When reading the directory fails. */ public static File getLatestAnnotationOutputFileInDirectory(File directory) throws IOException { File[] filesInDir = Helper.getAnnotationFilesInDirectory(directory); Arrays.sort(filesInDir, LastModifiedFileComparator.LASTMODIFIED_REVERSE); if (filesInDir.length > 0) { return filesInDir[0]; } return null; }
public static void saveJSON(File file, Object toSave, boolean isClient, boolean backups) { try { if (Options.debugMode) Progression.logger.log(Level.INFO, "Writing to the file is being done at saveJSON"); //Make a backup if (Options.enableCriteriaBackups && backups) { File backup = FileHelper.getBackupFile(serverName, isClient); FileUtils.copyFile(file, backup); File[] files = FileHelper.getBackup().listFiles(); if (files.length > Options.maximumCriteriaBackups) { Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR); for (File filez : files) { filez.delete(); break; } } } Writer writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8"); writer.write(getGson().toJson(toSave)); writer.close(); } catch (Exception e) { e.printStackTrace(); } if (Options.debugMode) Progression.logger.log(Level.INFO, "Saved JSON at " + System.currentTimeMillis()); }
/** * Gets lines from the log file which corresponds with specified correlation ID. * * @param correlationId the correlation ID * @param logDate which date to search log files for * @return log lines * @throws IOException when error occurred during file reading */ List<String> getLogLines(String correlationId, Date logDate) throws IOException { File logFolder = new File(logFolderPath); if (!logFolder.exists() || !logFolder.canRead()) { throw new FileNotFoundException("there is no readable log folder - " + logFolderPath); } final String logDateFormatted = fileFormat.format(logDate); // filter log files for current date IOFileFilter nameFilter = new IOFileFilter() { @Override public boolean accept(File file) { return logNameFilter.accept(file) && (StringUtils.contains(file.getName(), logDateFormatted) || file.getName().endsWith(BASE_FILE_EXTENSION)); } @Override public boolean accept(File dir, String name) { return StringUtils.contains(name, logDateFormatted) || name.endsWith(BASE_FILE_EXTENSION); } }; List<File> logFiles = new ArrayList<File>(FileUtils.listFiles(logFolder, nameFilter, null)); Collections.sort(logFiles, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR); // go through all log files List<String> logLines = new ArrayList<String>(); for (File logFile : logFiles) { logLines.addAll(getLogLines(logFile, correlationId)); } return logLines; }
public File[] getLogFiles(final DateTime date) throws FileNotFoundException { File logFolder = new File(logFolderPath); if (!logFolder.exists() || !logFolder.canRead()) { throw new FileNotFoundException("there is no readable log folder - " + logFolderPath); } final String logDateFormatted = FILE_DATE_FORMAT.print(date); final long dateMillis = date.getMillis(); File[] files = logFolder.listFiles(new FileFilter() { @Override public boolean accept(File file) { String name = file.getName(); return name.endsWith(FILE_EXTENSION) // it's a log file && name.contains(logDateFormatted) // it contains the date in the name && file.lastModified() >= dateMillis; // and it's not older than the search date } }); Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR); if (files.length == 0) { Log.debug("No log files ending with {}, containing {}, modified after {}, at {}", FILE_EXTENSION, logDateFormatted, date, logFolderPath); } else { Log.debug("Found log files for {}: {}", date, files); } return files; }
private File executeLocator(File reportsDirectory, Log log) { File[] subdirectories = reportsDirectory .listFiles(TIMESTAMPED_REPORTS_FILE_FILTER); File latest = reportsDirectory; log.debug("ReportSourceLocator starting search in directory [" + reportsDirectory.getAbsolutePath() + "]"); if (subdirectories != null) { LastModifiedFileComparator c = new LastModifiedFileComparator(); for (File f : subdirectories) { log.debug("comparing directory [" + f.getAbsolutePath() + "] with the current latest directory of [" + latest.getAbsolutePath() + "]"); if (c.compare(latest, f) < 0) { latest = f; log.debug("directory [" + f.getAbsolutePath() + "] is now the latest"); } } } else { throw new PitError("could not list files in directory [" + reportsDirectory.getAbsolutePath() + "] because of an unknown I/O error"); } log.debug("ReportSourceLocator determined directory [" + latest.getAbsolutePath() + "] is the directory containing the latest pit reports"); return latest; }
private void cleanupRepositories() throws Exception { RepositoryManager rm = (RepositoryManager) servletContext.getAttribute("repositoryManager"); RepositoryManager backgroundIndexingRepositoryManager = null; if (isBackgroundIndex) backgroundIndexingRepositoryManager = getBackgroundRepositoryManager(); File repositoryBaseDir = new File(servletContext.getInitParameter("repositoryData")); File[] repositoryDirectories = repositoryBaseDir.listFiles(); if(repositoryDirectories!=null && repositoryDirectories.length!=0) { Arrays.sort(repositoryDirectories, LastModifiedFileComparator.LASTMODIFIED_REVERSE); int succesfullBackupCount = 0; int errorBackupCount = 0; for(File repositoryDirectory:repositoryDirectories) { // If the file is not a director and does not start with the prefix repository_, then its not a // repos. If the directory we are looking at is the current repository or the background don't count // those either. if(!repositoryDirectory.isDirectory() || !repositoryDirectory.getName().startsWith("repository_")|| repositoryDirectory.getAbsolutePath().equals(rm.getRepositoryDirectory().getAbsolutePath())|| (backgroundIndexingRepositoryManager!=null && repositoryDirectory.getAbsolutePath().equals( backgroundIndexingRepositoryManager.getRepositoryDirectory().getAbsolutePath())) ) continue; // Otherwise its a backup, now figure out if its a valid repos or invalid, // If the repos doesn't have a summary, might be a legacy repository treat it as // a successful one IndexDataBean summary = rm.getIndexSummary(repositoryDirectory.getAbsoluteFile()); if(summary==null || summary.getIndexStatus().equals(IndexDataBean.STATUS_VALID)) { if(succesfullBackupCount<NUM_REPO_BACKUPS) { succesfullBackupCount++; continue; } } else { // Ignored statuses are considered errored backups if(errorBackupCount<NUM_ERROR_REPO_BACKUPS) { errorBackupCount++; continue; } } FileUtils.deleteDirectory(repositoryDirectory); } } }
public void importRepositories() throws Exception { RepositoryManager repositoryManager = (RepositoryManager) servletContext.getAttribute("repositoryManager"); File repositoryBaseDir = new File(servletContext.getInitParameter("repositoryData")); File firstAddedRepository = null; if(importRepositoryDestinationString!=null) { for(String destination:importRepositoryDestinationString.split(",")) { File destinationDirecotry = new File(destination); if(!destinationDirecotry.exists() || !destinationDirecotry.isDirectory()) { prtlnErr(destination+ " is not a valid directory that can hold repositories."); continue; } File[] possibleRepositories = new File(destination).listFiles(); Arrays.sort(possibleRepositories, LastModifiedFileComparator.LASTMODIFIED_REVERSE); for(File possibleRepository:possibleRepositories) { if(!possibleRepository.isDirectory() || !possibleRepository.getName().startsWith("repository_")) continue; IndexDataBean summary = repositoryManager.getIndexSummary(possibleRepository); if(summary!=null && summary.getIndexStatus().equals(IndexDataBean.STATUS_VALID)) { prtln(String.format("External repository %s was found at %s and is being copied into a local directory.", possibleRepository.getName(), destination)); FileUtils.moveDirectoryToDirectory(possibleRepository, repositoryBaseDir, false); // We need to change the index path to point the the new location, set the new one // and write it File newRepository = new File(repositoryBaseDir, possibleRepository.getName()); summary.getIndexDetail().put(IndexDataBean.INDEX_PATH, newRepository.getAbsolutePath()); repositoryManager.writeIndexSummary(summary, newRepository); if(firstAddedRepository==null) firstAddedRepository = newRepository; } else { // if its not a valid index or if it doesn't have a summary get rid of it FileUtils.deleteDirectory(possibleRepository); } } } } if(firstAddedRepository!=null) { prtln(String.format("Making imported repository %s current.", firstAddedRepository.getName())); // Only set it at live if the option for makeImportedRepositories is true if(getRepositoryInfoBean().isMakeImportedRepositoriesCurrentRepository()) this.setCurrentRepository(firstAddedRepository.getAbsolutePath(),false, true); // To make sure a repository that is never turned off nor does its own indexing. // gets to many indexes loaded we call cleanup to keep the file disk usage down this.cleanupRepositories(); } }
private String getEducationLevelsVsAlignmentsConcat(int dayPadding) throws MalformedURLException, DocumentException { File fileStorageDirectory = new File( Config.BASE_FILE_PATH_STORAGE); ArrayList<String> xmlStrings = new ArrayList<String>(); String header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; xmlStrings.add(header); xmlStrings.add("<collections>"); int collectionCount = 0; int recordCount = 0; // Loop through all the collections. for(File collectionDirectory: fileStorageDirectory.listFiles()) { File[] sessionDirectories = collectionDirectory.listFiles(); if(sessionDirectories!=null && sessionDirectories.length==0) continue; // We only want the latest session Arrays.sort(sessionDirectories, LastModifiedFileComparator.LASTMODIFIED_REVERSE); File lastSessionDirectory = sessionDirectories[0]; File collectionEdLevelsReport = new File(lastSessionDirectory, String.format(EducationLevelsVsAlignmentsReport.REPORT_FILE_NAME_FORMAT, dayPadding)); // If it doesn't have a report don't do anything if(!collectionEdLevelsReport.exists()) continue; Document collectionDocument = Dom4jUtils.getXmlDocument( collectionEdLevelsReport); // If the record count was not included in the collection report just use 0. // This will be the case for reports written before it was added. int collectionRecordCount = 0; try { collectionRecordCount = Integer.parseInt(collectionDocument.getRootElement().attributeValue("record_count")); } catch(Exception e) { collectionRecordCount= 0; } collectionCount++; recordCount += collectionRecordCount; String xmlString = collectionDocument.asXML().replace(header, ""); xmlStrings.add(xmlString); } xmlStrings.add("</collections>"); Document completeXmlDocument = Dom4jUtils.getXmlDocument( StringUtils.join(xmlStrings, "\n")); Element collectionsElement = completeXmlDocument.getRootElement(); // Finally add the collection_count and record_count to the collections element collectionsElement.addAttribute("collection_count", Integer.toString(collectionCount)); collectionsElement.addAttribute("record_count", Integer.toString(recordCount)); return Dom4jUtils.prettyPrint(completeXmlDocument).replace(header, ""); }
/** * Cleanup method that insures that the files don't get out of hand. * In the end we want the the number of success sessions that is defined * in the Config file and number of failure sessions. A session that wasn't * cleaned up for some reason will have a success of <success/> which is * considered a failure */ protected void cleanPastSessions() { if(!this.collectionDirectory.exists()) return; File[] sessionDirectories = this.collectionDirectory.listFiles(); if(sessionDirectories!=null && sessionDirectories.length==0) return; Arrays.sort(sessionDirectories, LastModifiedFileComparator.LASTMODIFIED_REVERSE); int failureCount = 0; int successCount = 0; boolean nonSourceHarvest = false; int nonSourceHarvestCount = 0; for(File sessionDirectory:sessionDirectories) { String workspaceStatus = Workspace.getStatus(sessionDirectory); String harvestProtocol = Workspace.getProtocol(sessionDirectory); // We only want 1 non source harvest and for sources we keep // whatever the max storage sessions is set for if(harvestProtocol.equals(Config.Protocols.HARVESTED_RECORDS_DB)) nonSourceHarvest = true; else nonSourceHarvest = false; if(nonSourceHarvest && nonSourceHarvestCount==0) { nonSourceHarvestCount++; continue; } if(!nonSourceHarvest && successCount<Config.Reporting.MAX_STORAGE_OF_SUCCESS_SESSIONS && workspaceStatus.equals(SUCCESS_RESULT)) { successCount++; continue; } else if(!nonSourceHarvest && failureCount<Config.Reporting.MAX_STORAGE_OF_FAILURE_SESSIONS && !workspaceStatus.equals(SUCCESS_RESULT)) { failureCount++; continue; } // This is a just in case. The current session should be the very first // one so either should be used as the success or failure. Just // to make sure for some odd reason, we don't want to delete it if(!sessionDirectory.equals(this.workingFileDirectory)) { try { FileUtils.deleteDirectory(sessionDirectory); } catch (IOException e) { e.printStackTrace(); } } } }
/** * Static helper method to determine when a directory or its content has been modified last time. * * @param directory * {@link File} * @return last modification date {@link DateTime} */ public static final DateTime getLastModified(File directory) { List<File> files = new ArrayList<File>(FileUtils.listFiles(directory, null, true)); Collections.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE); return new DateTime(files.get(0).lastModified()); }