How to use a folder under a certain path and a folder under a certain path

Source: Internet
Author: User

How to use a folder under a certain path and a folder under a certain path

/*###################################### ######################################## ######################################## ######################################## ######################################## ######################################## ######################################## ######################################## ######################################## ######################################## ######################################## ######################################## #########______ # _______ ############______________ There is no BUG in our future * ================ ========================================================== ========================================= * Filename: text * Created: 2016/4/26 11: 45 * Author: haYaShi WangYuChen * ====================================== ========================================================== = */using UnityEngine; using System. collections; using System. IO; using System; public class Text: monoBehaviour {/// <summary> // determine the number of files in the parent folder or the number of files in all parent folders. /// </summary> // <param name = "physicelPath"> the input is a physical path </param> /// <param name = "isIncludeSubDirectory"> </param> /// <returns> </returns> public static int FileCount (string physicelPath, bool isIncludeSubDirectory) {int count = 0; if (isIncludeSubDirectory) {// The number of files in the parent folder and the number of files in all subfolders count = Directory. getFiles (physicelPath ,"*. * ", SearchOption. allDirectories ). length;} else {// number of files in the parent folder count = Directory. getFiles (physicelPath ,"*. * ", SearchOption. topDirectoryOnly ). length;} return count ;} /// <summary> /// determine the number of parent folders or the number of all sub-parent folders. /// </summary> /// <param name = "physicelPath"> pass a Boolean parameter </param> /// <param name = "isIncludeSubDirectory"> </param> /// <returns> </returns> public static int DirectoryCount (string physicelPath, bool isIncludeSubDirectory) {int count = 0; if (isIncludeSubDirectory) {// number of parent folders and number of all subfolders count = Directory. getDirectories (physicelPath ,"*. * ", SearchOption. allDirectories ). length;} else {// The number of parent folders count = Directory. getDirectories (physicelPath ,"*. * ", SearchOption. topDirectoryOnly ). length;} return count ;} /// <summary> /// obtain the size of all objects in a directory /// </summary> /// <param name = "physicelPath"> folder path </ param> // <returns> </returns> public static long TotalFileSizes (string physicelPath) {string [] a = Directory. getFiles (physicelPath ,"*. * ", SearchOption. allDirectories); // Directory. getFiles (@ "D: \ Test ","*. jpg ", SearchOption. allDirectories); // find all JPG files in this directory (and its subordinates) long B = 0; foreach (var item in a) {FileInfo info = new FileInfo (item ); B + = info. length;} return B;} // <summary> // operation folder, delete all empty folders /// </summary> /// <param name = "physicalpath"> physical path </param> public static void RemoveAllEmptyDirectories (string physicalpath) {// traverse the subfolder foreach (var item in Directory. getDirectories (physicalpath) {// cyclically traverse its subfolders RemoveAllEmptyDirectories (item); // judge whether the files in the folder are empty and determine whether the files in its subfolders are empty if (Directory. getFiles (item ). length = 0 & Directory. getDirectories (item ). length = 0) {// delete the file Directory. delete (item, false );}}} /// <summary> /// delete the files in all folders under a path /// </summary> /// <param name = "physicalPath"> physical path </ param> public static void DeleteAllFiles (string physicalPath) {DirectoryInfo id = new DirectoryInfo (physicalPath); // traverses and deletes the file foreach (FileInfo item in id) in this folder. getFiles () {item. delete ();} // traverse the subfolders array foreach (DirectoryInfo item in id. getDirectories () {DeleteAllFiles (item. fullName );}} /// <summary> /// delete the files in all folders under a path within days /// </summary> /// <param name = "physicalPath"> physical path </param> /// <param name = "days"> days </param> public static void DeleteAllFilesDays (string physicalPath, int days) {int day =-(Math. abs (days); DirectoryInfo id = new DirectoryInfo (physicalPath); // traverse and delete the file foreach (FileInfo item in id. getFiles () {Debug. log ("File Creation Time" + item. creationTime); Debug. log ("before" + days + "day current time" + DateTime. now. addDays (day); // determines whether the file is later than the previous days if (item. creationTime> DateTime. now. addDays (day) {item. delete () ;}// traverse the subfolders array foreach (DirectoryInfo item in id. getDirectories () {DeleteAllFilesDays (item. fullName, days) ;}} void Start () {// Debug. log (TotalFileSizes ("C: \ Users \ EasyAR \ Desktop \ Text") + ": Byte"); // Debug. log ("the number of files in the parent folder and the number of files in all subfolders" + FileCount ("D: \ hahaha", true); // Debug. log ("number of files in the parent folder" + FileCount ("D: \ hahaha", false); // Debug. log ("the number of parent folders and the number of all subfolders" + DirectoryCount ("D: \ hahaha", true); // Debug. log ("number of parent Folders" + DirectoryCount ("D: \ hahaha", false); // Debug. log (TotalFileSizes ("C :\\ Users \ EasyAR \ Desktop \ Text") + ": Byte"); // RemoveAllEmptyDirectories ("D: \ hahaha \ xixixi "); // DeleteAllFiles (" D :\\ hahaha \ xixixi "); // DeleteAllFilesDays (" D :\\ Haha \ xixixi ", 10 );}}

I have recently studied how to obtain folders and summarized some points. I hope you can use them.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.