If I have a directory called temp with the following files:
a_file1.jpg
a_file2.jpg
b_file1.jpg
b_file2.jpg
It's possible to get all files like this:
VFS.getManager().resolveFile("temp").getChildren();
But, what I actually want to do is get a_file1.jpg and a_file2.jpg. Maybe like:
VFS.getManager().resolveFile("temp/a*").getChildren();
But this throws an exception:
org.apache.commons.vfs.FileSystemException: Could not list the contents of "temp/a*" because it is not a folder.
So, does anyone know how to resolve a set of files based on a regex with VFS?
You could use the findFiles method, with a FileFilterSelector.
You'll need to create your own FileFilter that accepts the files that match your desired regex.
Related
I want to get the project path and then use it, to find a file.
The problem is that it not returned in the correct format.
For example this is the response:
X:\Project_11_01_2021\test
and to use it I need it to be
X:\\Project_11_01_2021\\test
I use it in selenium to select the path to upload a file
.sendKeys(
path);
Is their a way to get the value and use it in other function?
String path2 = System.getProperty("user.dir");
I used it in windows computer
So if your problem is just, that the path is formatted wrong that is no problem. You just use pythons string replace function and do something like this:
path.replace("\", "\\")
and you should be ready to go. If not, I don't really understand your problem...
I'm trying to create an IntelliJ plugin that iterates over all files in the project folder and parses all the .java files and then makes some changes in them. The problem is that after reading the documentation I don't have a clear idea how to iterate files over the whole project folder, I think I may use PSI files but I am not sure. Does anyone know or has an idea on how to accomplish this?
To iterate all files in project content, you can use ProjectFileIndex.SERVICE.getInstance(project).iterateContent.
Then you can get PSI files from them (PsiManager#findFile), check if they're Java (instanceof PsiJavaFile) and do whatever you like.
If you don't need PSI, you can just check the file type
(VirtualFile#getFileType == JavaFileType.INSTANCE) and perform the modifications via document (FileDocumentManager#getDocument(file)) or VFS (LoadTextUtil#loadText, VfsUtil#saveText).
A possible way is to use AllClassesGetter, like this:
Processor<PsiClass> processor = new Processor<PsiClass>() {
#Override
public boolean process(PsiClass psiClass) {
// do your actual work here
return true;
}
};
AllClassesGetter.processJavaClasses(
new PlainPrefixMatcher(""),
project,
GlobalSearchScope.projectScope(project),
processor
);
processJavaClasses() will look for classes matching a given prefix in a given scope. By using an empty prefix and GlobalSearchScope.projectScope(), you should be able to iterate all classes declared in your project, and process them in processor. Note that the processor handles instances of PsiClass, which means you won't have to parse files manually. To modify classes, you just have to change the tree represented by these PsiClasses.
We have a folder where we dump lot of files. Our program needs to read one of the specific files with the latest version. The file name would be something like "2016-03-04-12-46-48_ABC_123456_1.xml".
Insted of reading all the files and then iterating to find the exact file i have used following code with a regular expression
File folder = new File("C:\\some_folder")
folder.listFiles((FilenameFilter) new AwkFilenameFilter("(\\d){4}-(\\d){2}-(\\d){2}-(\\d){2}-(\\d){2}-(\\d){2}_ABC_" + <ID_String> +"_(\\d){1,2}"))
But for some reason the reqular expression is not working. Can someone please help with this?
It seems you are missing the file extension in the regex:
(\\d){4}-(\\d){2}-(\\d){2}-(\\d){2}-(\\d){2}-(\\d){2}_ABC_" + <ID_String> +"_(\\d){1,2}\\.xml
Try out this one.
"\d{4}-\d{2}-\d{2}-\d{2}-\d{2}-\d{2}_ABC_"+<ID string>+"_\d{1}.xml"
It works perfectly for me.
My exchange body contains a class with a string attribute with path to file with which the body need to be enriched. There another attribute to hold file after enriched. I failed to find a solution to build a dynamic route that will load a file and return result I could use in Aggregation Strategy.
When you use the file component, you have to specify a path to a directory which can't be dynamic. You can only specify patterns on files in the directory.
Extract from the camel-file doc:
Camel supports only endpoints configured with a starting directory. So
the directoryName must be a directory. If you want to consume a single
file only, you can use the fileName option, e.g. by setting
fileName=thefilename. Also, the starting directory must not contain
dynamic expressions with ${ } placeholders. Again use the fileName
option to specify the dynamic part of the filename
Could you give us more info on what your are trying to achieve, so that we can give you workarounds ?
Let' say I have two paths, first can look like folder/ and second like /anotherFolder/image.png. I would like to merge those two paths in some automated fashion and with option for user to omit the last slash in first string and first slash in second string. So all of these
folder/ + /anotherFolder/image.png
folder + anotherFolder/image.png
folder + /anotherFolder/image.png
should give me folder/anotherFolder/image.png
I need to merge two properties in one of my projects and I want it as dummy as possible:)So is there some trick with URL class or do I have to play around with Strings?
You can do this with java.io.File, by using the constructor which takes a File and a String as arguments, will interpret the String as a relative path to the File.
Or with java.net.URL, you can send an URL and a String to the constructur, which will interpret the URL as a context for the String parameter.
I actually used FileUtils.getFile() from Apache Commons IO but Rolf's solution was working too.