如何根据文件名将文件移动到不同的目录? [英] How do I move files to different directories based upon filename?
问题描述
好的,我已经创建了一个程序,它将根据创建日期重命名目录中的文件。我现在需要能够根据创建日期将这些文件移动到不同的目录中 - 20131202-1.jpg将进入名为20131202的文件夹和名为20131203-2.jpg的文件到名为20131203,20131130-1的文件夹中。 jpg将进入名为20131130的文件夹 - 如果需要,创建目录等。有一种简单的方法吗?这是我用于重命名的代码:
Okay, I have created a program that will rename the files in a directory based upon creation date. I now need to be able to move these files into different directories based upon the creation date - 20131202-1.jpg will go into a folder called 20131202 and a file named 20131203-2.jpg into a folder called 20131203, 20131130-1.jpg would go into a folder called 20131130 - creating the directories if needed, etc. Is there a simple way to do that? This is the code I used for the rename:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.SimpleDateFormat;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class FileRenameAndMove extends JFrame implements ActionListener {
private JTextField filePath;
private JTextField outputFile;
private JButton input;
private JButton output;
private JButton rename;
JFileChooser chooser ;
File input_Folder = null;
File output_Folder = null;
public ExtraCredit(){
filePath = new JTextField();
outputFile = new JTextField();
chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
input = new JButton("select input Folder");
output = new JButton("select output Folder");
rename = new JButton("rename File");
setTitle("File Mover");
setLayout(null);
setVisible(true);
setSize(500,200);
filePath.setBounds(10,10,250,30);
outputFile.setBounds(10,50,250,30);
input.setBounds(280,10,150,30);
output.setBounds(280,50,150,30);
rename.setBounds(280, 90,150,30);
add(filePath);
add(outputFile);
add(input);
add(output);
add(rename);
input.addActionListener(this);
output.addActionListener(this);
rename.addActionListener(this);
public static void main(String[] args) {
new ExtraCredit();
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==input){
int option = chooser.showOpenDialog(this);
if (option == JFileChooser.APPROVE_OPTION) {
input_Folder=chooser.getSelectedFile();
filePath.setText(input_Folder.getAbsolutePath());
}
}
else if(e.getSource()==output){
int option = chooser.showOpenDialog(this);
if (option == JFileChooser.APPROVE_OPTION) {
output_Folder=chooser.getSelectedFile();
outputFile.setText(output_Folder.getAbsolutePath());
}
}
else if(e.getSource()==rename){
if(input_Folder==null||output_Folder==null){
JOptionPane.showMessageDialog(this,
"Please select the source and target folder",
"File error",
JOptionPane.ERROR_MESSAGE);
}
else{
if(input_Folder.exists()){
SimpleDateFormat dateFormat= new SimpleDateFormat("yyyyMMdd");
File[] fileList = input_Folder.listFiles();
for(int i=0;i<fileList.length;i++){
new File(output_Folder.getAbsolutePath()+"/"+dateFormat.format(fileList[i].lastModified())).mkdir();
fileList[i].renameTo(new File(output_Folder.getAbsolutePath()+"/"+dateFormat.format(fileList[i].lastModified())+"/"+fileList[i].getName()));
}
JOptionPane.showMessageDialog(this,
"Files renamed successfully!!",
"Information",
JOptionPane.INFORMATION_MESSAGE);
}
else
JOptionPane.showMessageDialog(this,
"Source folder doesn't exists",
"File error",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
如果您可以提供有关如何进行复制的示例,我将不胜感激。
If you can provide a sample on how to do the copying, I would appreciate it.
推荐答案
我写了一个方法来做到这一点。它将首先获取当前目录并收集其中所有文件+文件夹的列表(不是递归的)。然后,它将遍历内容并确保它是一个文件中包含 - ,在这种情况下,它将生成文件夹并移动文件。
I have a wrote a method to do this. It will first take the current directory and gather a list of all the files+folders in it (not recursive). Then, it will loop through the contents and make sure it is a file that has a "-" in it, in which case it will make the folder and move the file.
import java.io.*;
public class tmp {
public static void main(String[] args) throws IOException {
File folder = new File(System.getProperty("user.dir"));
File[] files = folder.listFiles();
for(int i = 0; i < files.length; i++) {
if(files[i].isFile()) {
String name = files[i].getName();
if(name.indexOf("-") != -1) {
System.out.println(name);
name = name.substring(0, name.indexOf("-"));
new File(System.getProperty("user.dir") + "/" + name + "/").mkdirs();
files[i].renameTo(new File(System.getProperty("user.dir") + "/" + name + "/" + files[i].getName()));
}
}
}
}
}
这篇关于如何根据文件名将文件移动到不同的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!