/*
* HelloNzb -- The Binary Usenet Tool
* Copyright (C) 2010-2013 Matthias F. Brandstetter
* https://sourceforge.net/projects/hellonzb/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package me.mabra.hellonzbtest;
import org.apache.commons.io.FileUtils;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.IOException;
public class HelloNzbTester
{
private final String sourceFolderName = "S:\\tmp\\test";
private final String targetFolderName = "M:\\test";
public static void main(String [] args)
{
try
{
new HelloNzbTester();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public HelloNzbTester() throws IOException
{
if(targetFolderName == null || targetFolderName.isEmpty())
return;
File targetFolder = new File(targetFolderName);
if(!targetFolder.isDirectory() || !targetFolder.canRead() || !targetFolder.canExecute())
{
System.err.println("Could not move extracted files, invalid target directory");
return;
}
File sourceFolder = new File(sourceFolderName);
if(!sourceFolder.isDirectory() || !sourceFolder.canRead() || !sourceFolder.canExecute())
{
System.err.println("Could not move extracted files, invalid source directory");
return;
}
// check file systems of source and target folders
boolean isSameFS;
try
{
isSameFS = isSameFileSystem(sourceFolder, targetFolder);
}
catch(IOException e)
{
e.printStackTrace();
return;
}
// check all files in data directory
File [] files = sourceFolder.listFiles();
for(File file : files)
{
System.out.println("Moving '" + file.getCanonicalPath() + "' to '" + targetFolderName + "'");
FileUtils.moveToDirectory(file, targetFolder, false);
}
}
private boolean isSameFileSystem(File source, File target) throws IOException
{
// check file systems of source and target folders
FileSystemView fsv = FileSystemView.getFileSystemView();
File [] roots = File.listRoots();
File sourceFS = null;
File targetFS = null;
for(int i = 0; i < roots.length; i++)
{
if(source.getCanonicalPath().startsWith(roots[i].getCanonicalPath()))
sourceFS = roots[i];
if(target.getCanonicalPath().startsWith(roots[i].getCanonicalPath()))
targetFS = roots[i];
}
// compare both file systems
if(sourceFS == null || targetFS == null)
throw new IOException("Failed to determine the file systems of source and target folders");
return sourceFS.equals(targetFS);
}
}