带进度条复制文件(调用ShellAPI)

Cplusplus
回复
头像
523066680
Administrator
Administrator
帖子: 573
注册时间: 2016年07月19日 12:14
联系:

带进度条复制文件(调用ShellAPI)

帖子 523066680 »

编译方法:
MinGW: g++ main.cpp -municode
MSVC : cl main.cpp /link shell32.lib

使用方法参见代码开头注释部分,支持从不同的目录复制文件到指定目录,支持Unicode字符
(在终端粘贴Unicode字符虽然看到是空白的,但是并没有丢失)

动机:有段时间帮别人复制电影,移动硬盘里分了多个子目录,从每个目录中挑选并分别复制到目标目录。
在 windows下不能对不同目录下的文件 ctrl+c 然后再一次性粘贴到某个目录。所以写了这个工具,可以在调用时把多个文件
(可以是不同目录下的)拖到参数里,最后把目标目录拖到最后一个参数,按Enter后开始复制。

图例:
Snap3.png
(11.31 KiB) 已下载 934 次
/*
Code by: 523066680
Date : 2015-11
Compile:
MinGW: g++ main.cpp -municode
MSVC : cl main.cpp /link shell32.lib
Usage:
Example:
main.exe D:\FA\* F:\Spare
main.exe D:\FA\* D:\FB\name.iso F:\Spare
main.exe "D:\FA\Pro?e.rar" "F:\Spare"
Note:
Last path is the destination

Support: Copy from different folder, Unicode String
*/

#include <cstdio>
#include <cstdlib>
#include <io.h>
#include <windows.h>


void ShellCopy( wchar_t *SRC, wchar_t *DST );

void connect_wcs_array (
wchar_t *buff,
wchar_t *array[],
int begin,
int end
);

int wmain(int argc, wchar_t *argv[] )
{
if (argc < 3)
{
printf("Arguments not enough\n");
return -1;
}

//argv[argc-1] - Destination

int length = 0;
for (int i = 1; i <= argc-2; i++)
{
length += wcslen( argv[i] ) + 1;
}
length++; // 0x00 0x00

wchar_t *fwaits = (wchar_t *) malloc(
length * sizeof(wchar_t) );

connect_wcs_array( fwaits, argv, 1, argc-2 );
ShellCopy( fwaits, argv[argc-1] );
free(fwaits);
return 0;
}

void ShellCopy( wchar_t *SRC, wchar_t *DST )
{
int sherr;
SHFILEOPSTRUCTW op;
ZeroMemory( &op, sizeof(op) );
op.hwnd = NULL;
op.wFunc = FO_COPY;
op.pFrom = SRC;
op.pTo = DST;
op.fFlags= 0;
sherr = SHFileOperationW( &op );
printf("%x", sherr);
}

void connect_wcs_array (
wchar_t *buff,
wchar_t *array[],
int begin,
int end
)
{
int i;
wchar_t *pt = buff;

for ( i = begin; i <= end; i++ )
{
wcsncpy( pt, array[i], wcslen(array[i]) + 1 );
pt += wcslen(array[i]) + 1;
}
*(pt) = L'\0'; // append 0x00
}
copyw.rar
(32.76 KiB) 已下载 64 次
参考:在拷贝、删除文件时显示飞行的文件夹动画
头像
523066680
Administrator
Administrator
帖子: 573
注册时间: 2016年07月19日 12:14
联系:

Re: 带进度条复制文件(调用ShellAPI)

帖子 523066680 »

> windows.h > _mingw.h > _cygwin.h > stddef.h > crtdefs.h
> _mingw_mac.h
> _mingw_secapi.h
> sdks/_mingw_ddk.h
> sdks/_mingw_directx.h
> vadefs.h
> cderr.h
> commdlg.h
> dde.h
> ddeml.h
> excpt.h
> imm.h
> lzexpand.h
> mcx.h
> mmsystem.h
> nb30.h
> ole.h
> rpc.h > rpcasync.h
> rpcdce.h > rpcdcep.h
> rpcnsi.h
> rpcnterr.h
> sdkddkver.h
> shellapi.h
> stdarg.h > _mingw_stdarg.h
> stralign.h > sec_api/stralign_s.h
> virtdisk.h
> winbase.h > apisetcconv.h
> bemapiset.h
> debugapi.h
> errhandlingapi.h
> fibersapi.h
> fileapi.h
> handleapi.h
> heapapi.h
> interlockedapi.h
> ioapiset.h
> jobapi.h
> libloaderapi.h
> memoryapi.h
> minwinbase.h
> namedpipeapi.h
> namespaceapi.h
> processenv.h
> processthreadsapi.h
> processtopologyapi.h
> profileapi.h
> realtimeapiset.h
> securityappcontainer.h
> securitybaseapi.h
> synchapi.h
> sysinfoapi.h
> systemtopologyapi.h
> threadpoolapiset.h
> threadpoollegacyapiset.h
> timezoneapi.h
> utilapiset.h
> winerror.h > fltwinerror.h
> wow64apiset.h
> wincon.h
> wincrypt.h > bcrypt.h
> ncrypt.h
> windef.h > minwindef.h > specstrings.h > sal.h
> winapifamily.h
> winnt.h > apiset.h
> basetsd.h
> ctype.h
> guiddef.h > string.h
> ktmtypes.h
> poppack.h
> psdk_inc/intrin-impl.h > intrin.h
> pshpack2.h
> pshpack4.h
> pshpack8.h
> winefs.h
> wingdi.h > pshpack1.h
> winnetwk.h > wnnc.h
> winnls.h > datetimeapi.h
> stringapiset.h
> winperf.h
> winreg.h > reason.h
> winresrc.h > commctrl.rh
> dde.rh
> dlgs.h
> winnt.rh
> winuser.rh
> winver.h > _mingw_unicode.h
> winscard.h > SCardErr.h
> winioctl.h
> winsmcrd.h
> wtypes.h > ole2.h > objbase.h > combaseapi.h
> objidl.h
> propidl.h
> urlmon.h
> oleauto.h > oaidl.h
> oleidl.h
> rpcndr.h > rpcnsip.h
> rpcsal.h
> wtypesbase.h
> winsock.h > _bsd_types.h
> _timeval.h
> inaddr.h
> psdk_inc/_fd_types.h
> psdk_inc/_ip_mreq1.h
> psdk_inc/_ip_types.h
> psdk_inc/_socket_types.h
> psdk_inc/_wsa_errnos.h
> psdk_inc/_wsadata.h
> psdk_inc/_xmitfile.h
> winspool.h > prsht.h
> winsvc.h
> winuser.h > tvout.h
回复

在线用户

正浏览此版面之用户: 没有注册用户 和 1 访客