[工具]C++一键改名
学习DirectX的2D游戏制作的时候,要切割小图并合并成TileMap。没找到好的切割工具,切割成小图只好用Unity,然后用TexturePacker合并。因为代码中是用索引来获取Json结构数据,因此小图需要重新命名,一个一个修改,太烦也太傻了。写个工具避免重复劳动。
一堆小图:
代码:
#include <iostream>
#include <Windows.h>
#include <ShlObj.h>
char* fileFolder = new char[MAX_PATH];
bool Initialize();
void RenameFile();
int main()
{
if (Initialize())
{
if(IDYES == MessageBox(NULL, fileFolder, "修改路径", MB_YESNO))
RenameFile();
}
system("pause");
return 0;
}
bool Initialize()
{
BROWSEINFO bi;
memset(&bi, 0, sizeof(bi));
bi.ulFlags = BIF_BROWSEFORCOMPUTER | BIF_EDITBOX | BIF_NEWDIALOGSTYLE;
bi.lpszTitle = "请选择目录";
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
bool result = pidl != nullptr;
if (!result)
return false;
SHGetPathFromIDList(pidl, fileFolder);
pidl = nullptr;
if ('\0' == fileFolder[0])
{
MessageBox(nullptr, "目录有误", "错误", MB_OK | MB_ICONERROR);
return false;
}
return true;
}
void RenameFile()
{
char* filePath = new char[MAX_PATH];
strcpy_s(filePath, strlen(fileFolder) + 1, fileFolder);
strcat_s(filePath, strlen(filePath) + strlen("\\*") + 1, "\\*");
WIN32_FIND_DATA findFileData;
HANDLE handle = FindFirstFile(filePath, &findFileData);
if (INVALID_HANDLE_VALUE == handle)
{
MessageBox(nullptr, filePath, "错误", MB_OK | MB_ICONERROR);
return;
}
int index = 0;
char* oldFileNmae = new char[MAX_PATH];
char* newFileName = new char[MAX_PATH];
char indexStr[10];
while (true)
{
if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
std::cout << "DIRECTORY: " << findFileData.cFileName << std::endl;
}
else
{
std::cout << "file: " << findFileData.cFileName << std::endl;
// Old file name
strcpy_s(oldFileNmae, strlen(fileFolder) + 1, fileFolder);
strcat_s(oldFileNmae, strlen(oldFileNmae) + strlen("\\") + 1, "\\");
strcat_s(oldFileNmae, strlen(oldFileNmae) + strlen(findFileData.cFileName) + 1, findFileData.cFileName);
// New file name
strcpy_s(newFileName, strlen(fileFolder) + 1, fileFolder);
strcat_s(newFileName, strlen(newFileName) + strlen("\\") + 1, "\\");
_itoa_s(index, indexStr, 10);
strcat_s(newFileName, strlen(newFileName) + strlen(indexStr) + 1, indexStr);
char* extensionName = strchr(findFileData.cFileName, '.');
strcat_s(newFileName, strlen(newFileName) + strlen(extensionName) + 1, extensionName);
std::cout << "oldFileNmae: " << oldFileNmae << std::endl;
std::cout << "newFileName: " << newFileName << std::endl;
rename(oldFileNmae, newFileName);
++index;
}
if (!FindNextFile(handle, &findFileData))
break;
}
FindClose(handle);
}