6: Working with Files and Directories in .NET

Creating Data Matrix in .NET 6: Working with Files and Directories

6: Working with Files and Directories
DataMatrix Reader In .NET Framework
Using Barcode Control SDK for .NET Control to generate, create, read, scan barcode image in VS .NET applications.
DataMatrix Creator In .NET
Using Barcode printer for .NET framework Control to generate, create Data Matrix ECC200 image in .NET applications.
// if this is a file // copy it copy ("$source/$file", "$destination/$file") or die ("Cannot copy file '$file'"); } } } // close directory closedir($dh); } // copy directory recursively copyRecursive("www/template", "www/site12"); echo "Directories successfully copied"; >
Data Matrix 2d Barcode Decoder In Visual Studio .NET
Using Barcode recognizer for .NET Control to read, scan read, scan image in .NET applications.
Draw Barcode In Visual Studio .NET
Using Barcode encoder for .NET Control to generate, create bar code image in Visual Studio .NET applications.
Comments
Barcode Reader In VS .NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
DataMatrix Drawer In Visual C#
Using Barcode generator for .NET Control to generate, create Data Matrix 2d barcode image in VS .NET applications.
This listing is actually a combination of techniques discussed in the listings in 611: Recursively Processing Directories and 613: Copying Files Here, the custom copyRecursive() function iterates over the source directory and, depending on whether it finds a file or directory, copies it to the target directory or invokes itself recursively The recursion ends when no further subdirectories are left to be traversed Note that if the target directory does not exist at any stage, it is created with the mkdir() function
Encoding Data Matrix In .NET Framework
Using Barcode drawer for ASP.NET Control to generate, create Data Matrix image in ASP.NET applications.
Data Matrix ECC200 Generator In Visual Basic .NET
Using Barcode printer for .NET framework Control to generate, create Data Matrix image in VS .NET applications.
616 Deleting Files
Printing Barcode In .NET Framework
Using Barcode drawer for .NET Control to generate, create barcode image in .NET framework applications.
Draw Universal Product Code Version A In .NET
Using Barcode creator for VS .NET Control to generate, create UPC A image in .NET framework applications.
Problem
Creating GS1 DataBar In .NET
Using Barcode creation for .NET framework Control to generate, create GS1 DataBar image in Visual Studio .NET applications.
EAN / UCC - 14 Creation In Visual Studio .NET
Using Barcode printer for Visual Studio .NET Control to generate, create UCC - 14 image in .NET applications.
You want to delete a file
Barcode Decoder In Visual Basic .NET
Using Barcode Control SDK for .NET framework Control to generate, create, read, scan barcode image in Visual Studio .NET applications.
Barcode Creation In Objective-C
Using Barcode maker for iPhone Control to generate, create bar code image in iPhone applications.
Solution
Bar Code Creator In None
Using Barcode creator for Font Control to generate, create bar code image in Font applications.
Drawing Bar Code In C#.NET
Using Barcode printer for .NET Control to generate, create barcode image in .NET applications.
Use PHP s unlink() function:
EAN / UCC - 14 Drawer In Java
Using Barcode generator for Android Control to generate, create USS-128 image in Android applications.
Draw UCC.EAN - 128 In .NET Framework
Using Barcode drawer for ASP.NET Control to generate, create USS-128 image in ASP.NET applications.
< php // set file name $file = "shakespeareasc";
Draw Bar Code In None
Using Barcode encoder for Microsoft Word Control to generate, create barcode image in Microsoft Word applications.
Bar Code Printer In .NET
Using Barcode maker for Reporting Service Control to generate, create bar code image in Reporting Service applications.
PHP Programming Solutions
// check if file exists // if it does, delete it if (file_exists($file)) { unlink ($file) or die("Cannot delete file '$file'"); echo "File successfully deleted"; } else { die ("Cannot find file '$file'"); } >
Comments
To delete a file with PHP, simply call the unlink() function with the file name and path The function returns true if the file was successfully deleted
NOTE
Typically, PHP will not be able to delete files owned by other users; the PHP process can only delete files owned by the user it s running as This is a common cause of errors, so keep an eye out for it!
617 Deleting Directories
Problem
You want to delete a directory and its contents, including subdirectories
Solution
Write a recursive function to travel through a directory and its children, deleting files as it goes:
< php // function to recursively delete // a directory and its subdirectories function deleteRecursive($dir) { // check if argument is a valid directory if (!is_dir($dir)) { die("'$dir' is not a valid directory"); } // open directory handle $dh = opendir($dir) or die ("Cannot open directory '$dir'");
6: Working with Files and Directories
// 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 delete it deleteRecursive("$dir/$file"); } else { // if this is a file // delete it unlink ("$dir/$file") or die ("Cannot delete file '$file'"); } } } // close directory closedir($dh); // remove top-level directory rmdir($dir); } // delete directory recursively deleteRecursive("junk/robert/"); echo "Directories successfully deleted"; >
Comments
In PHP, the function to remove a directory a rmdir() Unfortunately, this function only works if the directory in question is empty Therefore, to delete a directory, it is first necessary to iterate over it and delete all the files within it If the directory contains subdirectories, those need to be deleted too; you do this by entering them and erasing their contents The most efficient way to accomplish this task is with a recursive function such as the one in the previous listing, which is a combination of the techniques outlined in the listing in 611: Recursively Processing Directories and the listing in 616: Deleting Files Here, the deleteRecursive() function accepts a directory path and name and goes to work deleting the files in it If it encounters a directory, it invokes itself recursively to enter that directory and clean it up Once all the contents of a directory are erased, you use the rmdir() function to remove it completely
PHP Programming Solutions
618 Renaming Files and Directories
Problem
You want to move or rename a file or directory
Solution
Use PHP s rename() function:
< php // set old and new file/directory names $oldFile = "home/john"; $newFile = "home/jane"; // check if file/directory exists // if it does, move/rename it if (file_exists($oldFile)) { rename ($oldFile, $newFile) or die("Cannot move/rename file '$oldFile'"); echo "Files/directories successfully renamed"; } else { die ("Cannot find file '$oldFile'"); } >
Copyright © OnBarcode.com . All rights reserved.