locked
File Move with in time limits RRS feed

  • Question

  • User842257015 posted

    HI All,

    I have a requirement like to move/copy a file from folder to another folder with in time limits.

    Folder: Input

     file Name: sample.txt

    Folder: Output

    the same sample.txt,

    just assume that sample.txt has 500mb size, it takes 5 minutes to complete, but I wanna set time out for 3 minutes.

    if it takes more than 3minutes, stop the moving process, like sample.txt file resides in same input folder. 

    Thanks, 

    Saturday, July 22, 2017 7:06 PM

Answers

  • User1068175894 posted

    Before giving you the answer consider that moving files in the same drive doesn't transfer the data, only affect the directory entries in the hard drive, so it would be very fast,

    Other simpler option you can implement is to determine if copying/moving the file based on the file size, that way you dont have to wait for the timeout.

    Well here is the code for copying with timeout, this one is implemented using an async method with a timed cancellation token:

    static async Task Copy(string destFilePath, string sourceFilePath, int timeoutSecs)
    {
        var cancellationSource = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutSecs));
    
        using (var dest = File.Create(destFilePath))
        using (var src = File.OpenRead(sourceFilePath))
        {
            await src.CopyToAsync(dest, 81920, cancellationSource.Token);
        }
    }

    then you can do:

    try
    {
        await Copy(@"c:\temp\test2.bin", @"c:\temp\test.bin", 60);
        Console.WriteLine("finished..");
    }
    catch (OperationCanceledException ex)
    {
        Console.WriteLine("cancelled..");
    }
    catch (Exception ex)
    {
        Console.WriteLine("error..");
    }

    thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 23, 2017 4:04 AM

All replies

  • User1068175894 posted

    Before giving you the answer consider that moving files in the same drive doesn't transfer the data, only affect the directory entries in the hard drive, so it would be very fast,

    Other simpler option you can implement is to determine if copying/moving the file based on the file size, that way you dont have to wait for the timeout.

    Well here is the code for copying with timeout, this one is implemented using an async method with a timed cancellation token:

    static async Task Copy(string destFilePath, string sourceFilePath, int timeoutSecs)
    {
        var cancellationSource = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutSecs));
    
        using (var dest = File.Create(destFilePath))
        using (var src = File.OpenRead(sourceFilePath))
        {
            await src.CopyToAsync(dest, 81920, cancellationSource.Token);
        }
    }

    then you can do:

    try
    {
        await Copy(@"c:\temp\test2.bin", @"c:\temp\test.bin", 60);
        Console.WriteLine("finished..");
    }
    catch (OperationCanceledException ex)
    {
        Console.WriteLine("cancelled..");
    }
    catch (Exception ex)
    {
        Console.WriteLine("error..");
    }

    thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, July 23, 2017 4:04 AM
  • User842257015 posted

    Thanks, I updated little bit in order to accomplish my task. 

    I will remove the file in the source if it successfully copied from source to destination.

    If it canceled in between some buffered content in the destination folder, so I will remove those file if it not fully copied from the source.

    var fileMoveStatus = await _fileCopyProcess.Copy(source, destination, 100);
    if (fileMoveStatus.isMoved)
    {
       // remove file from source
    }else
    {
      // uncopied file removed from destination
    }
    
    public async Task<(bool isMoved, string fileName)> Copy(string sourceFilePath, string destFilePath, int timeoutSecs)
            {
                bool isMoved = false;
                string sourceFileName = string.Empty;
                string fileName = string.Empty;
                try
                {
                    if (File.Exists(sourceFilePath))
                    {
                        sourceFileName = Path.GetFileName(sourceFilePath);
                        await _fileCopy.CopyAsync(sourceFilePath, destFilePath, timeoutSecs);
                        fileName = Path.GetFileName(destFilePath);
                        isMoved = true;
                    }
                }
                catch (OperationCanceledException ex)
                {
                    fileName = sourceFileName; ;
                }
                catch (Exception ex)
                {
                    fileName = sourceFileName;
                }
                return (isMoved, fileName);
            }
    public async Task CopyAsync(string sourceFilePath, string destFilePath, int timeoutSecs)
            {
                var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(timeoutSecs));
                using (var dest = File.Create(destFilePath))
                using (var src = File.OpenRead(sourceFilePath))
                {
                    await src.CopyToAsync(dest, 81920, cancellationTokenSource.Token);
                }
            }



    Thanks,

    Tuesday, July 25, 2017 5:49 AM