编译方法:
MinGW: g++ main.cpp -municode
MSVC : cl main.cpp /link shell32.lib
使用方法参见代码开头注释部分,支持从不同的目录复制文件到指定目录,支持Unicode字符
(在终端粘贴Unicode字符虽然看到是空白的,但是并没有丢失)
动机:有段时间帮别人复制电影,移动硬盘里分了多个子目录,从每个目录中挑选并分别复制到目标目录。
在 windows下不能对不同目录下的文件 ctrl+c 然后再一次性粘贴到某个目录。所以写了这个工具,可以在调用时把多个文件
(可以是不同目录下的)拖到参数里,最后把目标目录拖到最后一个参数,按Enter后开始复制。
图例:
/*
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
}
参考:
在拷贝、删除文件时显示飞行的文件夹动画