windows下重命名一个带有前缀"."dot字符的名字的错误问题

本文介绍如何在Windows环境下手动创建或重命名带有点前缀的文件,如.project,这对于使用Eclipse等IDE导入特定配置文件非常重要。文中提供了通过命令提示符和记事本两种方法实现。

如果用正常的右键重命名那么肯定会报错的,比如:

有一个名为project的文件,我想把它命名为.project,加了个前缀dot。然后window就报错了,弹出个对话框让“你必须输入一个文件名”。它可能默认为这是文件后缀了吧。所以是“非法”的。但是Eclipse的工程文件就必须要这样命名,然后在用Eclipse导入工程的时候才能识别。也就是必须有这个前缀dot。这样的命名方法行不通可以用CMD窗口用命令行修改,如下:

 

move  project .project

 

与Linux下的文件重命名方式差不多。

 

或者还可以用记事本打开Eclipse的工程文件,因为工程文件实质上时xml的文档格式,然后另存为一个带有前缀dot的文件就行了。

 

 

references:

http://stackoverflow.com/questions/5004633/how-to-manually-create-a-file-with-a-dot-prefix-in-windows-for-example-htacce

转载于:https://www.cnblogs.com/foohack/p/4200654.html

#include <iostream> #include <string> #include <windows.h> #include <vector> #include <iomanip> #include <sstream> using namespace std; // 转换FILETIME到本地时间字符串 string FileTimeToString(const FILETIME& ft) { SYSTEMTIME st; FileTimeToSystemTime(&ft, &st); char buffer[26]; sprintf_s(buffer, "%04d-%02d-%02d %02d:%02d:%02d", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); return string(buffer); } // 获取文件创建时间 FILETIME GetFileCreationTime(const string& filePath) { HANDLE hFile = CreateFileA(filePath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); FILETIME ftCreation = {0}; if (hFile != INVALID_HANDLE_VALUE) { FILETIME ftAccess, ftWrite; if (GetFileTime(hFile, &ftCreation, &ftAccess, &ftWrite)) { // 时间获取成功 } CloseHandle(hFile); } return ftCreation; } // 比较两个FILETIME(time1 >= time2 返回true) bool CompareFileTime(const FILETIME& time1, const FILETIME& time2) { return (::CompareFileTime(&time1, &time2) >= 0); } // 搜索符合条件的文件 vector<string> SearchFiles(const string& directory, const string& fileType, const FILETIME& startTime, const FILETIME& endTime) { vector<string> result; string searchPath = directory + "\\*"; WIN32_FIND_DATAA findData; HANDLE hFind = FindFirstFileA(searchPath.c_str(), &findData); if (hFind != INVALID_HANDLE_VALUE) { do { // 跳过当前目录和上级目录 if (strcmp(findData.cFileName, ".") == 0 || strcmp(findData.cFileName, "..") == 0) { continue; } // 构建完整路径 string fullPath = directory + "\\" + findData.cFileName; // 处理目录递归搜索 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { vector<string> subFiles = SearchFiles(fullPath, fileType, startTime, endTime); result.insert(result.end(), subFiles.begin(), subFiles.end()); continue; } // 检查文件类型 string fileName = findData.cFileName; size_t dotPos = fileName.find_last_of('.'); string ext = (dotPos != string::npos) ? fileName.substr(dotPos + 1) : ""; if (!fileType.empty() && _stricmp(ext.c_str(), fileType.c_str()) != 0) { continue; } // 检查创建时间 FILETIME creationTime = findData.ftCreationTime; if ((CompareFileTime(creationTime, startTime) || (startTime.dwHighDateTime == 0 && startTime.dwLowDateTime == 0)) && (CompareFileTime(endTime, creationTime) || (endTime.dwHighDateTime == 0 && endTime.dwLowDateTime == 0))) { result.push_back(fullPath); } } while (FindNextFileA(hFind, &findData) != 0); FindClose(hFind); } return result; } // 重命名文件 bool RenameFile(const string& oldPath, const string& newPath) { if (MoveFileA(oldPath.c_str(), newPath.c_str())) { cout << "成功: " << oldPath << " -> " << newPath << endl; return true; } else { DWORD error = GetLastError(); cout << "错误: 重命名失败 - " << oldPath << " 错误码: " << error << endl; return false; } } // 转换用户输入的日期为FILETIME FILETIME StringToFileTime(const string& dateStr) { SYSTEMTIME st = {0}; if (dateStr.length() >= 10) { sscanf_s(dateStr.c_str(), "%4d-%2d-%2d", &st.wYear, &st.wMonth, &st.wDay); FILETIME ft; SystemTimeToFileTime(&st, &ft); return ft; } // 返回0表示不限制时间 FILETIME zero = {0}; return zero; } int main() { cout << "===== 文件搜索与重命名工具 (VS2008兼容版) =====" << endl; // 设置搜索目录 string directory; cout << "请输入搜索目录 (例如: C:\\MyFiles): "; getline(cin, directory); // 获取文件类型筛选条件 string fileType; cout << "请输入文件类型 (如txt, jpg, 留空则不限制): "; getline(cin, fileType); // 获取时间范围 string startDate, endDate; cout << "请输入开始日期 (格式: YYYY-MM-DD, 留空则不限制): "; getline(cin, startDate); cout << "请输入结束日期 (格式: YYYY-MM-DD, 留空则不限制): "; getline(cin, endDate); FILETIME startTime = StringToFileTime(startDate); FILETIME endTime = StringToFileTime(endDate); // 搜索文件 cout << "\n正在搜索文件..." << endl; vector<string> files = SearchFiles(directory, fileType, startTime, endTime); if (files.empty()) { cout << "未找到符合条件的文件!" << endl; system("pause"); return 0; } // 显示搜索结果 cout << "\n找到 " << files.size() << " 个文件:" << endl; for (size_t i = 0; i < files.size(); i++) { size_t pos = files[i].find_last_of("\\/"); string fileName = (pos != string::npos) ? files[i].substr(pos + 1) : files[i]; cout << "[" << setw(3) << i+1 << "] " << fileName << endl; } // 选择重命名模式 int renameMode; cout << "\n请选择重命名模式:" << endl; cout << "1. 添加前缀" << endl; cout << "2. 添加后缀" << endl; cout << "3. 替换文本" << endl; cout << "4. 序列编号" << endl; cout << "请输入选项 (1-4): "; cin >> renameMode; cin.ignore(); string prefix, suffix, oldText, newText; int startNum = 1; string format; switch (renameMode) { case 1: cout << "请输入要添加的前缀: "; getline(cin, prefix); break; case 2: cout << "请输入要添加的后缀: "; getline(cin, suffix); break; case 3: cout << "请输入要替换的文本: "; getline(cin, oldText); cout << "请输入替换后的文本: "; getline(cin, newText); break; case 4: cout << "请输入起始编号: "; cin >> startNum; cin.ignore(); cout << "请输入格式字符串 (例如: file_%%03d): "; getline(cin, format); break; default: cout << "无效的选项!" << endl; system("pause"); return 1; } // 执行重命名 int successCount = 0; cout << "\n==== 开始重命名 ====" << endl; for (size_t i = 0; i < files.size(); i++) { string filePath = files[i]; size_t lastSlash = filePath.find_last_of("\\/"); size_t lastDot = filePath.find_last_of("."); string path = (lastSlash != string::npos) ? filePath.substr(0, lastSlash + 1) : ""; string fileName = (lastSlash != string::npos) ? filePath.substr(lastSlash + 1) : filePath; string name = (lastDot != string::npos && lastDot > lastSlash) ? fileName.substr(0, lastDot - lastSlash - 1) : fileName; string ext = (lastDot != string::npos && lastDot > lastSlash) ? fileName.substr(lastDot) : ""; string newName; if (renameMode == 1) { newName = prefix + name + ext; } else if (renameMode == 2) { newName = name + suffix + ext; } else if (renameMode == 3) { // 替换文本 size_t pos = 0; while ((pos = name.find(oldText, pos)) != string::npos) { name.replace(pos, oldText.length(), newText); pos += newText.length(); } newName = name + ext; } else if (renameMode == 4) { // 序列编号 char numBuffer[20]; sprintf_s(numBuffer, format.c_str(), startNum++); newName = numBuffer + ext; } string newFilePath = path + newName; if (RenameFile(filePath, newFilePath)) { successCount++; } } cout << "\n==== 重命名完成 ====" << endl; cout << "成功重命名 " << successCount << " 个文件" << endl; cout << "失败 " << files.size() - successCount << " 个" << endl; system("pause"); return 0; }运行后出现错误:TEST_file_Rename.exe 中的 0x7c812aeb 处未处理的异常: Microsoft C++ 异常: 内存位置 0x0012f740 处的 std::out_of_range。基于vs2008向我提供优化后的代码
06-24
#include <iostream> #include <string> #include <windows.h> #include <vector> #include <iomanip> #include <sstream> #include <ctime> #include <io.h> using namespace std; // 转换FILETIME到本地时间字符串 string FileTimeToString(const FILETIME& ft) { SYSTEMTIME st; FileTimeToSystemTime(&ft, &st); char buffer[26]; sprintf_s(buffer, "%04d-%02d-%02d %02d:%02d:%02d", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); return string(buffer); } // 获取文件创建时间 FILETIME GetFileCreationTime(const string& filePath) { HANDLE hFile = CreateFileA(filePath.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); FILETIME ftCreation = {0}; if (hFile != INVALID_HANDLE_VALUE) { FILETIME ftAccess, ftWrite; if (GetFileTime(hFile, &ftCreation, &ftAccess, &ftWrite)) { // 转换为本地时间(可选) // SYSTEMTIME stLocal; // FileTimeToSystemTime(&ftCreation, &stLocal); } CloseHandle(hFile); } return ftCreation; } // 比较两个FILETIME(time1 >= time2 返回true) bool CompareFileTime(const FILETIME& time1, const FILETIME& time2) { if (time1.dwHighDateTime > time2.dwHighDateTime) return true; if (time1.dwHighDateTime < time2.dwHighDateTime) return false; return time1.dwLowDateTime >= time2.dwLowDateTime; } // 搜索符合条件的文件 vector<string> SearchFiles(const string& directory, const string& fileType, const FILETIME& startTime, const FILETIME& endTime) { vector<string> result; string searchPath = directory + "\\*.*"; WIN32_FIND_DATAA findData; HANDLE hFind = FindFirstFileA(searchPath.c_str(), &findData); if (hFind != INVALID_HANDLE_VALUE) { do { // 跳过当前目录和上级目录 if (strcmp(findData.cFileName, ".") == 0 || strcmp(findData.cFileName, "..") == 0) { continue; } // 跳过目录 if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { continue; } // 检查文件类型 string fileName = findData.cFileName; size_t dotPos = fileName.find_last_of('.'); string ext = (dotPos != string::npos) ? fileName.substr(dotPos + 1) : ""; if (!fileType.empty() && _stricmp(ext.c_str(), fileType.c_str()) != 0) { continue; } // 检查创建时间 FILETIME creationTime = GetFileCreationTime(directory + "\\" + fileName); if ((CompareFileTime(creationTime, startTime) || startTime.dwHighDateTime == 0) && (CompareFileTime(endTime, creationTime) || endTime.dwHighDateTime == 0)) { result.push_back(directory + "\\" + fileName); } } while (FindNextFileA(hFind, &findData) != 0); FindClose(hFind); } return result; } // 重命名文件 bool RenameFile(const string& oldPath, const string& newPath) { cout << "尝试重命名: " << oldPath << " -> " << newPath << endl; if (_access(oldPath.c_str(), 0) != 0) { cout << "错误: 源文件不存在 - " << oldPath << endl; return false; } if (_access(newPath.c_str(), 0) == 0) { cout << "错误: 目标文件已存在 - " << newPath << endl; return false; } if (MoveFileA(oldPath.c_str(), newPath.c_str())) { cout << "成功: " << newPath << endl; return true; } else { DWORD error = GetLastError(); cout << "错误: 重命名失败 - 错误码: " << error << endl; return false; } } // 转换用户输入的日期为FILETIME FILETIME StringToFileTime(const string& dateStr) { // 格式示例: "2023-01-01 12:00:00" SYSTEMTIME st = {0}; if (dateStr.length() >= 19) { sscanf_s(dateStr.c_str(), "%4d-%2d-%2d %2d:%2d:%2d", &st.wYear, &st.wMonth, &st.wDay, &st.wHour, &st.wMinute, &st.wSecond); FILETIME ft; SystemTimeToFileTime(&st, &ft); return ft; } // 返回0表示不限制时间 return {0}; } int main() { cout << "===== 文件搜索与重命名工具 =====" << endl; // 设置搜索目录(默认为E盘) string directory = "E:\\"; cout << "搜索目录: " << directory << endl; // 获取文件类型筛选条件 string fileType; cout << "请输入文件类型(如txt、jpg,留空则不限制): "; getline(cin, fileType); // 获取时间范围 string startDate, endDate; cout << "请输入开始日期 (格式: YYYY-MM-DD HH:MM:SS,留空则不限制): "; getline(cin, startDate); cout << "请输入结束日期 (格式: YYYY-MM-DD HH:MM:SS,留空则不限制): "; getline(cin, endDate); FILETIME startTime = StringToFileTime(startDate); FILETIME endTime = StringToFileTime(endDate); // 搜索文件 cout << "\n正在搜索文件..." << endl; vector<string> files = SearchFiles(directory, fileType, startTime, endTime); if (files.empty()) { cout << "未找到符合条件的文件!" << endl; system("pause"); return 0; } // 显示搜索结果 cout << "\n找到 " << files.size() << " 个文件:" << endl; for (size_t i = 0; i < files.size(); i++) { FILETIME ft = GetFileCreationTime(files[i]); cout << "[" << setw(2) << i+1 << "] " << files[i] << " (创建于: " << FileTimeToString(ft) << ")" << endl; } // 选择重命名模式 int renameMode; cout << "\n请选择重命名模式:" << endl; cout << "1. 添加前缀" << endl; cout << "2. 添加后缀" << endl; cout << "3. 替换文本" << endl; cout << "4. 序列编号" << endl; cout << "请输入选项 (1-4): "; cin >> renameMode; cin.ignore(); // 清除输入缓冲区 string prefix, suffix, oldText, newText; int startNum = 1; string format; switch (renameMode) { case 1: cout << "请输入要添加的前缀: "; getline(cin, prefix); break; case 2: cout << "请输入要添加的后缀: "; getline(cin, suffix); break; case 3: // 关键修改:将代码块改为单行语句,避免VS2008的语法解析错误 cout << "请输入要替换的文本: "; getline(cin, oldText); cout << "请输入替换后的文本: "; getline(cin, newText); break; case 4: cout << "请输入起始编号: "; cin >> startNum; cin.ignore(); cout << "请输入格式字符串 (例如: img_%%03d): "; getline(cin, format); break; default: cout << "无效的选项!" << endl; system("pause"); return 1; } // 执行重命名 int successCount = 0; int failCount = 0; cout << "\n==== 开始重命名 ====" << endl; for (size_t i = 0; i < files.size(); i++) { const string& filePath = files[i]; size_t lastSlash = filePath.find_last_of("\\/"); size_t lastDot = filePath.find_last_of("."); string path = (lastSlash != string::npos) ? filePath.substr(0, lastSlash + 1) : ""; string fileName = (lastSlash != string::npos) ? filePath.substr(lastSlash + 1) : filePath; string name = (lastDot != string::npos && lastDot > lastSlash) ? fileName.substr(0, lastDot - lastSlash - 1) : fileName; string ext = (lastDot != string::npos && lastDot > lastSlash) ? fileName.substr(lastDot) : ""; string newName = ""; // 使用if-else结构替代复杂的switch-case,提高兼容性 if (renameMode == 1) { newName = prefix + name + ext; } else if (renameMode == 2) { newName = name + suffix + ext; } else if (renameMode == 3) { // 关键修改:将代码块改为单行语句,避免VS2008的语法解析错误 string newFileName = name; size_t pos = 0; while ((pos = newFileName.find(oldText, pos)) != string::npos) { newFileName.replace(pos, oldText.length(), newText); pos += newText.length(); } newName = newFileName + ext; } else if (renameMode == 4) { char buffer[1024]; string fmt = format; size_t pos = 0; while ((pos = fmt.find("%%", pos)) != string::npos) { fmt.replace(pos, 2, "%"); pos += 1; } sprintf_s(buffer, fmt.c_str(), startNum++); newName = buffer + ext; } string newFilePath = path + newName; if (RenameFile(filePath, newFilePath)) { successCount++; } else { failCount++; } } cout << "\n==== 重命名完成 ====" << endl; cout << "成功: " << successCount << " 个" << endl; cout << "失败: " << failCount << " 个" << endl; system("pause"); return 0; }
06-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值