Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug FileUtil.Replace() cannot find the file specified, but file specified exists.

Discussion in 'Editor & General Support' started by Tomayopan, Mar 18, 2022.

  1. Tomayopan

    Tomayopan

    Joined:
    Dec 9, 2020
    Posts:
    11
    i'm currently trying to create a system that copies data found in a ScriptableObject that holds serialized data to a specific folder output. The main struggle i've found is that the call i'm doing on FileUtil.Replace() isnt working properly.

    A lot of other systems in the project that are based off my original process work just fine (The one i'm trying to create is based off the existing ones as a template of sorts). (The current look of the code, Both the system that copies the files to an output folder, and the scriptable object data structure can be found here: CopyProcess, DataStructure. For the specific copýing part of the CopyProcess, it can be found here)

    At first i thought something may have been wrong on my project, such as FileUtil being broken or some assets being broken, but trying to do a similar system with a context menu, there is no error in the copying file. and the file indeed appears on "TestFolder" as "Languagefile.json".

    Code (CSharp):
    1. public static class Test
    2. {
    3.     //File that belongs to this GUID is in "Assets/LanguageTesting/Languagefile.json"
    4.     public static string GUID = "7f27d5c2ba97b6d4ebe8db02352d0eab";
    5.  
    6.     [MenuItem("Test/DoSomething")]
    7.     public static void DoSomething()
    8.     {
    9.         string origPath = AssetDatabase.GUIDToAssetPath(GUID);
    10.         string origPathFileName = Path.GetFullPath(origPath);
    11.         string origFileName = Path.GetFileName(origPathFileName);
    12.  
    13.         //Dest path would be a "TestFolder" folder.
    14.         string destPath = Path.Combine("TestFolder", origFileName);
    15.  
    16.         FileUtil.ReplaceFile(origPath, destPath);
    17.     }
    18. }
    Even if i use the exact code that's on "DoSomething()" on my process to copy files over, it still errors with not being able to find the file specified.

    Is there any specific reason as to why FileUtil would only fail to replace when in this specific scenario? does FileUtil work with async? I was thinking on using the debugger to find the issue, but both DestPath and OrigPath are valid on the menu item, but in the other process theyre not. Even then, FileUtil's Replace method is external, so i wouldn't go very far using debuggers.
     
  2. Tomayopan

    Tomayopan

    Joined:
    Dec 9, 2020
    Posts:
    11
    Turns out the reason for the error was in the HasInvalidFiles() method, due to not disposing of the Stream nor the StreamReader.