Click here to Skip to main content
15,881,588 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Greetings,

is it possible to move file automatically when a new jpg files created in a defined folder to a different folder? i think i can use filesystemwatcher but i couldn't do it.

C#
fileSystemWatcher1.EnableRaisingEvents = true;
         string dst = @"c:\deneme1";
         string src = @"c:\deneme2";

         if (Directory.GetFiles(src).Length == 0)
         {
             string[] move = Directory.GetFiles(src, "*.jpg");
             foreach (string file in move)
             {
                 string file = Path.GetFileName(file);
                 string dstfile = Path.Combine(dst, file);
                 File.Move(file, dstfile);


             }
         }
         else
         {
             string[] move2 = Directory.GetFiles(src, "*.jpg");
             foreach (string file in move2)
             {
                 string file2 = Path.GetFileName(file);
                 string dstfile = Path.Combine(dst, file2);
                 File.Move(file, dstfile);
             }

         }



this is not working perfectly. when i start this code. it will move every jpg files to "deneme1" folder but when a new jpg file will appear in "deneme2" folder when code is working, it won't move this new file. how can i move these new files too?
Posted
Comments
BillWoodruff 2-Jan-15 20:15pm    
When you say "it won't move this new file," what exactly do you observe ? Do you get an error message ? Does your code execute but "nothing happens" ? Have you verified you are getting the 'Changed or 'Created FileSystemWatcher Event fired ?
Member 10587827 2-Jan-15 20:19pm    
just nothing happens. there is no error message. i think code doesn't check new files for move them
BillWoodruff 2-Jan-15 20:49pm    
Have you put break-points in your 'Changed and 'Created FileSystemWatcher Event Handlers and observed whether your code is getting called ?
Sergey Alexandrovich Kryukov 2-Jan-15 20:16pm    
We can discuss is, but the main question is: why?
—SA
Member 10587827 2-Jan-15 20:19pm    
i just need it for my project

Looks like you are not using the FileSystemWatcher functionality at all, or you don't show all your code. I can't see that you are using the events in your code.

This example should do the job for you. MSDN has more extensive examples: FileSystemWatcher Class[^]
C#
FileSystemWatcher fileSystemWatcher1 = new FileSystemWatcher();
fileSystemWatcher1.NotifyFilter = NotifyFilters.FileName;
fileSystemWatcher1.Filter = "*.jpg";
fileSystemWatcher1.Path = @"c:\temp\deneme2";
fileSystemWatcher1.Created += fileSystemWatcher1_Created;
fileSystemWatcher1.EnableRaisingEvents = true;

void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e)
{
    if (e.ChangeType == WatcherChangeTypes.Created)
    {
        string dest = Path.Combine(@"C:\temp\deneme1", e.Name);
        if (File.Exists(dest))
        {
            File.Delete(e.FullPath);    // For example
        }
        else
        {
            File.Move(e.FullPath, dest);
        }
    }
}
 
Share this answer
 
Comments
BillWoodruff 3-Jan-15 6:39am    
I think your code is deleting the wrong file, the newly created file. I think you must have intended to check if the file with the same name is already in the move-to destination, and then delete it before moving the newly created file ?
George Jonsson 4-Jan-15 1:15am    
I actually mean to delete the new file if a file with the same name already exists in the destination directory. No need to move file in this case.
If this is a desired behaviour or not depends on the situation.
Delete the existing file and then move the new file works as well, but might be a waste of time and resources.
this looks work great for me

C#
bool xxx;
            fileSystemWatcher1.EnableRaisingEvents = true;
            string dst = @"c:\deneme1";
            string src = @"c:\deneme2";
            Find:
            string[] move = Directory.GetFiles(src, "*.jpg",SearchOption.TopDirectoryOnly);
            if (move.Length != 0)
            {
                xxx = true;
                foreach (string file in move)
                {
                    string a = Path.GetFullPath(file);
                    string files = Path.GetFileName(file);
                    string dstfile = Path.Combine(dst, files);
                    File.Move(a, dstfile);

                }

            }

            else
            {
                xxx = false;
                Console.WriteLine("Empty");
                System.Threading.Thread.Sleep(5000);

            }
            goto Find;
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 2-Jan-15 21:57pm    
Makes no sense at all. You sleep for 5 seconds. It means it does not really work. If you think it's great, I'm really sorry for you.
—SA
George Jonsson 3-Jan-15 0:04am    
What he did was to create a loop with a goto statement and checks the directory for files every 5 sec. Not the solution I would have chosen.
Sergey Alexandrovich Kryukov 3-Jan-15 1:01am    
Of course not :-)
—SA
George Jonsson 2-Jan-15 23:59pm    
Every time a sleep is added to "solve" a problem in the code, the actual problem is not actually solved. We have all done this one time or another, but it usually comes back and bites you in the ass later.
Basically what you have done here is to create an infinite loop using a goto statement.
I have only used goto when I did some simple basic programs in the 80's.
Since then I have avoided using it.
Sergey Alexandrovich Kryukov 3-Jan-15 1:01am    
Exactly.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900