String (@NotNull VirtualFile dir, @NotNull String fileNameAndExt) { FileTypeManager fileTypeManager = FileTypeManager.getInstance(); IntSet existing = new IntOpenHashSet(); String fileName = getNameWithoutExtension(fileNameAndExt, dir.isCaseSensitive(), fileTypeManager); int fileNameLen = fileName.length(); String fileExt = fileNameAndExt.length() > fileNameLen + 1 ? fileNameAndExt.substring(fileNameLen + 1) : ""; for (VirtualFile child : dir.getChildren()) { if (child.isDirectory()) continue; String childName = getNameWithoutExtension(child, fileTypeManager); if (!StringUtil.startsWithIgnoreCase(childName, fileName)) continue; if (childName.length() == fileNameLen) { existing.add(0); } else if (childName.length() > fileNameLen + 1 && childName.charAt(fileNameLen) == '_') { int val = StringUtil.parseInt(childName.substring(fileNameLen + 1), -1); if (val > -1) existing.add(val); } } int num = 0; while (existing.contains(num)) num++; return PathUtil.makeFileName(num == 0 ? fileName : fileName + "_" + num, fileExt); }