0

I have no problems when I want to create a new folder I'm working with

Directory.CreateDirectory

Now I'm trying to get all image files from my desktop and I want to move all images to that folder which was created with Directory.CreateDirectory

I've testet file.MoveTo from here

FileInfo file = new FileInfo(@"C:\Users\User\Desktop\test.txt");

to here

file.MoveTo(@"C:\Users\User\Desktop\folder\test.txt");

This works perfect. Now I want to do that with all the image files from my dekstop

(Directory.CreateDirectory(@"C:\Users\User\Desktop\Images");)

How could I do that?

Rick james
  • 824
  • 1
  • 11
  • 30

2 Answers2

2

Example code of getting images with certain extentions from one root folder:

        static void Main(string[] args)
      {
        // path to desktop
        var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

        //get file extentions by speciging the needed extentions
        var images = GetFilesByExtensions(new DirectoryInfo(desktopPath) ,".png", ".jpg", ".gif");

        // loop thrue the found images and it will copy it to a folder (make sure the folder exists otherwise filenot found exception) 
        foreach (var image in images)
        {
            // if you want to move it to another directory without creating a copy use:
            image.MoveTo(desktopPath + "\\folder\\" + image.Name);

            // if you want to move a copy of the image use this
            File.Copy(desktopPath + "\\"+ image.Name, desktopPath + "\\folder\\" + image.Name, true);
        }
      }

    public static IEnumerable<FileInfo> GetFilesByExtensions(DirectoryInfo dir, params string[] extensions)
    {
        if (extensions == null)
            throw new ArgumentNullException("extensions");

        var files = dir.EnumerateFiles();
        return files.Where(f => extensions.Contains(f.Extension));
    }
Timon Post
  • 2,779
  • 1
  • 17
  • 32
  • Thank you man! I this is what I mean ... I get no error but my folder is still empty why? – newatstackoverflow Feb 20 '17 at 14:14
  • Oh sorry I did not work with string searchPattern = "*.bmp" + "*.jpg"; – newatstackoverflow Feb 20 '17 at 14:15
  • I've tried it again with string searchPattern = "*.bmp" + "*.jpg"; foreach (string img in Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), searchPattern)) { FileInfo file = new FileInfo(img); file.MoveTo(@"C:\Users\User\Desktop\Images"); } and my folder is still empty i dont know why – newatstackoverflow Feb 20 '17 at 14:18
  • I'm on it wait a second – Timon Post Feb 20 '17 at 14:19
  • Thanks but why File.Copy(desktopPath + "//" + image.Name, desktopPath + "//folderr//" + image.Name, true); and not move? I don't understand the part with +//folder// I'm getting errors – newatstackoverflow Feb 20 '17 at 14:49
  • File.Copy creates a copy of the image and move's that copy to the (desktop path + foldername + name_of_destination file) you can use File.Move but this is just another approach – Timon Post Feb 20 '17 at 14:52
  • I've updated the question now you can choose to move the copy or just the image itself without – Timon Post Feb 20 '17 at 14:59
1

Please try this:
You can filter files in a specific directory and then looop through search results to move each file, you might be able to modify search pattern to match on a number of different image file formats

var files = Directory.GetFiles("PathToDirectory", "*.jpg");
    foreach (var fileFound in files)
    {
        //Move your files one by one here
        FileInfo file = new FileInfo(fileFound);
        file.MoveTo(@"C:\Users\User\Desktop\folder\" + file.Name);
    }
J. Tuc
  • 424
  • 2
  • 10