C++之调用脚本实现复制当前路径指定目录下文件到另外的地方

使用system()可以完成程序调用脚本,其实system(“pause”);就是这个原理的

#include <iostream>
#include <string>
#include <vector>
#include <windows.h>
#include "MyFile.h"
using namespace std;

int main()
{
	vector<string> vct_strFilePath;
	MyFile myFile;
	//获取当前路径
	char ch_temp[120];
	GetCurrentDirectoryA(120, ch_temp);
	string str_CurrentPath = ch_temp;
	//cout << str_CurrentPath;
	//遍历相对路径的目录
	string str_OldPath = str_CurrentPath.substr(0, str_CurrentPath.find("CPulsCode"));
	myFile.GetWorkList(str_OldPath + "Old\\", vct_strFilePath, true);
	

	//使用system接口调用脚本复制文件
	for (size_t i = 0; i < vct_strFilePath.size(); i++)
	{
		cout << vct_strFilePath[i] << endl;
		string str_temp = "copy " + vct_strFilePath[i] + " E:\\new\\";
		system(str_temp.c_str());
	}

	system("pause");
	return 0;
}

//获取当前目录下所有文件
void MyFile::GetWorkList(string strWorkPath, vector<string>& vctWorkList, bool bSeachChild)
{
	WIN32_FIND_DATAA FileData;
	HANDLE FileHandle = FindFirstFileA((strWorkPath + "*.*").c_str(), &FileData);
	if(FileHandle == INVALID_HANDLE_VALUE)
	{
		return ;
	}
	do
	{
		if (strcmp(FileData.cFileName, ".") == 0 || strcmp(FileData.cFileName, "..") == 0)
		{
			continue;
		}
		else if ((FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
		{
			if (bSeachChild)
			{
				GetWorkList(strWorkPath + FileData.cFileName + "\\", vctWorkList, bSeachChild);
			}
		}
		else
		{
			vctWorkList.push_back(strWorkPath + FileData.cFileName);
		}
	} while (FindNextFileA(FileHandle, &FileData) != 0);
	FindClose(FileHandle);
	return;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值