16,473
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <shellapi.h>
#include <direct.h>
#include <string>
#include "Shlwapi.h"
int CopyDirectory(LPCTSTR PathFrom,LPCTSTR PathTo)
{
SHFILEOPSTRUCT Op;
ZeroMemory(&Op, sizeof(Op));
TCHAR ToBuf[MAX_PATH];
TCHAR FromBuf[MAX_PATH];
ZeroMemory(ToBuf, sizeof(ToBuf));
ZeroMemory(FromBuf, sizeof(FromBuf));
lstrcpy(ToBuf, PathTo);
lstrcpy(FromBuf, PathFrom);
Op.hwnd = NULL;
Op.pFrom = FromBuf;
Op.pTo = ToBuf;
Op.fFlags =FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI;
Op.fAnyOperationsAborted = FALSE;
Op.hNameMappings = NULL;
Op.lpszProgressTitle = NULL;
Op.wFunc = FO_COPY;
int Re = SHFileOperation(&Op);
return Re;
}
TCHAR* TestGetExePath(void)
{
//
const int nBufSize = 512;
TCHAR chBuf[nBufSize];
ZeroMemory(chBuf,nBufSize);
//获取当前执行文件的路径。
if (GetModuleFileName(NULL,chBuf,nBufSize))
{
//获取文件路径。
TCHAR* lpStrPath = chBuf;
PathRemoveFileSpec(lpStrPath);
return lpStrPath;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
/* if ( argc!= 3 )
{
printf("Usage %s file1 file2\n", argv[0]);
return 1;
}
if (!CopyFile(argv[1],argv[2],false))
{
printf("Copy file error : %x\n", GetLastError());
return 2;
}*/
//path="'"+path+"'";
//dir.Format(_T("%s DFDFD"),path);
TCHAR buffer[MAX_PATH];
GetShortPathName(argv[1], buffer, sizeof(buffer));
CopyDirectory(buffer,TestGetExePath());
// printf( "The current directory is:%s", path);
return 0;
}
TCHAR buffer[MAX_PATH];
GetShortPathName(argv[1], buffer, sizeof(buffer));
把路径变成了短文件名,现在主要的问题是,如果目录下有一个长文件名的文件(比如“sdfkljsdklafjklasdfklfsdfsdfsdf.exe")就无法复制了,关键还是只复制函数上?依然等待。。。TCHAR* TestGetExePath(void)
{
const int nBufSize = 512;
TCHAR* lpStrPath = new TCHAR[nBufSize];
GetModuleFileName(NULL, lpStrPath, nBufSize);
PathRemoveFileSpec(lpStrPath);
return lpStrPath;
}