title: Create folders and move files comparing part of the name in SharePoint
body: <p>I am trying to automate a process in the company in order to organize the data and prepare it for the next project, but I am having problems as I am pretty new to SharePoint automation.</p>
  <p>I have data coming from the field teams every day on the company's SharePoint. The data is separated by projects, and all file types are going into separate subfolders, but all pdf files, videos, pictures are stored into one subfolder.
  example: <em>SharePoint site/Video/all files inside</em></p>
  <p>The files are named with specific naming convention: &quot;name of the asset&quot;+&quot;_&quot;+(&quot;id&quot; or date depending on the file type).
  examples: N1600027801_2022-05-09_0.pdf
  N1600027801_4777c091-628e-412a-9145-3a3ef6537cb474b984b4-0966-4991-acca-37b707278b00.mp4</p>
  <p>For each area I have the names for the assets into excel file, and my aim is to have separate subfolder into the parent directory for each of them containing the files for that asset (I usually have from 1 to 15 pdfs, 1 to 20 pictures, 1 to 10 videos per asset).</p>
  <p>example: <em>SharePoint site/Video/N1600027801/all video files inside</em></p>
  <p>Because I have sync between the SharePoint and my laptop, I tried to make a script which will create folders for the assets which are already surveyed (based on the documents), but I am getting an error:
  [WinError 5] Access is denied: 'C:/Users/username/SharePointSite/Docu/N1600003602'</p>
  <p>From the search I did, it seems that even I can view the folders and files on the SharePoint location, I can't modify anything with the standard os python libraries.</p>
  <p>Here is the python code that I tried to use:</p>
  <pre><code>import os
  import openpyxl as xl
  from openpyxl import Workbook
  
  
  
  path1=r&quot;C:/Users/username/SharePoint/Misc/Docu/&quot;  #sharepoint location on my laptop
  excel_path=r&quot;C:/Automation/Scripts/move_files_sharepoint/assets.xlsx&quot; #location to the excel file that contains all asset names
  wb = xl.load_workbook(excel_path)
  sheet = wb['assets'] #Sheet that contains the data
  rows = sheet.max_row
  
  #loading the asset names into a python list
  ids=[]
  for row in range(2,rows):
      if sheet.cell(row,2).value!=None:
          area_name=sheet.cell(row,2).value
          pipe_name=sheet.cell(row,1).value
          ids.append(pipe_name)
  
  #loading the existing folder names into a python list
  existing_folders=[]
  
  for file in os.listdir(path1):
      d = os.path.join(path1, file)
      if os.path.isdir(d):
          f = os.path.basename(d)
          existing_folders.append(f)
  
  #loading all files from the parent folder into a python list
  file_list=[]
  for file in os.listdir(path1):
      d = os.path.join(path1, file)
      fname = os.path.basename(d)
      pipe=fname.split('_')[0]
      if pipe not in file_list:
          file_list.append(pipe)
  
  
  #comparing if the files are for the assets, because there could be files which are just from the work orders and I don't need to group into the subfolder
  folders_create=[]
  for file in os.listdir(path1):
      d = os.path.join(path1, file)
      for p in file_list:
          if p not in existing_folders and p not in folders_create:
              if p in ids:
                  os.makedirs(os.path.join(path1, p))
                  folders_create.append(p)
  
  print('In the destination folder were created: ', len(folders_create), ' folders')
  
  
  </code></pre>
  <p>Is this possible with python, or I need to look into something else?
  If yes, could you help me what to look for, or some examples?</p>
  <p>We also have license for Power Automate, but I've never used it, it was ordered by someone that used to work in the company before me.</p>