{"id":76,"date":"2008-06-10T09:36:42","date_gmt":"2008-06-10T13:36:42","guid":{"rendered":"http:\/\/www.dev-notes.com\/blog\/2008\/06\/10\/get-files-from-a-directory-in-the-file-system-using-java\/"},"modified":"2008-06-10T09:36:42","modified_gmt":"2008-06-10T13:36:42","slug":"get-files-from-a-directory-in-the-file-system-using-java","status":"publish","type":"post","link":"https:\/\/www.dev-notes.com\/blog\/2008\/06\/10\/get-files-from-a-directory-in-the-file-system-using-java\/","title":{"rendered":"Get files from a directory in the file system using Java"},"content":{"rendered":"<p>If provided, any files matching any one of the strings found in the array of string ignore[] will be ignored; this can be useful if you want pick up all files except, for example, those marked with the word &#8220;temp&#8221; or obsolete files marked with &#8220;.old&#8221; extension.<\/p>\n<pre class=\"code\">\n\/**\n * Get the all files in a directory, ignoring files with file names containing particular strings.\n *\n * @param folderPath : Full path of directory in question, eg. \"C:\\docs\\myFiles\\\"\n * @param ignore[] : An array of strings containing words with which to ignore files, eg. { \"temp\",\"ignore\",\".old\" }\n * @return File[]\n * @author C. Peter Chen of http:\/\/dev-notes.com\n * @date 20080610\n *\/\nprivate static File[] getFiles(String folderPath, String ignore[]) {\n\tFile f = new File(folderPath);\n\tFile[] fList = f.listFiles();\n\tList<File> retFl = new ArrayList<File>();\n\tif (fList != null && fList.length > 0) {\n\t\tfor (File file : fList) {\n\t\t\tboolean pass = true;\n\t\t\tfor (String ig : ignore) {\n\t\t\t\tif (file.getName().indexOf(ig) >= 0) {\n\t\t\t\t\tpass = false;\n\t\t\t\t}\n\t\t\t}\n\t\t\tif (pass) { \/\/ Only get the file if none of the ignore strings are present\n\t\t\t\tretFl.add(file);\n\t\t\t}\n\t\t}\n\t}\n\treturn retFl.toArray(new File[0]);\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This simple Java method can be used to get an array of files from your operating system&#8217;s file system.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-76","post","type-post","status-publish","format-standard","hentry","category-java"],"_links":{"self":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/76","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/comments?post=76"}],"version-history":[{"count":0,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/76\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/media?parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/categories?post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/tags?post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}