I am a beginner in Java trying to work with Files and Directories. I wanted to create a program where I could change file names automatically while searching through all the child directories for file names that are not valid. I am actually trying to load a huge amount of files on to a server but the server settings do not allow file names containing special characters. To start with I was able to write the code where if I pass the path to a directory it renames all the files with invalid names in that directory:
public class reNaming {
public static String baseLoc = "C:/Users/Developer/Desktop/.../Data Cleanup";
public static void main(String[] args) {
//LinkedList<File> fileList = new LinkedList<File>();
File obj = new File(baseLoc);
int count = 0;
for (File file: obj.listFiles())
{
String origName = file.getName();
if (origName.contains("&") || origName.contains("#") || origName.contains("#"))
{
System.out.println("Original name: "+origName);
origName = origName.replaceAll("&", "_and_");
origName = origName.replaceAll("#", "_at_");
String newName = origName.replaceAll("#", "_");
System.out.println("New Name: "+newName);
String newLoc = baseLoc+"/"+newName;
File newFile = new File(newLoc);
System.out.println(file.renameTo(newFile));
count++;
}
}
}
}
Now I want to do the same but only this time I want all the files to be reNamed even in the child directories. Can somebody please guide me how I can achieve that?
Recursion is your friend
/**Removes 'invalid' characters (&,#,#) from pathnames in the given folder, and subfolders, and returns the number of files renamed*/
public int renameDirectory(File base){
//LinkedList<File> fileList = new LinkedList<File>();
int count=0;//count the renamed files in this directory + its sub. You wanted to do this?
//Process each file in this folder.
for (File file: base.listFiles()){
String origName = file.getName();
File resultFile=file;
if (origName.contains("&") || origName.contains("#") || origName.contains("#")){
//I would replace the if statement with origName.matches(".*[&##].*") or similar, shorter but more error prone.
System.out.println("Original name: "+origName);
origName = origName.replaceAll("&", "_and_");
origName = origName.replaceAll("#", "_at_");
String newName = origName.replaceAll("#", "_");
System.out.println("New Name: "+newName);
String newLoc = baseLoc+File.separator+newName;//having "/" hardcoded is not cross-platform.
File newFile = new File(newLoc);
System.out.println(file.renameTo(newFile));
count++;
resultFile=newFile;//not sure if you could do file=newFile, tired
}
//if this 'file' in the base folder is a directory, process the directory
if(resultFile.isDirectory()){//or similar function
count+=renameDirectory(resultFile);
}
}
return count;
}
Move the code you have to a utility method (e.g. public void renameAll(File f){}). Have a condition that checks if the file is a directory and recursively call your method with it's contents. After that do what you are currently doing.
public void renameAll(File[] files){
for(File f: files){
if(f.isDirectory){
renameAll(f.listFiles());
}
rename(f);
}
}
public void rename(File f){ }
Related
I have an arrayList "rules" containing Rules. Each Rule is an XML file and have some properties such as filename...
I want to copy the Rules from the arraylist to a folder named AllMRG. I tried the code between comments but I get the message "Source 'RG6.31.xml' does not exist".
I changed the code by the following, but there is still a problem with 'RG6.31.xml' and the folder AllMRG is empty even though the arrayList contains many Rules!
First attemption:
File AllMRGFolder = new File("AllMRG");
for(int p = 0; p < rules.size(); p++) {
/* File MRGFile = new File(rules.get(p).fileName);
FileUtils.copyFileToDirectory(MRGFile, AllMRGFolder); */
File MRGFile = new File("AllMRG/" + rules.get(p).fileName);
if (!MRGFile.exists()) {
FileUtils.copyFileToDirectory(MRGFile, AllMRGFolder);
}
}
Second attemption:
String path = "AllMRG";
for(Rule rule : rules) {
File MRGFile = new File(rule.fileName);
Files.copy(MRGFile.toPath(), (new File(path + MRGFile.getName())).toPath(), StandardCopyOption.REPLACE_EXISTING);
}
PS: Rule is a class
public class Rule implements Comparable{
public String fileName;
public String matches;
public String TPinstances;
public int nbrOfMatches;
public double T;
#Override
public int compareTo(Object o) {
if(o instanceof Rule){
//processing to compare one Rule with another
}
return 0;
}
}
Here is the entire code after having considered Shyam's answer. The same problem persists!
Path directoryPath = Files.createDirectory(Paths.get("AllMGR"));
for(Rule rule : rules) {
Path filePath = directoryPath.resolve(rule.fileName);
Files.createFile(filePath);
File MRGFile = new File(rule.fileName);
String ruleContent = new String(Files.readAllBytes(Paths.get(MRGFile.getPath())));
String fileContent = new String(Files.readAllBytes(filePath));
fileContent=ruleContent;
PrintWriter out13= new PrintWriter("AllMGR/"+rule.fileName+".xml");
out13.print(fileContent);
out13.close();
}
Firstly, you are creating a new File with rule.filename without giving any predefined path. Then, you are building a path like: path + MRGFile.getName() without any path delimiters and trying to copy the file to this location. I don't think this will work.
What can actually help you is, creating a base directory first and then creating individual files in it.
Create base directory:
Path directoryPath = Files.createDirectory(Paths.get("AllMGRDir"));
Then for each of your Rule object you can crate file using:
for(Rule rule : rules) {
Path filePath = directoryPath.resolve(rule.fileName());
Files.createFile(filePath);
// your remaining code
}
The resolve(String other) method resolves the given path. Java doc says that:
Converts a given path string to a Path and resolves it against this
Path in exactly the manner specified by the resolve(Path) method.
For example, suppose that the name separator is "/" and a path
represents "foo/bar", then invoking this method with the path string
"gus" will result in the Path "foo/bar/gus"
Hope this helps.
I am creating an application that automatically sorts and organizes files into a database. I have written my code to read files within the imported folder one at a time, and process them into the DB. However, I am having trouble looping this process, so that I can process files that are nested in any amount of folders within the original folder that the user wants to input.
I simply need to instruct my program to go back to a specific part of my code and start running from there again.
Another possible way to solve this issue would be to create a way to list out all of the individual files within folder (including all the files within subfolders), and I could easily fit that into my program too.
I tried using labeled continue, return, and break keywords based off of an answer I got online, but I never expected those to succeed in looping my code back to a specific spot.
JFileChooser chooser = new JFileChooser();
chooser.setSelectedFiles(null);
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
chooser.showOpenDialog(null);
//Getting file paths from within folder
File f = chooser.getSelectedFile();
String file = f.getAbsolutePath();
if (f.isDirectory()) {
//Need to loop back to here
File folder = new File(file);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isDirectory()) {
//Code here is run if there is a folder within a folder. I tested it too
//I want the code here to loop back above where it says "Need to loop back to here"
}
if (listOfFiles[i].isFile()) { //Once I list the files from within the folder, their information gets assigned variable here, and the rest of my program sorts it and saves it to DB accordingly.
//Everything below here is not important, but it might be helpful to see what happens each file with the folders.
System.out.println(listOfFiles[i]);
String filename = (listOfFiles[i].getName()); //For Files
Long filemodified = (listOfFiles[i].lastModified());
String filepath = (listOfFiles[i].getAbsolutePath());
Long filesizeraw = (listOfFiles[i].length());
long filehashcode = (listOfFiles[i].hashCode());
String fileparent = (listOfFiles[i].getParent());
Currently, there is no error message. It would process any individual files directly in the imported file (not nested in any folder within the folder), but wouldn't get to any of the files that are in folders within folders.
Another possible way to solve this issue would be to create a way to list out all of the individual files within folder (including all the files within subfolders), and I could easily fit that into my program too
Although this doesn't do the SQLite inserts, the following class extracts a list (of File objects) the files (thus file name and path are available via the File object).
public class FTS {
private ArrayList<File> mFileList; //Resultant list of Files extracted
private String mBaseDirectory; // The Directory to search
private long mSubDirectoryCount; // The count of the subdirectories
//Constructor
public FTS(String directory) {
this.mBaseDirectory = directory;
this.mSubDirectoryCount = 0;
buildFileListing(this.mBaseDirectory);
}
//
private void buildFileListing(String directory) {
// Initialise the ArrayList for the result
if (mFileList == null) {
mFileList = new ArrayList(){};
}
//Get the File (directory to process)
File dir = new File(directory);
// Get the List of the Directories contents
String[] filelist = dir.list();
// If empty (null) then return
if (filelist == null) {
return;
}
// Loop through the directory list
for (String s: filelist) {
//get the current list item as a file
File f = new File(dir.getAbsolutePath() + File.separator + s);
// is it a file or directory?
if (f.isFile() && !f.isDirectory()) {
this.mFileList.add(f); // If a file then add the file to the extracted list
} else {
// If a directory then increment the count of the subdirectories processed
mSubDirectoryCount++;
// and then recursively call this method to process the directory
buildFileListing(f.getAbsolutePath());
}
}
}
// return the list of extracted files
public ArrayList<File> getFileList() {
return this.mFileList;
}
// return the number of sub-directories processed
public long getSubDirectoryCount() {
return this.mSubDirectoryCount;
}
}
An example usage of the above is :-
public class Main {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
FTS fileTreeSearch;
String BaseDirectory = "E:" + File.separator;
List<File> files = (fileTreeSearch = new FTS(BaseDirectory)).getFileList();
System.out.println("Extracted " + String.valueOf(files.size()) + " files, from " + String.valueOf(fileTreeSearch.getSubDirectoryCount()) + " sub-directories of " + BaseDirectory);
/* this commented out code would process all the extracted files
for (File f: files) {
System.out.println("File is " + f.getName() + "\t\t path " + f.getAbsolutePath());
}
*/
}
}
Example output from running the above :-
Extracted 186893 files, from 54006 sub-directories of E:\
I am trying to create a program that uploads multiple files and stores their name and BPM tag into an ArrayList ready for comparison between the files. I have found two functions to help me but I am unable to combine them to get the function that I need.
The first function takes a singular mp3 file and outputs its data into the console (using mp3agic library):
File file = new File(dataPath("") + "/Song.mp3");
Mp3File mp3file = new Mp3File(file.getPath());
if (mp3file.hasId3v2Tag()) {
ID3v2 id3v2Tag = mp3file.getId3v2Tag();
println("Track: " + id3v2Tag.getTrack());
println("Artist: " + id3v2Tag.getArtist());
println("BPM: " + id3v2Tag.getBPM());
println("Album artist: " + id3v2Tag.getAlbumArtist());
}
The second function takes a data path and outputs the directory containing the names and info of the files in the folder
void setup() {
String path = "Desktop/mp3folder";
println("Listing all filenames in a directory: ");
String[] filenames = listFileNames(path);
printArray(filenames);
println("\nListing info about all files in a directory: ");
File[] files = listFiles(path);
for (int i = 0; i < files.length; i++) {
File f = files[i];
println("Name: " + f.getName());
println("Is directory: " + f.isDirectory())
println("-----------------------");
}
}
// This function returns all the files in a directory as an array of Strings
String[] listFileNames(String dir) {
File file = new File(dir);
if (file.isDirectory()) {
String names[] = file.list();
return names;
} else {
// If it's not a directory
return null;
}
}
// This function returns all the files in a directory as an array of File objects
// This is useful if you want more info about the file
File[] listFiles(String dir) {
File file = new File(dir);
if (file.isDirectory()) {
File[] files = file.listFiles();
return files;
} else {
// If it's not a directory
return null;
}
}
The function I am trying to create combines the two. I need the Artist, Track and BPM from the first function to work with an array list of files from a directory.
Any guidance would be appreciated. Any advice on another way to go about it would also be appreciated.
One way to approach this is to use classes to encapsulate the data you want to track.
For example, here's a simplified class that contains information about artist, track, and bpm:
public class TrackInfo{
private String artist;
private String track;
int bpm;
}
I would also take a step back, break your problem down into smaller steps, and then take those pieces on one at a time. Can you create a function that takes a File argument and prints out the MP3 data of that File?
void printMp3Info(File file){
// print out data about file
}
Get that working perfectly before moving on. Try calling it with hard-coded File instances before you try to use it with an ArrayList of multiple File instances.
Then if you get stuck, you can post a MCVE along with a specific technical question. Good luck.
Hello I'm trying to get with numerical order all files that exist in a folder.
First i check a folder(main folder) the sub-folders that contained in. After that, for each sub-folder I'm getting the names of the files that exist in.
I wrote a code that does this job for me but when i print (the names of the files of a sub-folder) I'm not getting them with the correct order.(numerical).
For example i have my main Folder called 'test', in there exist 3 sub-folder named Sub1, Sub2, Sub3
FOLDER Test *contains* [ FOLDER SUB1 || FOLDER SUB2 || FOLDER SUB3 ]
Each sub-Fodler have files with names (1.txt , 2.txt ,.....,15.txt,16.txt,...,22.txt,...etc)
This code does this job... but...
import java.io.File;
import java.io.FileFilter;
public class Main {
public static void main(String[] args) {
File file = new File("C:\\test");
File[] files = file.listFiles(new FileFilter() {
public boolean accept(File f) {
String name=f.getName(); //read every subfodler name
System.out.println(name);
File folder = new File("C:\\test\\"+name); //for each subfolder
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println("File " + listOfFiles[i].getName());
//print the names of files that included
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory " + listOfFiles[i].getName());
}
}
return f.isDirectory();
}});
}
}
but the output is like that...
Folder1
File 1.txt
File 10.txt
File 11.txt
File 12.txt
File 13.txt
File 14.txt
File 2.txt
File 3.txt
File 4.txt
File 5.txt
File 6.txt
File 7.txt
File 8.txt
File 9.txt
Folder2
............... same order as Folder1 ... etc
How i could taking the file names with numerical order so the output that i will get will be like that:
Folder1
File 1.txt
File 2.txt
File 3.txt
File 4.txt
File 5.txt
File 6.txt
File 7.txt
File 8.txt
File 9.txt
File 10.txt
File 11.txt
File 12.txt
File 13.txt
File 14.txt
Once you have the files in a list or array you can sort them using Collections.sort() or Arrays.sort() and a custom comparator like in this example
java.io.File is an obsolete class. It was replaced years ago by Path and other classes in the java.nio.file package, like Files and Paths.
You should not use a filter to perform any actions other than filtering. Yes, it works, but you’re relying on side effects instead of the intended functionality.
For comparing the numeric part of a filename, you will need to write your own Comparator:
Path root = Paths.get("C:\\test");
Pattern numericFileNamePattern = Pattern.compile(".*[^0-9]([0-9]+)\\.[^.]+");
Comparator<Path> byNumber = (p1, p2) -> {
String name1 = p1.getFileName().toString();
String name2 = p2.getFileName().toString();
Matcher matcher = numericFileNamePattern.matcher(name1);
if (matcher.matches()) {
String digits1 = matcher.group(1);
String withoutDigits1 = matcher.replaceFirst("");
matcher.reset(name2);
if (matcher.matches()) {
String digits2 = matcher.group(1);
String withoutDigits2 = matcher.replaceFirst("");
if (withoutDigits1.equals(withoutDigits2)) {
long num1 = Long.parseLong(digits1);
long num2 = Long.parseLong(digits2);
return Long.compare(num1, num2);
}
}
}
return p1.compareTo(p2);
};
try (DirectoryStream<Path> dir = Files.newDirectoryStream(root)) {
for (Path child : dir) {
if (Files.isDirectory(child)) {
try (Stream<Path> listing = Files.list(child)) {
Path[] files = listing.filter(f -> Files.isRegularFile(f))
.sorted(byNumber).toArray(Path[]::new);
for (Path file : files) {
System.out.println(file);
}
}
}
}
}
I'm writing a program that does various data analysis functions for use with Excel.
I need a way of returning file names of documents so I can search through them and find the ones I want.
I need to be able to take a string, saved as a variable, and use it to return the name of every document in a folder whose file name contains that string.
This will be used to sift through pre-categorized sections of data. Ideally I would save those documents' file names in a string array for later use within other functions.
private List<String> searchForFileNameContainingSubstring( String substring )
{
//This is assuming you pass in the substring from input.
File file = new File("C:/Users/example/Desktop"); //Change this to the directory you want to search in.
List<String> filesContainingSubstring = new ArrayList<String>();
if( file.exists() && file.isDirectory() )
{
String[] files = file.list(); //get the files in String format.
for( String fileName : files )
{
if( fileName.contains( substring ) )
filesContainingSubstring.add( fileName );
}
}
for( String fileName : filesContainingSubstring )
{
System.out.println( fileName ); //or do other operation
}
return filesContainingSubstring; //return the list of filenames containing substring.
}
Using this method, you could pass in the input from the user as the string you want the filename to contain. The only other thing you need to change is where you want in your directory to start searching for files, and this program only looks in that directory.
You could further look recursively within other directories from the starting point, but I won't add that functionality here. You should definitely look into it though.
This also assumes that you are looking for everything within the directory, including other folders and not just files.
You can get the list of all the files in a directory and then store them in an array. Next, using the java.io.File.getName() method, you can get the names of the files. Now you can simply use the .indexOf() method to check whether the string is a substring of the file name. I assume that all the items in the directory of concern are files and not sub directories.
public static void main(String[] args) throws IOException {
File[] files = new File("X:/").listFiles(); //X is the directory
String s <--- the string you want to check filenames with
for(File f : files){
if(f.getName().toLowerCase().indexOf(s.toLowerCase()) != -1)
System.out.println(f.getName());
}
}
This should display the names of all those files in the directory X:\ whose names include the String s.
References
This question: How do I iterate through the files in a directory in Java?
The java.io.File.getName() method
Statutory edit info
I have edited this answer simply to replace the previous algorithm, for checking the existence of a substring in a string, with the one that is currently used in the code above.
Here is an answer to search the file recursively??
String name; //to hold the search file name
public String listFolder(File dir) {
int flag;
File[] subDirs = dir.listFiles(new FileFilter() {
#Override
public boolean accept(File pathname) {
return pathname.isDirectory();
}
});
System.out.println("File of Directory: " + dir.getAbsolutePath());
flag = Listfile(dir);
if (flag == 0) {
System.out.println("File Found in THe Directory: " + dir.getAbsolutePath());
Speak("File Found in THe Directory: !!" + dir.getAbsolutePath());
return dir.getAbsolutePath();
}
for (File folder : subDirs) {
listFolder(folder);
}
return null;
}
private int Listfile(File dir) {
boolean ch = false;
File[] files = dir.listFiles();
for (File file : files) {
Listfile(file);
if (file.getName().indexOf(name.toLowerCase()) != -1) {//check all in lower case
System.out.println(name + "Found Sucessfully!!");
ch = true;
}
}
if (ch) {
return 1;
} else {
return 0;
}
}