腳本實現拷貝特定的文件

腳本實現拷貝特定的文件

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)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章