脚本实现拷贝特定的文件
0.前言
任务要求:实现从一个文档中挑选中需要的文件,实现自动化选择。
1.MATLAB代码
%% move file select_end.m
% Summary of example objective
clear;clc;
DST_PATH_t = strcat(pwd,'\A2L_and_HEX');%目的文件目录
if (exist(DST_PATH_t)==0) %如果不存在目录,则新建
mkdir(DST_PATH_t);
end
source_path = strcat(pwd,'\_bin\swb\');
file_A2L = dir(strcat(pwd,'\_bin\swb\*.a2l'));% 找到A2L文件,
SOURCE_File_A2L = [source_path,file_A2L.name];
copyfile(SOURCE_File_A2L,DST_PATH_t);
file_HEX = dir(strcat(pwd,'\_bin\swb\*.hex'));% 找到HEX文件
SOURCE_File_HEX = [source_path,file_HEX.name];
copyfile(SOURCE_File_HEX,DST_PATH_t);
file_map = dir(strcat(pwd,'\_bin\swb\filegroup\interfaces\*.map'));% 找到map文件,因为有多个map文件,所以需要遍历
file_map_number = length(file_map);
for i = 1 : file_map_number
file_map_file = file_map(i).name;
SOURCE_File_map = [source_path,'filegroup\interfaces\',file_map_file];
copyfile(SOURCE_File_map,DST_PATH_t);
end
2.MATLAB实现拷贝出.c和.H文件并分类
%% move file select_end.m
% Summary of example objective
clear;clc;
ASW_path = strcat(pwd,'\ASW');%目的文件目录
if (exist(ASW_path)==0) %如果不存在目录,则新建
mkdir(ASW_path);
end
C_path =[ASW_path,'\C'];
if (exist(C_path)==0) %如果不存在目录,则新建
mkdir(C_path);
end
H_path =[ASW_path,'\H'];
if (exist(H_path)==0) %如果不存在目录,则新建
mkdir(H_path);
end
source_path = strcat(pwd,'\ToBSWCode\A2L\');
file_A2L = dir(strcat(pwd,'\ToBSWCode\A2L\*.a2l'));% 找到A2L文件
SOURCE_File_A2L = [source_path,file_A2L.name];
copyfile(SOURCE_File_A2L,ASW_path);
%
source_path_C = strcat(pwd,'\ToBSWCode\Code\');
file_C = dir(strcat(pwd,'\ToBSWCode\Code\*.c'));% 找到C文件
file_map_number_C = length(file_C);
for i = 1 : file_map_number_C
file_C_file = file_C(i).name;
SOURCE_File_C = [source_path_C,file_C_file];
copyfile(SOURCE_File_C,C_path);
end
%
source_path_H = strcat(pwd,'\ToBSWCode\Code\');
file_H = dir(strcat(pwd,'\ToBSWCode\Code\*.h')); % 找到H文件
file_map_number_H = length(file_H);
for i = 1 : file_map_number_H
file_H_file = file_H(i).name;
SOURCE_File_H = [source_path_H,file_H_file];
copyfile(SOURCE_File_H,H_path);
end
3.用Python实现文件拷贝
#coding:utf-8
import os
import shutil
current_path = os.getcwd();
DST_PATH_t = current_path + '\\VCU_executable_file'
if (os.path.exists(DST_PATH_t)==False):
os.makedirs(DST_PATH_t);
source_path = current_path + '\\_bin\\swb'
for root, dirs, files in os.walk(source_path):
for file in files:
if os.path.splitext(file)[1] == '.a2l':
SOURCE_File_A2L = os.path.join(root, file)
shutil.copy(SOURCE_File_A2L, DST_PATH_t)
if os.path.splitext(file)[1] == '.map':
if file !="relocatable.map":
SOURCE_File_MAP = os.path.join(root, file)
shutil.copy(SOURCE_File_MAP, DST_PATH_t)
names = os.listdir(source_path) #这将返回一个所有文件名的列表
SOURCE_File_HEX = source_path + '\\'+ names[3] # 注意是\\
shutil.copy(SOURCE_File_HEX, DST_PATH_t)