/* neon-fm - File management extensions to Neon Copyright (C) 2024 Ulrich Hilger This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ package de.uhilger.fm; import java.io.File; import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; /** * Eine Klasse mit Methoden zum Kopieren und Verschieben von Dateien * * @author Ulrich Hilger, 15. Janaur 2024 */ public class Mover extends FileHelper { public String copyOrMoveFiles(String fromPath, String toPath, String[] fileNames, int operation, String base) throws IOException { String result = null; File srcDir = new File(base, fromPath); File targetDir = new File(base, toPath); for (String fileName : fileNames) { File srcFile = new File(srcDir, fileName); //logger.fine("srcFile: " + srcFile); if (srcFile.isDirectory()) { //logger.fine("srcFile is directory."); OrdnerBearbeiter bearbeiter = new OrdnerBearbeiter(); bearbeiter.setTargetDir(targetDir.toPath()); bearbeiter.setOperation(operation); Files.walkFileTree(srcFile.toPath(), bearbeiter); } else { Path source = srcFile.toPath(); File destFile = targetDir.toPath().resolve(source.getFileName()).toFile(); if (destFile.exists()) { destFile = getNewFileName(destFile); } if (operation == Const.OP_MOVE) { String fname = srcFile.getName().toLowerCase(); if (fname.endsWith(Const.JPEG) || fname.endsWith(Const.JPG) || fname.endsWith(Const.PNG)) { moveImgFilesToDirectory(srcFile, srcDir, targetDir, false); } else { Files.move(source, destFile.toPath()); } } else { Files.copy(source, destFile.toPath()); } } } return result; } private void moveImgFilesToDirectory(File srcFile, File srcDir, File targetDir, boolean createDestDir) throws IOException { String fnameext = srcFile.getName(); int dotpos = fnameext.lastIndexOf("."); String fname = fnameext.substring(0, dotpos); String ext = fnameext.substring(dotpos); //logger.fine("fname: " + fname + ", ext: " + ext); Path targetPath = targetDir.toPath(); DirectoryStream stream = Files.newDirectoryStream(srcDir.toPath(), fname + "*" + ext); //"*.{txt,doc,pdf,ppt}" for (Path path : stream) { //logger.fine(path.getFileName().toString()); //Files.delete(path); Files.move(path, targetPath.resolve(path.getFileName())); } stream.close(); } }