Home > Back-end >  Files disappear after they fail to be moved
Files disappear after they fail to be moved

Time:11-24

We have a process where people scan documents with photocopiers and drop them in a certain directory on our file server. We then have a hourly service within an .NET Core app, that scans the directory, grabs the file and moves them according to their file name to a certain directory. Here comes the problems.

The code looks something like that:

private string MoveFile(string file, string commNumber)
        {
            var fileName = Path.GetFileName(file);
            var baseFileName = Path.GetFileNameWithoutExtension(fileName).Split("-v")[0];
            // 1. Check if the file already exists at destination
            var existingFileList = luxWebSamContext.Documents.Where(x => EF.Functions.Like(x.DocumentName, "%"   Path.GetFileNameWithoutExtension(baseFileName)   "%")).ToList();
            
            // If the file exists, check for the current version of file
            if (existingFileList.Count > 0)
            {
                var nextVersion = existingFileList.Max(x => x.UploadVersion)   1;                
                var extension = Path.GetExtension(fileName);
                fileName = baseFileName   "-v"   nextVersion.ToString()   extension;
            }   

            var from = @file;
            var to = Path.Combine(@destinationPath, commNumber,fileName);

            try
            {
                log.Info($"------ Moving File! ------ {fileName}");
                Directory.CreateDirectory(Path.Combine(@destinationPath, commNumber));
                File.Move(from, to, true);
                return to;
            }
            catch (Exception ex)
            {
                log.Error($"----- Couldn't MOVE FILE: {file} ----- commission number: {commNumber}", ex);

The interesting part is in the try-block, where the file move takes place. Sometmes we have the problem that the program throws the following exception

2021-11-23 17:15:37,960 [60] ERROR App ----- Couldn't MOVE FILE: \PATH\PATH\PATH\Filename_423489120.pdf ----- commission number: 05847894 System.IO.IOException: The process cannot access the file because it is being used by another process. at System.IO.FileSystem.MoveFile(String sourceFullPath, String destFullPath, Boolean overwrite) at System.IO.File.Move(String sourceFileName, String destFileName, Boolean overwrite)

So far so good. I would expect that after the file cannot be moved, it remains in the directory from it was supposed to be moved. But that's not the case. We had this issue yesterday afternoon and after I looked for the file, it was gone from the directory.

Is this the normal behaviour of the File.Move() method?

CodePudding user response:

Typically, File.Move() only removes the source file, once the destination file is successfully transferred in place. So the answer to your question is no, it cannot be purely the File.Move(). The interesting part is, why is this file locked? Probaby because some file stream is still open and blocking access to the file. Also, do you have multiple instances of the copy process services running? This may cause several services trying to access the file simultaneously, causing the exception you posted.

There must be a different cause making the files disappear because the File.Move() will certainly not remove the file when the copy process did not succeed.

For debugging purposes, you may try and open the file with a lock on it. This will fail when a different process locks the file providing you a little bit more information.

CodePudding user response:

First to your question:

Is this the normal behaviour of the File.Move() method?

No, thats not the expected behaviour. The documentation says:

Moving the file across disk volumes is equivalent to copying the file and deleting it from the source if the copying was successful.

If you try to move a file across disk volumes and that file is in use, the file is copied to the destination, but it is not deleted from the source.

Your Exception says, that another process is using the file in the same moment. So you should check, whether other parts of your application may performs a Delete, or someone (if this scenario is valid) is deleting files manually from the file system.

  • Related