Difference Between getcwd() and dirname(__FILE__) ? Which should I use?

__FILE__ is a magic constant containing the full path to the file you are executing. If you are inside an include, its path will be the contents of __FILE__. So with this setup: /folder/random/foo.php <?php echo getcwd() . “\n”; echo dirname(__FILE__) . “\n” ; echo “——-\n”; include ‘bar/bar.php’; /folder/random/bar/bar.php <?php echo getcwd() . “\n”; echo … Read more

.Net’s Directory Services throws a strange exception

I had this problem too using IIS Express and VS 2010. What fixed it for me was a comment on another thread. Validate a username and password against Active Directory? but i’ll save you the click and search… 🙂 Just add ContextOpations.Negotiate to you Validate Credentials call like below. bool valid = context.ValidateCredentials(user, pass, ***ContextOptions.Negotiate***); … Read more

Creating files and directories via Python

import os path = chap_name if not os.path.exists(path): os.makedirs(path) filename = img_alt + ‘.jpg’ with open(os.path.join(path, filename), ‘wb’) as temp_file: temp_file.write(buff) Key point is to use os.makedirs in place of os.mkdir. It is recursive, i.e. it generates all intermediate directories. See http://docs.python.org/library/os.html Open the file in binary mode as you are storing binary (jpeg) data. … Read more

Using scandir() to find folders in a directory (PHP)

To determine whether or not you have a folder or file use the functions is_dir() and is_file() For example: $path=”extracted/” . $name[0]; $results = scandir($path); foreach ($results as $result) { if ($result === ‘.’ or $result === ‘..’) continue; if (is_dir($path . “https://stackoverflow.com/” . $result)) { //code to use if directory } }

Given full path, check if path is subdirectory of some other path, or otherwise

DirectoryInfo di1 = new DirectoryInfo(dir1); DirectoryInfo di2 = new DirectoryInfo(dir2); bool isParent = di2.Parent.FullName == di1.FullName; Or in a loop to allow for nested sub-directories, i.e. C:\foo\bar\baz is a sub directory of C:\foo : DirectoryInfo di1 = new DirectoryInfo(dir1); DirectoryInfo di2 = new DirectoryInfo(dir2); bool isParent = false; while (di2.Parent != null) { if (di2.Parent.FullName … Read more

Iterate through folders, then subfolders and print filenames with path to text file

Charles’ answer is good, but can be improved upon to increase speed and efficiency. Each item produced by os.walk() (See docs) is a tuple of three items. Those items are: The working directory A list of strings naming any sub-directories present in the working directory A list of files present in the working directory Knowing … Read more

directories in a zip file when using java.util.zip.ZipOutputStream

ZipOutputStream can handle empty directories by adding a forward-slash / after the folder name. Try (from) public class Test { public static void main(String[] args) { try { FileOutputStream f = new FileOutputStream(“test.zip”); ZipOutputStream zip = new ZipOutputStream(new BufferedOutputStream(f)); zip.putNextEntry(new ZipEntry(“xml/”)); zip.putNextEntry(new ZipEntry(“xml/xml”)); zip.close(); } catch(Exception e) { System.out.println(e.getMessage()); } } }