6: Working with Files and Directories in .NET

Paint Data Matrix 2d barcode in .NET 6: Working with Files and Directories

6: Working with Files and Directories
Data Matrix 2d Barcode Scanner In VS .NET
Using Barcode Control SDK for .NET Control to generate, create, read, scan barcode image in .NET framework applications.
DataMatrix Drawer In VS .NET
Using Barcode creation for .NET framework Control to generate, create Data Matrix 2d barcode image in .NET applications.
610 Processing Directories
Scanning Data Matrix In .NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in .NET framework applications.
Encoding Bar Code In VS .NET
Using Barcode maker for Visual Studio .NET Control to generate, create bar code image in .NET applications.
Problem
Scanning Bar Code In .NET
Using Barcode reader for VS .NET Control to read, scan read, scan image in VS .NET applications.
Drawing Data Matrix In C#.NET
Using Barcode drawer for .NET framework Control to generate, create Data Matrix image in VS .NET applications.
You want to iteratively process all the files in a directory
Data Matrix ECC200 Generator In .NET
Using Barcode encoder for ASP.NET Control to generate, create Data Matrix image in ASP.NET applications.
Encoding Data Matrix 2d Barcode In VB.NET
Using Barcode drawer for VS .NET Control to generate, create Data Matrix image in .NET framework applications.
Solution
Code39 Generator In .NET
Using Barcode generation for Visual Studio .NET Control to generate, create Code39 image in .NET framework applications.
Matrix Barcode Drawer In VS .NET
Using Barcode generation for VS .NET Control to generate, create Matrix Barcode image in .NET applications.
Use PHP s scandir() function:
Paint UPC-A Supplement 2 In VS .NET
Using Barcode maker for VS .NET Control to generate, create GTIN - 12 image in .NET framework applications.
European Article Number 8 Generator In Visual Studio .NET
Using Barcode generator for .NET framework Control to generate, create EAN-8 Supplement 2 Add-On image in .NET applications.
< php // define directory path $dir = '/test'; // get directory contents as an array $fileList = scandir($dir) or die ("Not a directory"); // print file names and sizes foreach ($fileList as $file) { if (is_file("$dir/$file") && $file != '' && $file != '') { echo "$file: " filesize("$dir/$file") "\n"; } } >
Creating Code 39 In None
Using Barcode printer for Office Excel Control to generate, create Code 39 Full ASCII image in Microsoft Excel applications.
EAN 13 Generation In None
Using Barcode printer for Font Control to generate, create EAN13 image in Font applications.
Comments
Data Matrix Creation In Java
Using Barcode creator for Android Control to generate, create ECC200 image in Android applications.
Barcode Creation In Objective-C
Using Barcode generation for iPhone Control to generate, create bar code image in iPhone applications.
PHP s scandir() function offers a simple solution to this problem it returns the contents of a directory as an array, which can then be processed using any loop construct or array function An alternative approach is to use the Iterators available as part of the Standard PHP Library (SPL) Iterators are ready-made, extensible constructs designed specifically to loop over item collections, such as arrays and directories To process a directory, use a DirectoryIterator, as illustrated here:
Reading UPC Symbol In VB.NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in .NET applications.
GTIN - 12 Generator In None
Using Barcode maker for Office Excel Control to generate, create UPC-A Supplement 5 image in Office Excel applications.
< php // define directory path $dir = '/test'; // create a DirectoryIterator object $iterator = new DirectoryIterator($dir);
Barcode Recognizer In VB.NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in .NET framework applications.
Barcode Reader In C#.NET
Using Barcode recognizer for .NET Control to read, scan read, scan image in VS .NET applications.
PHP Programming Solutions
// rewind to beginning of directory $iterator->rewind(); // iterate over the directory using object methods // print each file name while($iterator->valid()) { if ($iterator->isFile() && !$iterator->isDot()) { print $iterator->getFilename() ": " $iterator->getSize() "\n"; } $iterator->next(); } >
Here, a DirectoryIterator object is initialized with a directory name, and the object s rewind() method is used to reset the internal pointer to the first entry in the directory You can then use a while() loop, which runs so long as a valid() entry exists, to iterate over the directory Individual file names are retrieved with the getFilename() method, while you can use the isDot() method to filter out the entries for the current () and parent () directories The next() method moves the internal pointer forward to the next entry You can read more about the DirectoryIterator at http://wwwphpnet/ ~helly/php/ext/spl/
611 Recursively Processing Directories
Problem
You want to process all the files in a directory and its subdirectories
Solution
Write a recursive function to process the directory and its children:
< php // function to recursively process // a directory and all its subdirectories function dirTraverse($dir) { // check if argument is a valid directory if (!is_dir($dir)) { die("Argument '$dir' is not a directory!"); }
6: Working with Files and Directories
// declare variable to hold file list global $fileList; // open directory handle $dh = opendir($dir) or die ("Cannot open directory '$dir'!"); // iterate over files in directory while (($file = readdir($dh)) !== false) { // filter out "" and "" if ($file != "" && $file != "") { if (is_dir("$dir/$file")) { // if this is a subdirectory // recursively process it dirTraverse("$dir/$file"); } else { // if this is a file // do something with it // for example, reverse file name/path and add to array $fileList[] = strrev("$dir/$file"); } } } // return the final list to the caller return $fileList; } // recursively process a directory $result = dirTraverse('/test'); print_r($result); >
Comments
As illustrated in the listing in 610: Processing Directories, it s fairly easy to process the contents of a single directory with the scandir() function Dealing with a series of nested directories is somewhat more complex The previous listing illustrates the standard technique, a recursive function that calls itself to travel ever deeper into the directory tree The inner workings of the dirTraverse() function are fairly simple Every time the function encounters a directory entry, it checks to see if that value is a file or a directory If it s a directory, the function calls itself and repeats the process until it reaches the end of the directory tree If it s a file, the file is processed the previous
PHP Programming Solutions
listing simply reverses the file name and adds it to an array, but you can obviously replace this with your own custom routine and then the entire performance is repeated for the next entry Another option is to use the Iterators available as part of the Standard PHP Library (SPL) Iterators are ready-made, extensible constructs designed specifically to loop over item collections such as arrays and directories A predefined Recursive DirectoryIterator already exists and it s not difficult to use this for recursive directory processing Here s how:
< php // initialize an object // pass it the directory to be processed $iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator('/test') ); // iterate over the directory foreach ($iterator as $key=>$value) { print strrev($key) "\n"; } >
The process of traversing a series of nested directories is significantly simpler with the SPL at hand First, initialize a RecursiveDirectoryIterator object and pass it the path to the top-level directory to be processed Next, initialize a RecursiveIterat orIterator object (this is an Iterator designed solely for the purpose of iterating over other recursive Iterators) and pass it the newly minted RecursiveDirectoryIterator You can now process the results with a foreach() loop You can read more about the RecursiveDirectoryIterator and the RecursiveIterator Iterator at http://wwwphpnet/~helly/php/ext/spl/ For more examples of recursively processing a directory tree, see the listings in 611: Recursively Processing Directories, 615: Copying Directories, n 617: Deleting Directories, and 620: Searching for Files in a Directory You can also read about recursively processing arrays in the listing in 43: Processing Nested Arrays
Copyright © OnBarcode.com . All rights reserved.