private static String makeValidUniqueFilename(String name, DomainFolder folder) {

		// Trim-off file extension if ours *.g?? (e.g., gzf, gdt, etc.)
		int extIndex = name.lastIndexOf(".g");
		if (extIndex > 1 && (name.length() - extIndex) == 4) {
			name = name.substring(0, extIndex);
		}

		CharBuffer buf = CharBuffer.wrap(name.toCharArray());
		for (int i = 0; i < buf.length(); i++) {
			if (!LocalFileSystem.isValidNameCharacter(buf.get(i))) {
				buf.put(i, '_');
			}
		}

		String baseName = buf.toString();
		name = baseName;
		int count = 0;
		while (folder.getFile(name) != null) {
			++count;
			name = baseName + "." + count;
		}
		return name;
	}
A {@link Plugin} that supplies menu items and tasks to import files into Ghidra.
Ghidra/Features/Base/src/main/java/ghidra/plugin/importer/ImporterPlugin.java