matlab版本:
function [names,class_num] = GetFiles(sourceDir)
files = dir(sourceDir);
size0 = size(files);
length = size0(1);
names = files(3:length); % names(i).name即可获取到对应的文件夹名称
class_num = size(names);
end
clear all;
clc
%[data,name_excel]= xlsread('C:\Users\Administrator\Desktop\示例\test1.xls'); % name_excel(i)即为所需要的文件夹名称
name_excel = importdata('C:\Users\Administrator\Desktop\示例\test3.txt');
sourceDir = 'C:\Users\Administrator\Desktop\示例';
targetDir = 'C:\Users\Administrator\Desktop\target';
[names,class_num] = GetFiles(sourceDir)
%c = name_excel{1}
%b = names(3).name
for i =1: size(name_excel)
for j =1:size(names)
if isequal(name_excel{i},names(j).name)
copyfile([sourceDir,'\',names(j).name],[targetDir,'\',names(j).name],'f');
fprintf('The matching data is the %d file\n',i)
end
end
end
Python 版本:
# coding=utf-8
import xlrd
import shutil
import os
def find_col(path):
data = xlrd.open_workbook(path)
# 查看工作表
data.sheet_names()
print("sheets:" + str(data.sheet_names()))
# 打印data.sheet_names()可发现,返回的值为一个列表,通过对列表索引操作获得工作表1
table = data.sheet_by_index(0)
# 获取第一列的所有元素
aim_col = table.col_values(0)
print("第一列值:\n" + str(aim_col))
return aim_col
def folder_name(sourceDir):
dbtype_list = os.listdir(sourceDir)
for dbtype in dbtype_list[::]:
if os.path.isfile(os.path.join(sourceDir,dbtype)):
dbtype_list.remove(dbtype)
print(dbtype_list)
return dbtype_list
import shutil
import os
if __name__ == "__main__":
path_excel = r'C:\Users\Administrator\Desktop\示例\test.xlsx'#excel表格的文件路径
#sql_dir_war = r'C:\Users\Administrator\Desktop\示例'#目标文件所在的文件路径
sourceDir = 'C:/Users/Administrator/Desktop/示例'#目标文件所在的文件路径
targetDir = 'C:/Users/Administrator/Desktop/target'
aim_col = find_col(path_excel)
dbtype_list = folder_name(sql_dir_war)
print("\n")
for each_name in aim_col:
for each_folder in dbtype_list:
if each_name == each_folder:
shutil.copytree(sourceDir+"/"+each_name,targetDir+"/"+each_name)
print("查找完成")