I'm trying to make a very simple java program that switches between a few sets of files. To do this, I am trying to delete preexisting files, and then copy over the ones from one of the "presets" I'm talking about. However, since there will be many other files in the same location that I do not want to touch, I only want to delete files with a certain prefix/name.
An example to clarify:
Preset folder one has: enbcache (folder with subfolders and items), enbseries (folder with subfolders and items), and enblocal.ini.
The destination folder has the same files and many others: enbcache (folder with subfolders and items), enbseries (folder with subfolders and items), enblocal.ini, data, d3d11.dll, tbb.dll, other folders... etc.
The program should delete all of the old files and folders with "enb" in the name, and leave everything else alone. Then the files from "Preset folder one" would be copied into the destination folder.
I have found many different methods for deleting files and folders with java, as well as ways to delete specific files. However, The problem I encounter is when trying to combine these things. Since the items within the folders need to be deleted but do not have the "enb" prefix, the program cannot delete the folder as it still contains left over files.
I've tried using NIO Java 7 and 8 as well as simpler methods of just using File, but keep getting to the same point of not being able to delete the "enb" folders with items inside. I will post the code someone provided in here:
Delete Files with same Prefix String using Java
that I am currently trying to use.
When using the "enb" prefix with it I get the previously described problem: It can't delete all of the folders because some of them have files in them without the "enb" prefix.
public static void deleteFilesForPathByPrefix(final Path path, final String prefix) {
try (DirectoryStream<Path> newDirectoryStream = Files.newDirectoryStream(path, prefix + "*")) {
for (final Path newDirectoryStreamItem : newDirectoryStream) {
Files.delete(newDirectoryStreamItem);
}
} catch (final Exception e) {
e.printStackTrace();
}
}