{"record":{"id":"ac7598a96dc09628","repo":"peass-ng/PEASS-ng","slug":"error-dir-not-empty","errorCode":"ERROR_DIR_NOT_EMPTY","errorMessage":"({145}) The directory is not empty: [{junctionPath}]","messagePattern":"\\((.+?)\\) The directory is not empty: \\[(.+?)\\]","errorType":"exception","errorClass":"DirectoryNotEmptyException","httpStatus":null,"severity":"error","filePath":"winPEAS/winPEASexe/winPEAS/3rdParty/AlphaFS/Filesystem/Directory Class/Directory Core Methods/Directory.CreateJunctionCore.cs","lineNumber":103,"sourceCode":"         File.ThrowIOExceptionIfFsoExist(transaction, false, directoryPath, pathFormat);\n         File.ThrowIOExceptionIfFsoExist(transaction, false, junctionPath, pathFormat);\n\n\n         // Check for existing directory junction folder.\n         if (File.ExistsCore(transaction, true, junctionPath, pathFormat))\n         {\n            if (overwrite)\n            {\n               DeleteDirectoryCore(transaction, null, junctionPath, true, true, true, pathFormat);\n\n               CreateDirectoryCore(true, transaction, junctionPath, null, null, false, pathFormat);\n            }\n\n            else\n            {\n               // Ensure the folder is empty.\n               if (!IsEmptyCore(transaction, junctionPath, pathFormat))\n                  throw new DirectoryNotEmptyException(junctionPath, true);\n\n               throw new AlreadyExistsException(junctionPath, true);\n            }\n         }\n\n\n         // Create the folder and convert it to a directory junction.\n         CreateDirectoryCore(true, transaction, junctionPath, null, null, false, pathFormat);\n\n         using (var safeHandle = OpenDirectoryJunction(transaction, junctionPath, pathFormat))\n            Device.CreateDirectoryJunction(safeHandle, directoryPath);\n\n\n         // Copy the target date and time stamps to the directory junction.\n         if (copyTargetTimestamps)\n            File.CopyTimestampsCore(transaction, true, directoryPath, junctionPath, true, pathFormat);\n\n","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/peass-ng/PEASS-ng/blob/53fb989abc2219826385683a6fee826bd6cd38d6/winPEAS/winPEASexe/winPEAS/3rdParty/AlphaFS/Filesystem/Directory Class/Directory Core Methods/Directory.CreateJunctionCore.cs#L85-L121","documentation":"Directory.CreateJunctionCore throws DirectoryNotEmptyException (Win32 error 145, ERROR_DIR_NOT_EMPTY) when the junction target path already exists as a directory but still contains files or subdirectories. AlphaFS refuses to convert a non-empty existing folder into a junction, because doing so would make the existing content unreachable through the junction point. The check is done via IsEmptyCore before throwing.","triggerScenarios":"Calling Directory.CreateJunction (or CreateJunctionTransacted) with a junctionPath that already exists as a directory containing one or more files/subdirectories. The path exists branch of CreateJunctionCore runs IsEmptyCore, which returns false and raises this exception instead of AlreadyExistsException.","commonSituations":"Re-running a setup script that previously partially created a junction and left temp files inside the target folder; pointing a junction at an existing data directory (e.g. moving a profile/cache folder) without emptying it first; a failed prior junction deletion that left contents behind.","solutions":["Empty the target directory (delete or move its contents) before calling CreateJunction.","Check Directory.IsEmpty(path) before creating the junction and delete the folder if it is non-empty but disposable.","Delete the existing directory entirely and let CreateJunction create a fresh one (CreateJunctionCore calls CreateDirectoryCore afterward).","Catch DirectoryNotEmptyException and surface a clear message telling the user the junction target must be empty.","If the existing folder's contents must be preserved, move them to the real target location first, then create the junction at the original path."],"exampleFix":"// before\nDirectory.CreateJunction(@\"C:\\link\", @\"D:\\realTarget\"); // fails if C:\\link exists and is non-empty\n// after\nif (Directory.Exists(linkPath) && !Directory.IsEmpty(linkPath))\n    Directory.Delete(linkPath, true);\nDirectory.CreateJunction(linkPath, realTarget);","handlingStrategy":"validation","validationCode":"if (Directory.Exists(junctionPath) && !Directory.IsEmpty(junctionPath))\n    throw new InvalidOperationException($\"Junction target {junctionPath} must be empty\");","typeGuard":null,"tryCatchPattern":"try { Directory.CreateJunction(junctionPath, targetPath); }\ncatch (DirectoryNotEmptyException ex) { logger.LogError(ex, \"Junction target not empty: {Path}\", junctionPath); }","preventionTips":["Always call Directory.IsEmpty on the junction path before creating","Delete or empty pre-existing folders in setup scripts","Make junction creation idempotent: check existence and emptiness first","Clean up test fixtures between runs"],"tags":["windows","filesystem","junction","directory-not-empty"],"backgroundTag":"directory-not-empty","analyzedSha":"53fb989abc2219826385683a6fee826bd6cd38d6","analyzedAt":"2026-09-02T04:25:09.259Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T11:17:12.671Z"}