EzDevInfo.com

directory interview questions

Top directory frequently asked interview questions

Best way to require all files from a directory in ruby?

What's the best way to require all files from a directory in ruby ?


Source: (StackOverflow)

How can I get the list of files in a directory using C or C++?

How can I determine the list of files in a directory from inside my C or C++ code?

I'm not allowed to execute the 'ls' command and parse the results from within my program.


Source: (StackOverflow)

Advertisements

Deleting all files from a folder using PHP?

For example I had a folder called `Temp' and I wanted to delete or flush all files from this folder using PHP. Could I do this?


Source: (StackOverflow)

What is the current directory in a batch file?

I want to create a couple of batch files to automate some thing.

My question is when I create a batch file. What is the current directory? Is it the directory where the file is located or is it the same directory that appears in cmd?


Source: (StackOverflow)

Getting a list of all subdirectories in the current directory

Is there a way to return a list of all the subdirectories in the current directory in python?

I know you can do this with files, but I need to get the list of directories instead.


Source: (StackOverflow)

Can a Bash script tell what directory it's stored in?

How do I get the path of the directory in which a Bash script is located FROM that Bash script?

For instance, let's say I want to use a Bash script as a launcher for another application. I want to change the working directory to the one where the Bash script is located, so I can operate on the files in that directory, like so:

$ ./application

Source: (StackOverflow)

How can I add an empty directory to a Git repository?

How can I add an empty directory (that contains no files) to a Git repository?


Source: (StackOverflow)

In a Git repository, how to properly rename a directory?

In a Git repository, how to properly rename a directory? I think it should work to copy the directory to be renamed to a new directory with desired name, and delete the old directory, and git add, git commit and push everything. But is this the best way?


Source: (StackOverflow)

How to create a directory in Java?

How do I create Directory/folder?

Once I have tested System.getProperty("user.home");

I have to create a directory (directory name "new folder" ) if and only if new folder does not exist.


Source: (StackOverflow)

Create whole path automatically when writing to a new file

I want to write a new file with the FileWriter. I use it like this:

FileWriter newJsp = new FileWriter("C:\\user\Desktop\dir1\dir2\filename.txt");

Now dir1 and dir2 currently don't exist. I want Java to create them automatically if they aren't already there. Actually Java should set up the whole file path if not already existing.

How can I achieve this?


Source: (StackOverflow)

Rscript: Determine path of the executing script

I have a script called foo.R that includes another script other.R, which is in the same directory:

#!/usr/bin/env Rscript
print("Hello")
source("other.R")

But I want R to find that other.R no matter what the current working directory.

In other words, foo.R needs to know its own path. How can I do that?


Source: (StackOverflow)

How to find out mount/partition a directory or file is on? (Linux Server) [closed]

Is there a Linux command to easily find out which partition/mount a directory or file is on?

(This is probably a RTFM question, and I feel guilty for asking it, but somehow, I can't find a good answer on google just yet..)


Source: (StackOverflow)

How to [recursively] Zip a directory in PHP?

Directory is something like:

home/
    file1.html
    file2.html
Another_Dir/
    file8.html
    Sub_Dir/
        file19.html

I am using the same PHP Zip class used in PHPMyAdmin http://trac.seagullproject.org/browser/branches/0.6-bugfix/lib/other/Zip.php . I'm not sure how to zip a directory rather than just a file. Here's what I have so far:

$aFiles = $this->da->getDirTree($target);
/* $aFiles is something like, path => filetime
Array
(
    [home] => 
    [home/file1.html] => 1251280379
    [home/file2.html] => 1251280377
    etc...
)

*/
$zip = & new Zip();
foreach( $aFiles as $fileLocation => $time ){
    $file = $target . "/" . $fileLocation;
    if ( is_file($file) ){
        $buffer = file_get_contents($file);
        $zip->addFile($buffer, $fileLocation);
    }
}
THEN_SOME_PHP_CLASS::toDownloadData($zip); // this bit works ok

but when I try to unzip the corresponding downloaded zip file I get "operation not permitted"

This error only happens when I try to unzip on my mac, when I unzip through the command line the file unzips ok. Do I need to send a specific content type on download, currently 'application/zip'


Source: (StackOverflow)

Moving Files into a Real Folder in Xcode

When I started my project I was happy to use Groups in Xcode rather than literal folders: Since I'm using the browser in Xcode to access everything, stuff was nicely organized and I was happy.

However, now that the project is about to be shared for version control, the project folder itself is a horror show for those trying to scan it via a terminal, about 300 files, over half of which are graphics.

I'm trying to now reorganize things, creating real folders and importing them into Xcode. Unfortunately Xcode doesn't let me work with them the way it does with groups. For example, if I right-click on an actual added folder (blue, not yellow) and choose to add existing files, it doesn't actually put them in that folder, it puts them in its root.

Similarly, I can't move a file from a faux-folder (a group) into a real folder: Xcode doesn't consider the real folders to be valid places to move stuff to.

What am I missing? How can I convince Xcode to let me use the folders the way I use groups? There's an answer here to a somewhat similar question, but it doesn't actually solve my problem since I'm working with existing files.


Source: (StackOverflow)

How can I find script's directory with Python?

Consider the following Python code:

import os
print os.getcwd()

I use os.getcwd() to get the script file's directory location. When I run the script from the command line it gives me the correct path whereas when I run it from a script run by code in a Django view it prints /.

How can I get the path to the script from within a script run by a Django view?

UPDATE:
Summing up the answers thus far - os.getcwd() and os.path.abspath() both give the current working directory which may or may not be the directory where the script resides. In my web host setup __file__ gives only the filename without the path.

Isn't there any way in Python to (always) be able to receive the path in which the script resides?


Source: (StackOverflow)