title: -Python- Move All PDF Files in Folder to NewDirectory Based on Matching Names, Using Glob or Shutil
body: <p>I'm trying to write code that will move hundreds of PDF files from a :/Scans folder into another directory based on the matching each client's name. I'm not sure if I should be using Glob, or Shutil, or a combination of both. My working theory is that Glob should work for such a program, as the glob module "finds all the pathnames matching a specified pattern," and then use Shutil to physically move the files?</p>
  
  <p>Here is a breakdown of my file folders to give you a better idea of what I'm trying to do.</p>
  
  <p>Within :/Scans folder I have thousands of PDF files, manually renamed based on client and content, such that the folder looks like this:</p>
  
  <p>lastName, firstName - [contentVariable]
  (repeat the above 100,000x)</p>
  
  <p>Within the :/J drive of my computer I have a folder named 'Clients' with sub-folders for each and every client, similar to the pattern above, named as 'lastName, firstName'</p>
  
  <p>I'm looking to have the program go through the :/Scans folder and move each PDF to the matching client folder based on 'lastName, firstName'</p>
  
  <p>I've been able to write a simple program to move files between folders, but not one that will do the aforesaid name matching.</p>
  
  <p><code>shutil.copy('C:/Users/Kenny/Documents/Scan_Drive','C:/Users/Kenny/Documents/Clients')</code></p>
  
  <p>^ Moving a file from one folder to another.. quite easily done.</p>
  
  <p>Is there a way to modify the above code to apply to a regex (below)? </p>
  
  <p><code>shutil.copy('C:/Users/Kenny/Documents/Scan_Drive/\w*', 'C:/Users/Kenny/Documents/Clients/\w*')</code></p>
  
  <p>EDIT: @Byberi - Something as such?</p>
  
  <p><code>path = "C:/Users/Kenny/Documents/Scans"
      dirs = os.path.isfile()</code></p>
  
  <p>This would print all the files and directories</p>
  
  <p><code>for file in dirs:
         print file</code></p>
  
  <p><code>dest_dir = "C:/Users/Kenny/Documents/Clients"
  for file in glob.glob(r'C:/*'):
      print(file)
      shutil.copy(file, dest_dir)</code></p>
  
  <p>I've consulted the following threads already, but I cannot seem to find how to match and move the files. </p>
  
  <p><a href="https://stackoverflow.com/questions/37297527/select-files-in-directory-and-move-them-based-on-text-list-of-filenames">Select files in directory and move them based on text list of filenames</a></p>
  
  <p><a href="https://docs.python.org/3/library/glob.html" rel="nofollow noreferrer">https://docs.python.org/3/library/glob.html</a></p>
  
  <p><a href="https://stackoverflow.com/questions/43381446/python-move-files-from-directories-that-match-given-criteria-to-new-directory">Python move files from directories that match given criteria to new directory</a></p>
  
  <p><a href="https://www.guru99.com/python-copy-file.html" rel="nofollow noreferrer">https://www.guru99.com/python-copy-file.html</a></p>
  
  <p><a href="https://docs.python.org/3/howto/regex.html" rel="nofollow noreferrer">https://docs.python.org/3/howto/regex.html</a></p>
  
  <p><a href="https://code.tutsplus.com/tutorials/file-and-directory-operations-using-python--cms-25817" rel="nofollow noreferrer">https://code.tutsplus.com/tutorials/file-and-directory-operations-using-python--cms-25817</a></p>