Home > Back-end >  copy files based on matching symbol
copy files based on matching symbol

Time:09-16

i have a directory that contains thousand of files.However i have a list that contain file names of the same directory. I want to copy the files which contain star mark * at the end from the list.

List is

2016-05-23-0145-30S.INMME_012*
2016-07-12-0546-26S.INMME_006
2016-07-17-2033-16S.INMME_003
2016-07-19-1307-57S.INMME_003
2016-08-17-1649-12S.INMME_006*
2016-09-03-1151-03S.INMME_003
2016-10-21-1240-02S.INMME_006*
2016-10-21-1310-38S.INMME_006
2016-10-23-0016-39S.INMME_006
2016-10-23-0859-50S.INMME_006

So i want to copy 2016-05-23-0145-30S.INMME_012* 2016-08-17-1649-12S.INMME_006* 2016-10-21-1240-02S.INMME_006* to separate directory.

CodePudding user response:

import os
import shutil
def copy_file_to_another_location(inputpath, outputpath, filename):
   if not os.path.exists(outputpath):
      os.makedirs(outputpath)
   shutil.copy(str(os.path.join(inputpath, filename)).replace("\n", "").replace("*", ""), str(os.path.join(outputpath, filename)).replace("\n", "").replace("*", ""))
   print("Copying: "   str(os.path.join(inputpath, filename)).replace("\n", ""))

inputpath = r'C:\\Users\\me\\Desktop\\tada\\in'
outputpath = r'C:\\Users\\me\\Desktop\\tada\\out'
file = open("asd.txt", "r", encoding="utf-8")
files_list = file.readlines()
for file in files_list:
   if "*" in str(file):
      copy_file_to_another_location(inputpath, outputpath, file)

NOTE: This script will make sure to keep the folder structure from input location to output location (ex: inputlocation/some_folder/2016-10-23.... will be copied to outputlocation/some_folder/2016-10-23...)

CodePudding user response:

This bash script will create a directory and copy the files to it

#!/usr/bin/env bash

new_dir="temp"
mkdir $new_dir

for l in list; do
    dup=$(sed -n '/*$/p' $l)
    cp $dup $new_dir/
done
  • Related