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)

File move is failing in Batch operation

(0) ShareShare
ReportReport
Posted on by 717

Hi All,

System.IO.file::Move fails : The process cannot access the file because it is being used by another process. in ax 2012.

Can somebody guide me how to resolve this issue.

Regards,

Pradeep

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Iulian Cordobin Profile Picture
    8,201 on at

    So, if the code is written to be run in batch, this means that the code is running on the AOS server. As a result the files paths should be either valid paths on the server or UNC paths (shared paths), but I guess this is not the issue here since the code can locate the file, as it would seem from the error you get (that is not complaining that the file can't be found).

    The error should provide enough information with the fact that you actually have the file open in some manner that doesn't allow moving (either in AX for reading as a stream, or in another process, like Excel?). Can you check this first and make sure your code is not the problem (for instance not closing the file handle you have opened, or editing the file). Also, a question, this file would be what type? csv, txt, etc?

  • Verified answer
    Dick Wenning Profile Picture
    8,705 Moderator on at

    when completing the write operation, close the file correct by using the finalize() function.

  • Pradeep_Prakash Profile Picture
    717 on at

    Thanks Iulian and Dick. Can you please tell me, which finalize should be called?

    Regards,

    Pradeep

  • Pradeep_Prakash Profile Picture
    717 on at

    Below is my code

    while (li.more())

           {

               ttsbegin;

               filename = li.value();

               this.Update();

               ttsCommit;  // 160107/pewi

               [filePath, fileName, fileExt] = fileNameSplit(Filename);

               System.IO.File::Move(filePath + fileName + fileExt, filePath + "Arkiv\\" + fileName + fileExt);

              li.next();

              ttscommit;

           }

  • Suggested answer
    Iulian Cordobin Profile Picture
    8,201 on at

    For sure you don't want to move the file in the middle of parsing it.

    You can try to move the following code outside the while statement, and also call a .Close() on the file or stream variable you have in your code (but it's not shown in your post)

    [filePath, fileName, fileExt] = fileNameSplit(Filename);

    System.IO.File::Move(filePath + fileName + fileExt, filePath + "Arkiv\\" + fileName + fileExt);

  • Pradeep_Prakash Profile Picture
    717 on at

    Do you want me to loop all the files again outside the loop and move to the new path.

    What is the advantage of doing that?

    Regards,

    Pradeep

  • Pradeep_Prakash Profile Picture
    717 on at

    Iulian,

    Can you tell me how to use .close() on the file? I'm not very sure on that.

    Regards,

    Pradeep

  • Pradeep_Prakash Profile Picture
    717 on at

    try

       {

           filelist = ACA_AutogiroImport::buildFilesList(ACA_AutogiroParameters::find().Filename_Import);

           li = new ListIterator(filelist);

           liLocal = new ListIterator(filelist);

           li.begin();

           while (li.more())

           {

               ttsbegin;

               filename = li.value();

               //this.Update();

               ttsCommit;  // 160107/pewi

               //[filePath, fileName, fileExt] = fileNameSplit(Filename);

               perm.assert();

                   sw = new System.IO.StreamWriter(filePath + fileName + fileExt);

               //System.IO.File::Move(filePath + fileName + fileExt, filePath + "Arkiv\\" + fileName + fileExt);

                   sw.Flush();

                   sw.Close();

                   sw.Dispose();

                   CodeAccessPermission::revertAssert();

               //ACA_AutogiroImport::copyFileClientServer(Filename);

               //ACA_AutogiroImport::deleteFileClientServer(Filename);

               li.next();

               // 160107/pewi ttscommit;

           }

           while (liLocal.more())

           {

               filename = liLocal.value();

               [filePath, fileName, fileExt] = fileNameSplit(Filename);

               System.IO.File::Move(filePath + fileName + fileExt, filePath + "Arkiv\\" + fileName + fileExt);

               liLocal.next();

           }

       }

    The new change also doesn't seem to work. Can somebody suggest, If I'm missing something.

  • Pradeep_Prakash Profile Picture
    717 on at

    getting below error with this new code

    System.UnauthorizedAccessException: Access to the path '\\10.103.97.11\Ax\DiverseFiler\Autogiro\Import\BFEP.UAGAG.K0145672.D151029.T113111.v001' is denied.

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

      at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)

      at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)

      at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)

      at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize)

      at System.IO.StreamWriter..ctor(String path)

      at Dynamics.Ax.Application.ACA_AutogiroImport.Run() in ACA_AutogiroImport.run.xpp:line 38

      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)

  • Suggested answer
    Iulian Cordobin Profile Picture
    8,201 on at

    Alright, so first thing.

    The new code you've pasted tells another whole story to actually what you are trying to do: looks like, you get a list of files to move, you parse the the list, and move the file.

    However, I just noticed a first issue: what is with the this.update() statement. It has no logic there - please explain what you are trying to achieve.

    Second, the initial code, from the standpoint of what I've just described looks alright now the way it was written, so for sure there must be some other problem, like access rights (the new error message you are getting let's us know that).

    Can you please make sure the following are done:

    1. you can use the inital code you've pasted;

    2. look into the this.update() - it looks like you are not changing any value so why updating (whatever it is you're updating)?

    3. make sure you run this with only one file (to be moved) for starters, that is not opened by another external application and that you have RW access to the initial file as well as the path you are writing to;

    4. make sure you do not have an already existing file in your destination. The Move does not overwrite an existing file and it fails with an error. So, it would be best to check first in the code if the file exists (using System.IO.File::Exists())

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
Priya_K Profile Picture

Priya_K 4

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#3
Ali Zaidi Profile Picture

Ali Zaidi 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans