//当文件存在时,加(1),(2)... added by Qian 2012/7/31
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
std::string szFileName = strFullPath.substr(strFullPath.rfind("\\") + 1); //最原始的无路径文件名
int i = 1;//当前重复次数
while( (hFind = ::FindFirstFile((LPCSTR)strFullPath.c_str(), &FindFileData)) != INVALID_HANDLE_VALUE )
{
char buf[4];
sprintf(buf,"(%d)",i);
std::string strNewFileName = szFileName;//调整后的文件名
strNewFileName.insert(strNewFileName.find_last_of("."),buf);
//删除原路径中的文件名
strFullPath.erase(strFullPath.find_last_of("\\"), strFullPath.size());
//将调整后的文件名加入路径
strFullPath.append("\\").append(strNewFileName);
FindClose(hFind);
i++;
}
程序调试
// test.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <fstream>
#include <string>
#include <io.h>
#include <Windows.h>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
std::string str = "C:\\Users\\Administrator\\AppData\\Local\\JtangIM\\用户名\\FileRecv\ //当文件存在时,加(1) added by Qian 2012/7/31
FILE *stream;
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
while( (hFind = FindFirstFile((LPCWSTR)str.c_str(), &FindFileData)) != INVALID_HANDLE_VALUE )
{
int i = 1;
char buf[4];
sprintf(buf,"(%d)",i);
str.insert(str.find_last_of("."),buf);
FindClose(hFind);
i++;
}
// FILE *stream;
//
// WIN32_FIND_DATA FindFileData;
// HANDLE hFind;
//
// while( (hFind = FindFirstFile((LPCWSTR)str.c_str(), &FindFileData)) != INVALID_HANDLE_VALUE )
// {
// int last = str.find_last_of( "\\" );
// str.erase(last, str.size());
// int i = 1;
// char buf[4];
// sprintf(buf,"(%d)",i);
// dd.insert(t,buf);
// str.append(dd);
// FindClose(hFind);
// }
// while ( (stream = fopen( "data", "r" )) == NULL )
// while( !::CreateDirectory((LPCWSTR)str.c_str(), NULL) )
// {
// last = str.find_last_of( "\\" );
// str.erase(last, str.size());
// int i = 1;
// char buf[4];
// sprintf(buf,"(%d)",i);
// dd.insert(t,buf);
// str.append(dd);
// }
// while ( i != -1 )
// int last = str.find_last_of( "\\" );
return 0;
}