25

I am moving some images (filenames are(1).PNG, (2).PNG and so on) from one directory to another. I am using the following code:

for (int i = 1; i < n; i++)
{
    try
    {
        from = "E:\\vid\\(" + i + ").PNG";
        to = "E:\\ConvertedFiles\\" + i + ".png";
        File.Move(from, to); // Try to move
        Console.WriteLine("Moved"); // Success
    }
    catch (IOException ex)
    {
        Console.WriteLine(ex); // Write error
    }
}

However, I am getting the following error:

A first chance exception of type System.IO.FileNotFoundException occurred in mscorlib.dll

System.IO.FileNotFoundException: Could not find file 'E:\vid\(1).PNG'.

Also, I am planning to rename the files so that the converted file name will be 00001.png, 00002.png, ... 00101.png and so on.

B--rian
  • 5,578
  • 10
  • 38
  • 89
MKS
  • 736
  • 2
  • 14
  • 33
  • 4
    Silly question, but you're sure that "E:\vid(1).PNG" exists? – Rotem Nov 29 '12 at 08:55
  • 1
    `Could not find file 'E:\vid(1).PNG'` but your code says `E:\\vid\\(1).png` - so there's a slash missing here somewhere, no? – Andras Zoltan Nov 29 '12 at 08:59
  • Something is wrong: your code expects the files to be named `(_num_).png` and reside in the `E:\vid` directory. The error messages states that you were trying to "find" the file `vid(_num_).png` in the `E:\\` directory. So error message and code don't match. Please make sure you posted the actual code and error message that match. – Christian.K Nov 29 '12 at 09:00
  • 1
    Something wrong in your code above? the file you are trying to move is named `(1).png` inside the folder `E:\vid`, not `E:\vid(1).png`. Which is the right one? – Steve Nov 29 '12 at 09:02

6 Answers6

26

I suggest you to use '@' in order to escape slashes in a more readable way. Use also Path.Combine(...) in order to concatenate paths and PadLeft in order to have your filenames as your specifics.

for (int i = 1; i < n; i++)
{
    try
    {
        from = System.IO.Path.Combine(@"E:\vid\","(" + i.ToString() + ").PNG");
        to = System.IO.Path.Combine(@"E:\ConvertedFiles\",i.ToString().PadLeft(6,'0') + ".png");

        File.Move(from, to); // Try to move
        Console.WriteLine("Moved"); // Success
    }
    catch (IOException ex)
    {
        Console.WriteLine(ex); // Write error
    }
}
Hille
  • 2,123
  • 22
  • 39
Tobia Zambon
  • 7,479
  • 3
  • 37
  • 69
4

Why don't you use something like this?

var folder = new DirectoryInfo(@"E:\vid\"));

if (folder.Exists)
{
    var files = folder.GetFiles(".png");
    files.toList().ForEach(f=>File.Move(from,to));
}
Hille
  • 2,123
  • 22
  • 39
Keysharpener
  • 486
  • 3
  • 14
1

The exception means that the file E:\vid(1).PNG doesn't exist. Do you mean E:\vid1.PNG?

Use System.IO.Path class for constructing paths, it's better than concatenating strings. You don't have to worry about escapting backslashes.

Jakub Konecki
  • 45,581
  • 7
  • 87
  • 126
1
i.ToString()

might help you. You are passing

from = "E:\\vid\\(" + i + ").PNG";
to = "E:\\ConvertedFiles\\" + i + ".png";

I as integer and concatenation doesn't work due to that
and instead of using \\, add @ like this

from = @"E:\vid\(" + i + ").PNG";
Hille
  • 2,123
  • 22
  • 39
Gustav Klimt
  • 430
  • 3
  • 14
  • 2
    `ToString()` is called implicitly when contcatenating with a string. If this was the problem, his code wouldn't even compile. – Rotem Nov 29 '12 at 08:59
1

I just ran this in Visual Studio. It worked.

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


namespace ConsoleApplication2

{

    class Program
    {
        static void Main()
        {
            int n = 3;
            for (int i = 1; i < n; i++)
            {
                string from = "C:\\vid\\(" + i + ").PNG";
                string to = "C:\\ConvertedFiles\\" + i + ".png";
                {
                    try
                    {
                        File.Move(from, to); // Try to move
                        Console.WriteLine("Moved"); // Success
                    }
                    catch (System.IO.FileNotFoundException e)
                    {
                        Console.WriteLine(e); // Write error
                    }
                }
            }
        }
    }

}

Maybe when you were moving files into vid directory to begin the test, windows shaved off the parenthesis. (1).png became 1.png... I got a file not found error from that phenomenon... otherwise, your code is solid. My version is almost identical.

Bura Chuhadar
  • 3,653
  • 1
  • 14
  • 17
BrianK
  • 89
  • 1
  • 4
0
var folder = new DirectoryInfo(sourcefolder);

if (folder.Exists)
{
    var files = folder.GetFiles("*.png");
    files.ToList().ForEach(f => File.Move(sourcefolder + f, newFolderName + f));
}

I believe this will help.

Hille
  • 2,123
  • 22
  • 39