web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Error Moving File

(0) ShareShare
ReportReport
Posted on by 563

Dear All,

I'm beginner here, customizing Dynamic AX 2012 R3

I create a class for batch job. The class process a file that read and move to other folder

and I got an error 

System.IO.IOException: The process cannot access the file 'E:\AXFILE\AP\Upload\DealsPurchase.csv' because it is being used by another process.

   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

   at System.IO.File.InternalDelete(String path, Boolean checkHost)

   at Dynamics.Ax.Application.VDI_MoveJournalFile.Moveuploadedfile(String _filename, VDI_JournalStatus _status, VDI_Module _module) in VDI_MoveJournalFile.MoveUploadedFile.xpp:line 32

   at Dynamics.Ax.Application.VDI_UploadAPJournalDeals.Run() in VDI_UploadAPJournalDeals.run.xpp:line 19

   at Dynamics.Ax.Application.BatchRun.runJobStaticCode(Int64 batchId) in BatchRun.runJobStaticCode.xpp:line 54

   at Dynamics.Ax.Application.BatchRun.runJobStatic(Int64 batchId) in BatchRun.runJobStatic.xpp:line 13

   at BatchRun::runJobStatic(Object[] )

   at Microsoft.Dynamics.Ax.Xpp.ReflectionCallHelper.MakeStaticCall(Type type, String MethodName, Object[] parameters)

   at BatchIL.taskThreadEntry(Object threadArg)


Any suggestion, please?

Thanks in advance

Regards,

Bintang

*This post is locked for comments

