Read file path from commandline and displays information about file and directory

The following example console application takes a file path from a commandline argument and then displays information about the file and the containing directory. using System; using System.IO;   public class FileInform {   private static void Main(string[] args) {   if (args.Length == 0) {   Console.WriteLine("Please supply a file name."); return; }   […]
Continue reading…

Enjoyed this post? Share it!

 
 

Display subfolders from directory using php

Example to display subfolders from directory using php: <?php echo "<h2>subdirs in dir</h2><ul>"; $basedir = basename( __FILE__ ); $dirtoscan = ($basedir . ‘/somedir/’); $albumlisting = scandir($dirtoscan); foreach ($albumlisting as $item) { $dirinfo = pathinfo($item); print_r($dirinfo); if (is_dir("$item")) { echo "<li><a href=’index.php?subdirs=$item’>$item</a></li>"; } } ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Copy files using stream in PHP using stream_copy_to_stream()

The stream_copy_to_stream() can be used to copy data from one stream to another. It makes a copy of up to maxlength bytes of data from the current position (or from the offset position, if specified) in source to dest. If maxlength is not specified, all remaining content in source will be copied.   <?php function […]
Continue reading…

Enjoyed this post? Share it!

 
 

Recursively copy directories and subdirectories in PHP

<?php function recurse_copy($src,$dst) { $dir = opendir($src); @mkdir($dst); while(false !== ( $file = readdir($dir)) ) { if (( $file != ‘.’ ) && ( $file != ‘..’ )) { if ( is_dir($src . ‘/’ . $file) ) { recurse_copy($src . ‘/’ . $file,$dst . ‘/’ . $file); } else { copy($src . ‘/’ . $file,$dst […]
Continue reading…

Enjoyed this post? Share it!

 
 

Remove directory, its contents and subdirectories using PHP

We can use the RecursiveDirectoryIterator and RecursiveIteratorIterator to delete directories and their children (files and subdirectories). The children directories should be deleted before their parents <?php function cleanup_directory($dir) { $iter = new RecursiveDirectoryIterator($dir); foreach (new RecursiveIteratorIterator($iter, RecursiveIteratorIterator::CHILD_FIRST) as $f) { if ($f->isDir()) { rmdir($f->getPathname()); } else { unlink($f->getPathname()); } } rmdir($dir); }   cleanup_directory(’c:\\wamp\\junk’); ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Calculate disk space used by files under a directory

The RecursiveDirectoryIterator extends the DirectoryIterator with a getChildren() method that provides access to the elements in a subdirectory. The RecursiveIteratorIterator flattens the hierarchy that the RecursiveDirectoryIterator returns into one list. <?php $dir = new RecursiveDirectoryIterator(’C:\wamp’); $totalSize = 0; foreach (new RecursiveIteratorIterator($dir) as $file) { $totalSize += $file->getSize(); } print "The total size is $totalSize.\n"; ?>
Continue reading…

Enjoyed this post? Share it!

 
 

Find all filenames that match a pattern using a FilterIterator

The following code illustrates the use of a FilterIterator subclass with DirectoryIterator. The FilterIterator subclass needs its own accept() method that decides whether or not a particular value is acceptable. <?php class ImgFilter extends FilterIterator { public function accept() { return preg_match(’@\.(gif|jpe?g|png)$@i’,$this->current()); } }   foreach (new ImgFilter(new DirectoryIterator(’/www/public_html/images’)) as $img) { print "<img src=’".htmlentities($img)."’/>\n"; […]
Continue reading…

Enjoyed this post? Share it!

 
 

Working with directories in Isolated Storage in C#

Files can be stored in Isolated Storage under directories. Directories must be created using the CreateDirectory method of IsolatedStorageFile class. IsolatedStorageFile mStore = IsolatedStorageFile.GetMachineStoreForAssembly(); mStore.CreateDirectory("dirname"); IsolatedStorageFileStream mStream = new IsolatedStorageFileStream(@"dirname\settings.ini", FileMode.Create, mStore); The code above will create a file named settings.ini under a directory named dirname. It is simple to read the file by opening […]
Continue reading…

Enjoyed this post? Share it!

 
 

Move uploaded files to a different directory using PHP

PHP stores uploaded files in a temporary directory with temporary file names. It is a good practise to move uploaded files to a permanent directory, if you want to keep them permanently. The move_uploaded_file() function can be used to move uploaded files. Example code: <?php $file = ‘/tmp/w3image.gif’; print("<pre>\n"); move_uploaded_file($_FILES[’w3img’][’tmp_name’], $file); print("File uploaded: ".$file."\n"); print(" […]
Continue reading…

Enjoyed this post? Share it!

 
 

Remove an empty directory in php

An empty existing directory can be removed by using the rmdir() php function. Example use of rmdir(): <?php if (file_exists("/temp/test")) { rmdir("/temp/test"); print("Directory removed.\n"); } else { print("Directory does not exist.\n"); } ?> This script will print: Directory removed. If you run this script again, it will print: Directory does not exist.
Continue reading…

Enjoyed this post? Share it!

 
 

Create a directory in php

The mkdir() php function can be used to create a directory provided that the necessary file permissions exist. Example on mkdir(): <?php if (file_exists("/temp/test")) { print("Test Directory already exists.\n"); } else { mkdir("/temp/test"); print("Test Directory created.\n"); } ?> Output – first execution Test Directory created. If you run this script again, it will print: Test […]
Continue reading…

Enjoyed this post? Share it!

 
 

Programatically monitor directory for changes using c#

To check for changes to a directory i.e a new file or folder added to the directory or a file or folder deleted from the directory, we must use the filesystemwatcher class. This class allows us to raise events when changes occur to a directory. We create a new FileSystemWatcher object, specifying the directory in […]
Continue reading…

Enjoyed this post? Share it!

 
 

How to enumerate or list files in a directory using c#

To obtain a list of files in a directory, we must create a new DirectoryInfo object by specifying the path to the directory and use the DirectoryInfo object’s GetFiles method to list the files. Example code: DirectoryInfo di = new DirectoryInfo(@"C:\mydir");   Console.WriteLine("Directory: {0}", di.FullName);   foreach (FileInfo file in di.GetFiles()) { Console.WriteLine("File: {0}", file.Name); […]
Continue reading…

Enjoyed this post? Share it!

 
 

Directory operations in PHP

PHP has a whole variety of functions for directory operations. Here are a few of the important ones: chdir($dir): Change working directory $dir = getcwd(): Get working directory mkdir($dir, [$permission], [$recursive]): Create a new directory with permission model (in octal) $permission is_dir($directory): Check if path is an actual directory chmod($file, $permission): Change permission of a […]
Continue reading…

Enjoyed this post? Share it!

 
 

Removing a directory in php with rmdir()

The php function rmdir() enables developers to remove a directory from the file system. The process trying to delete the directory must have enough permissions and the directory must be empty. The rmdir() function requires a string representing the path to the directory to be deleted. It returns TRUE on success or FALSE on failure. […]
Continue reading…

Enjoyed this post? Share it!

 
 

Creating directories in php with mkdir()

mkdir() php function enables you to create a directory. mkdir() requires a string representing the path to the directory you want to create and an integer that should be an octal number representing the mode you want to set for the directory. You specify an octal (base 8) number with a leading 0. The mode […]
Continue reading…

Enjoyed this post? Share it!