user474901
user474901

Reputation:

how to get files in sub folders

I'm creating C# console app to clean up my download folder in windows my app work fine for video file and move and and delete it from download folder. but how can I make get get the file in subfolder and add it to my files array?

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CleanDownloadFolder
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourcePath = @"C:\Users\___\Downloads";
            string targetPath = @"C:\Users\__\Videos";

            CopyDirectory(sourcePath, targetPath);
        }

        private static void CopyDirectory(string sourcePath, string targetPath)
        {
            // To copy a folder's contents to a new location: 
            // Create a new target folder, if necessary. 
            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }

            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath);
                string fileName = null;
                string destFile = null;
                // Copy the files and overwrite destination files if they already exist. 
                foreach (string s in files)
                {
                    // Use static Path methods to extract only the file name from the path.
                    fileName = System.IO.Path.GetFileName(s);
                    destFile = System.IO.Path.Combine(targetPath, fileName);
                    if (Path.GetExtension(fileName) == ".avi")
                    {
                        System.IO.File.Copy(s, destFile, true);
                        System.IO.File.Delete(s);
                    }


                }

            }

        }
    }
}

Upvotes: 1

Views: 1274

Answers (1)

Steve
Steve

Reputation: 216343

Directory.GetFiles has an overload that could be used to get the list of files in subdirectories

  string[] files = Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories);

The remainder of your code should work as is, however, if your are interested only in the AVI files then you could put that extension directly in the GetFiles call. In that way you get only AVI files and your code could be simplified removing the if

string[] files = Directory.GetFiles(sourcePath. "*.AVI", SearchOption.AllDirectories);
string fileName = null;
string destFile = null;
// Copy the files and overwrite destination files if they already exist. 
foreach (string s in files)
{
    // Use static Path methods to extract only the file name from the path.
    fileName = Path.GetFileName(s);
    destFile = Path.Combine(targetPath, fileName);
    File.Copy(s, destFile, true);
    File.Delete(s);
}

I suggest also to add a using System.IO; at the top of your code file to avoid all of that full namespace typing required without the using

Upvotes: 1

Related Questions