FilePathAutoRename.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. // FilePathAutoRename.cpp
  2. #include "StdAfx.h"
  3. #include "FilePathAutoRename.h"
  4. #include "Common/Defs.h"
  5. #include "Common/IntToString.h"
  6. #include "Windows/FileName.h"
  7. #include "Windows/FileFind.h"
  8. using namespace NWindows;
  9. static bool MakeAutoName(const UString &name,
  10. const UString &extension, int value, UString &path)
  11. {
  12. wchar_t number[32];
  13. ConvertUInt64ToString(value, number);
  14. path = name;
  15. path += number;
  16. path += extension;
  17. return NFile::NFind::DoesFileExist(path);
  18. }
  19. bool AutoRenamePath(UString &fullProcessedPath)
  20. {
  21. UString path;
  22. int dotPos = fullProcessedPath.ReverseFind(L'.');
  23. int slashPos = fullProcessedPath.ReverseFind(L'/');
  24. #ifdef _WIN32
  25. int slash1Pos = fullProcessedPath.ReverseFind(L'\\');
  26. slashPos = MyMax(slashPos, slash1Pos);
  27. #endif
  28. UString name, extension;
  29. if (dotPos > slashPos && dotPos > 0)
  30. {
  31. name = fullProcessedPath.Left(dotPos);
  32. extension = fullProcessedPath.Mid(dotPos);
  33. }
  34. else
  35. name = fullProcessedPath;
  36. name += L'_';
  37. int indexLeft = 1, indexRight = (1 << 30);
  38. while (indexLeft != indexRight)
  39. {
  40. int indexMid = (indexLeft + indexRight) / 2;
  41. if (MakeAutoName(name, extension, indexMid, path))
  42. indexLeft = indexMid + 1;
  43. else
  44. indexRight = indexMid;
  45. }
  46. if (MakeAutoName(name, extension, indexRight, fullProcessedPath))
  47. return false;
  48. return true;
  49. }