I am trying to filter files using FilenameFilter to grep files files in a directory.
% ls -1
DirFilter.class
DirList.class
DirList.java
doctors.txt
node.l
rels.l
I am trying to filter node.l and rels.l. Filter should succeed if and only if both files are present.
I tried my regex on debuggex.com and it seems to work as expected :
http://www.debuggex.com/embed/CZgVeUE2iWsNfRNG
my regex : (?s)node.l.?(?=(rels.l))
but when I run it through DirList.java filter it doesn't work..
% java DirList "(?s)node.l.?(?=(rels.l))"
<no-output>
Now I am using DirList.java from Thinking in Java
http://www.cs.odu.edu/~cs476/tijava2/c10/DirList.java
Any ideas?
DirList is evaluating your regex against each file name separately, not as a single \n delimited directory listing string as returned by ls. Your regex will never match under those conditions since it never sees more than one file name at a time.
FilenameFilter works for single file not for groups of files so regex will be applied only to that single file. Instead of regex try maybe this way:
take name of file
if it is node.l check if new File(currentDir+"/rels.l").exists().
if it is rels.l check if new File(currentDir+"/node.l").exists().
Related
I try to move a file from one directory to another.
I do this with
File fileToMove = new File("/Users/kai-dj/separator_problem/from/file_to_move.file");
File destDir = new File("/Users/kai-dj/separator_problem/to");
if (fileToMove.exists() && destDir.isDirectory()) {
fileToMove.renameTo(new File(destDir.getAbsolutePath()+File.pathSeparator+fileToMove.getName()));
}
I'd expect to find file_to_move.file in folder /Users/kai-dj/separator_problem/to after execution, but I get a file named to/file_to_move.file placed in the parent folder /Users/kai-dj/separator_problem. At least that's what Finder shows.
As I thought: "File names mustn't contain path separator characters, this can't be true.", I also checked what ls would output in terminal:
mac-book:separator_problem kai-dj$ ls
from to:file_to_move.file
to
OK – seems no /in file name. Very strange nontheless.
Why does Finder show it as file name containing /?
Why does Java rename the file to <dirname>:<filename> – especially even when I used File.pathSeparator, not / and certainly not :?
I also tried with Files.move – same result.
EDIT: Solved, but I'd still love to know, why Finder shows : as / ^^
As mentioned in the comment above, the correct member to use is called File.separator.
Also, you can avoid using File.separator in general, and use Paths instead:
System.out.println(Paths.get("/Users/kai-dj/separator_problem/to", fileToMove.getName()).toAbsolutePath());
I am trying to read a file in my Download folder (I have a mac system) using the below line of code:
PDDocument doc = PDDocument.load(new File("/Users/mohand/Downloads/1963_Automation_BigTurnip_290618.pdf"));
The problem is, the numeral "1963" will keep on changing but text "_Automation_BigTurnip_290618.pdf" will remain the same.
Can I use any regex that will pick up any file that has "_Automation_BigTurnip_290618.pdf" ?
I did this using :
File dir = new File("/Users/mohand/Downloads/");
File[] files = dir.listFiles((dir1, name) -> name.endsWith("Automation_BigTurnip_290618.pdf"));
No need for regex. Just use endsWith:
if (name.endsWith("_Automation_BigTurnip_290618.pdf"))
You could use a regular expression like this:
^(\d+)_Automation_BigTurnip_290618\.pdf$
This would match any file starting with at least one digit and ending in the pattern you specified.
I have a folder in "Windows 10" Operating System with files named as;
afilename_1
bfilename_3
cfilename_2
This is how it appears. But I need it to make it sorted according to numbers appended after the underscore. I want the words before underscore, as well as they, depict file description.
Hint: Swapping the words/numbers separated by '_'.
Output expected:
1_afilename
2_cfilename
3_bfilename
Any idea apart from manual renaming?
P.S. The folder consists of 100's of files.
A Solution in Java programming language is appreciable but not mandatory.
Update (10.01.2017) : Here is the sample Java code figured out to get all file names as output.
File folder = new File("/xxx/ss/folder/");
File[] listOfFiles = folder.listFiles();
for (File file : listOfFiles)
{ if (file.isFile())
{ System.out.println(file.getName());
} }
Now, thinking how to get each filename at once and swapping done in a loop.
Any thoughts?
Solution: Windows Powershell code worked.
If input: file names are as follows
xxx_afilename_1
xxx_bfilename_3
xxx_cfilename_2
then the following code helps in getting similar output as stated above.
ls | %{ ren $_ ($_.name -replace '.*?-(.*?)-(.*?)(\..*)','$2_$1$3') }
In Windows Powershell
ls | %{ ren $_ ($_.name -replace '(.+)_(.+)','$2_$1') }
Update for keeping extension
ls | %{ ren $_ ($_.name -replace '(.+)_(.+)(\..+)','$2_$1$3') }
There are two ways I can think of.
It is a fairly simple program to write in C/C++/C# or Python or what-have-you. Depending on your expertise it may take as long to write and debug the program as to do the job by hand, but much more fun.
A good programmers text editor can take a list of files such as from a directory listing and generate a .bat file with the rename commands in. Such things as macros and regular-expression search-and-replace makes the job fairly painless. (I always recommend jEdit -- free, multiplatform and powerful.)
I am having a file name like suppose abcde-1_Transformed.xml.
Now if I have many number of files suppose in the above file name the '1' which is present has files like
abcde-2_Transformed.xml.
abcde-3_Transformed.xml.
abcde-4_Transformed.xml.
till abcde-1966_Transformed.xml.
How to get the file names which are not present in the total number of files using regex ?
I think rather than making a regex you can just read the file in the current directory using this link Read all files in a folder
Then parse out the number from file name and check if it's present or not.
Background:
I was writing a filewalker that starts at a specific folder and walks through each subfolder and its contents. Each time a directory or file is visited I compare the path to a PathMatcher containing a glob pattern (directories matched on path, files matched on extension). If it matched the pattern I would add it to a list for later use. This is so that I can build a filtered list based on a GLOB pattern. I wrote this on Unix (mac) and it seemed to work fine. When I tested it on Windows I got 0 results.
Filter(String dirPattern, String extensionPattern) {
dirMatcher = FileSystems.getDefault().getPathMatcher(
"glob:" + dirPattern);
extensionMatcher = FileSystems.getDefault().getPathMatcher(
"glob:" + extensionPattern);
}
Problem:
Take these GLOB patterns for example
Directory pattern:
**/{foo/bar,foo/baz/,fizz/buzz/fizzbuzz}
Would work fine on UNIX but not on Windows. Now I've read that the JVM shouldn't have trouble interpreting '/' as a valid file seperator for both platforms but nonetheless I tried changing it to a Windows style path.
**\{foo\bar,foo\baz\,fizz\buzz\fizzbuzz}
Needs some escaping for java:
**\\{foo\\bar,foo\\baz\\,fizz\\buzz\\fizzbuzz}
But this still wont match anything...
I even tried doing an OS check and replacing the '/' with Java File.seperator.
if (OSUtil.getCurrentOS() == OS.Windows) {
dirPattern = dirPattern.replace("/", File.separator);
extensionPattern = extensionPattern
.replace("/", File.separator);
}
It seems like the file seperators between the brackets are interpreted and correct but the first slash needs to be Windows style. At this point I'm not sure what is happening. Is there anyone that can shed some light on this problem? Any help would be appreciated.