I have the same question (0)
  • Martin Dráb Profile Picture
    237,967 Most Valuable Professional on at

    The error says that you can't move the file because it's currently being used by another process. For example, somebody may be working on it in Excel.

    Either stop trying to delete the file, wait until it's available or kill all processes that you're using it.

  • Bintang Profile Picture
    563 on at

    Hi Crispin John Augustine,

    Here is my code , using System.IO.File, batch job refuses WinApi

    public boolean MoveUploadedFile(Str60 _filename,VDI_JournalStatus _status,VDI_Module _module)
    {
        FilenameOpen    location;
        FilenameOpen    destination;
    
        System.DateTime dateTime    = System.DateTime::get_UtcNow();
        str timeStr                 = dateTime.ToString("_yyyyMMdd_HHmmss");
        ;
        if(_module==VDI_Module::GL){
            location    = @"E:\AXFILE\GL\Upload\"+_filename+".csv";
    
            if(_status == VDI_JournalStatus::Success){
                destination = @"E:\AXFILE\GL\Finish\"+_filename+timeStr+".csv";
            }
            else{
                destination = @"E:\AXFILE\GL\Error\"+_filename+timeStr+".csv";
            }
        }
        else if(_module==VDI_Module::AP){
            location    = @"E:\AXFILE\AP\Upload\"+_filename+".csv";
    
            if(_status == VDI_JournalStatus::Success){
                destination = @"E:\AXFILE\AP\Finish\"+_filename+timeStr+".csv";
            }
            else{
                destination = @"E:\AXFILE\AP\Error\"+_filename+timeStr+".csv";
            }
        }
        try{
            if(System.IO.File::Exists(location)){
                System.IO.File::Copy(location,destination);
                System.IO.File::Delete(location);
                return true;
            }
            warning(strFmt("File "+location+"not found"));
            return false;
        }
        catch(Exception::Error){
            error(strFmt("Error while moving file %1",Exception::Error));
            return false;
        }
        return false;
    }


  • Bintang Profile Picture
    563 on at

    Dear Martin Dráb,

    This is Batch Job that run on server , any idea to make Batch Job run clearly?

  • Verified answer
    Martin Dráb Profile Picture
    237,967 Most Valuable Professional on at

    Your code is unnecessary complicated, because you duplicate lots of things. Let me simplify it, fix some issues and replace Copy + Delete with Move.

    public boolean MoveUploadedFile(Str60 _filename,VDI_JournalStatus _status,VDI_Module _module)
    {
        System.DateTime dateTime    = System.DateTime::get_UtcNow();
        str timeStr                 = dateTime.ToString("_yyyyMMdd_HHmmss");
        
        str filenameWithTime = strFmt("%1%2.csv", _filename, timeStr);
        str baseFolder = strFmt(@"E:\AXFILE\%1", this.subfolderForVDIModule(_module));
        str destinationFolder = (_status == VDI_JournalStatus::Success ? "Finish" : "Error");
        
        Filename location = strFmt(@"%1\Upload\%1", baseFolder, filenameWithTime);
        FileName destination = strFmt(@"%1\%2\%3", baseFolder, destinationFolder, filenameWithTime);
    
        try
        {
            if (System.IO.File::Exists(location))
            {
                System.IO.File::Move(location, destination);
                return true;
            }
            warning(strFmt("File %1 not found", location));
        }
        catch (Exception::Error)
        {
            error(strFmt("Error while moving file %1", location));
        }
        
        return false;
    }
    
    private str subfolderForVDIModule(VDI_Module _module)
    {
        switch (_module)
        {
            case VDI_Module::GL:
                return "GL";
            case VDI_Module::AP:
                return "AP";
            default:
                throw error("Invalid module");
        }
    }
  • Suggested answer
    Vilmos Kintera Profile Picture
    46,149 on at

    The file locks could be checked at Control Panel > Administrative tasks > Computer management > System Tools > Shared Folders > Open files. Check it on the server where your E: drive is on to see what process has the hold on the file.

    It is also possible that the Copy command did not finish and release the file lock fast enough, try adding a sleep(1000) maybe between the copy and the delete, if you cannot spot any locking.

    Alternatively if you do spot that the file is open by another application, you could forcibly try to close it by using the "net file fileid /close" command.

    technet.microsoft.com/.../cc732037(v=ws.11).aspx

    Please note that fileid is not the file name, but a unique identifier held by Windows, which you need to query beforehand.

  • Bintang Profile Picture
    563 on at

    Dear Martin Dráb,

    Thank you for simplified my code, but still get same error

    System.IO.IOException: The process cannot access the file because it is being used by another process.

      at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

      at System.IO.File.InternalMove(String sourceFileName, String destFileName, Boolean checkHost)

      at Dynamics.Ax.Application.VDI_MoveJournalFile.Moveuploadedfile(String _filename, VDI_JournalStatus _status, VDI_Module _module) in VDI_MoveJournalFile.MoveUploadedFile.xpp:line 18

      at Dynamics.Ax.Application.VDI_UploadLedgerJournal.Run() in VDI_UploadLedgerJournal.run.xpp:line 19

      at Dynamics.Ax.Application.BatchRun.runJobStaticCode(Int64 batchId) in BatchRun.runJobStaticCode.xpp:line 54

      at Dynamics.Ax.Application.BatchRun.runJobStatic(Int64 batchId) in BatchRun.runJobStatic.xpp:line 13

      at BatchRun::runJobStatic(Object[] )

      at Microsoft.Dynamics.Ax.Xpp.ReflectionCallHelper.MakeStaticCall(Type type, String MethodName, Object[] parameters)

      at BatchIL.taskThreadEntry(Object threadArg)

    Dear Vilmos Kintera,

    I added sleep() too after System.IO.File::Move(location, destination); but didn't solved my error

    or maybe any procedure for updating class on batch job ?

  • Martin Dráb Profile Picture
    237,967 Most Valuable Professional on at

    We at least ruled out that it's caused by the combination of copy and delete. (By the way, that's also what Vilmos was talking about; adding sleep() after Move() isn't useful.)

    Seriously, look at which process is holding a lock on the file. I can't do it for you from here.

  • Suggested answer
    Vilmos Kintera Profile Picture
    46,149 on at

    As per above, I've sent you the information on how to check file locks. You may even do the same from AX code with WinAPI, or .Net reflection.

    msdn.microsoft.com/.../winapiserver.filelocked.aspx

    blogs.msdn.microsoft.com/.../ax-application-files-locked-by-another-process

    community.dynamics.com/.../check-to-see-if-a-file-is-locked-by-another-process-error-the-process-cannot-access-the-file-39-filelocation-39-because-it-is-being-used-by-another-process

    These should be more than enough information to get you going.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans