Jump to content

[Solved] Copying files from Sharepoint to local directory

Recommended Posts


I'm hoping to create a way of copying and renaming a specific file off of a company Sharepoint site.

For local files I've always used the method of using FileExists( "path") then FileCopy ( "source", "dest" [, flag = 0] )

#include <WinAPIFiles.au3>


Func Copy_File()

local $source = "C:\Users\auser\Documents\test.xls"
Local $dest = "C:\Users\auser\Documents\test"
Local $iFileExists = FileExists($source)

    If $iFileExists Then
        FileCopy($source,$dest);copy file to new location
        MsgBox($MB_SYSTEMMODAL, "", "File was copied")
        MsgBox($MB_SYSTEMMODAL, "", "File doesn't exist")


However with the file location provided by sharepoint, it seems autoIt isn't able to find it. File path provided by sharepoint looks something like this:


I know if I have excel open and paste the link into the excel file name open box, it will open the file just fine. Also I know I can create shortcuts to these links, and when I click on them it will open the file just fine too. So I'm not sure how I have to refer to these files for AutoIT to recognize it and copy it to the folder location I want.

I don't really have a good understanding on how this stuff works, but I was hoping the solution wasn't too complicated, and could use some help.

Any help is appreciated, thanks in advance.

Edited by AnonymousX
Link to post
Share on other sites

I also tried to making a batch file instead since I couldn't get Autoit to work, but wasn't able to get that to work either.

Not working code:

@echo off

set /a flag = 0

echo Locating Folder path of files

set Sourcedir="https://company.com/folder/file%20name.xls"
echo Source = %Sourcedir%

set Destinationdir="%cd%\testing.xls"
echo Destination = %Destinationdir%

if not exist %Sourcedir% set /a flag=1 :if directory doesn't exist set flag

echo Attempting to Copy
if not "%flag%"==1 (copy %Sourcedir% %Destinationdir%  )
if "%flag%"==1 (echo. & echo Error: Source Location Not Found & echo. & echo Program Cancelled)
if not "%flag%"==1 (echo. & echo Copy Complete)


Link to post
Share on other sites

Normal Windows network path has the format like \\DOMAIN\Application\Folder\Filename.xls

But you want to use something like http://... you should use FTP* or TCP* or InetGet to copy files. 

Edited by Nine
Link to post
Share on other sites

To open an Excel workbook from Sharepoint you can use the latest Excel UDF. If needed you will find a fix for the FileExist problem here.

My UDFs and Tutorials:


Active Directory (NEW 2022-02-19 - Version - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version - Download
Outlook Tools (2019-07-22 - Version - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

ADO - Wiki
WebDriver - Wiki


Link to post
Share on other sites

I've done this using a mapped drive before... here's the steps I use

  1. Open Internet Explorer to your sharepoint site
  2. Navigate to the "Documents" folder
  3. In the view options, click "View in File Explorer" (Other browsers don't have this option. I haven't explored what this does in the background either, but if you know Javascript, please tell me what you find!)
  4. Map this directory as a network drive (I like S:\ so I can remember SharePoint)
  5. Copy and paste as you like

I'm not sure if it's my permissions here at work or not, but the mapped drive doesn't stay mapped, so I repeat this process whenever I need to copy paste in. Also note that this won't allow you to set custom properties on your documents. It does, however, seem to overwrite documents and maintain their custom properties if they were previously added.

I think at one point I mapped a drive using "@SSL " somewhere in the path, but I failed to document or remember it. It may be helpful to someone who understands internet tech better :D

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.


My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager

Link to post
Share on other sites

Thanks guys for the comments, but sadly wasn't able to get anything to work. I'm pretty beginner to coding so I feel like this may just be one over my head. If it helps I'm just needing to copy a file from sharepoint locally, it never needs to go the other direction. 


@Nine  I tried using the InetGet function but wasn't able to get it to work, I'll have to do some more reading on it. I gave up a little quick as I was hoping I could get something working with Water's suggestion.


@water I tried using your _Excel_BookOpenEX found in the link you posted but I wasn't able to get it to work either. I just tried opening the file but nothing happened, no errors were created.  

#include <Excel.au3>
#include <File.au3>
#include <Array.au3>
#include <WinAPIFiles.au3>

Local $oExcel =_Excel_Open(true)
$datawb = _Excel_BookOpenEX($oExcel,"https://workspace.company.com/folder/File%20name.xlsm")

; #FUNCTION# ====================================================================================================================
; Author ........: SEO <locodarwin at yahoo dot com>
; Modified.......: litlmike, water, GMK, willichan
; ===============================================================================================================================
Func _Excel_BookOpenEX($oExcel, $sFilePath, $bReadOnly = Default, $bVisible = Default, $sPassword = Default, $sWritePassword = Default, $bUpdateLinks = Default)
    ; Error handler, automatic cleanup at end of function
    Local $oError = ObjEvent("AutoIt.Error", "__Excel_COMErrFunc")
    #forceref $oError
    If Not IsObj($oExcel) Or ObjName($oExcel, 1) <> "_Application" Then Return SetError(1, @error, 0)
    If StringLeft($sFilePath, "HTTP") = 0 And Not FileExists($sFilePath) Then Return SetError(2, 0, 0) ; <== Modified
    If $bReadOnly = Default Then $bReadOnly = False
    If $bVisible = Default Then $bVisible = True
    Local $oWorkbook = $oExcel.Workbooks.Open($sFilePath, $bUpdateLinks, $bReadOnly, Default, $sPassword, $sWritePassword)
    If @error Then Return SetError(3, @error, 0)
    Local $oWindow = $oExcel.Windows($oWorkbook.Name)
    If IsObj($oWindow) Then $oWindow.Visible = $bVisible
    ; If a read-write workbook was opened read-only then set @extended = 1
    If $bReadOnly = False And $oWorkbook.Readonly = True Then Return SetError(0, 1, $oWorkbook)
    Return $oWorkbook
EndFunc   ;==>_Excel_BookOpenEX


@seadoggie01 Thanks for the alternative suggestion, but that isn't going to work for my purposes.





I've been thinking about it and I think Nine, you have the right idea. I was able to open the file by using:


Local $oIE = _IECreate(URL)

So I'm just going to play around with that InetGet.

Edited by AnonymousX
Link to post
Share on other sites

@Nine Thanks mate!

I kept trying with the advice to look at InetGet and got it to work. 

Here is the code for anyone in the future:


#include <InetConstants.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>

; Download a file in the background.
; Wait for the download to complete.


Func DownloadfromSP()
    ; Save the downloaded file from website
    Local $sFilePath = @ScriptDir & "\SP_Downloaded_File.xlsm"

    ; Download the file in the background with the selected option of 'force a reload from the remote site.'
    Local $hDownload = InetGet("https://workspace.company.com/folder/File%20name.xlsm", $sFilePath, $INET_FORCERELOAD, $INET_DOWNLOADBACKGROUND)

    ; Wait for the download to complete by monitoring when the 2nd index value of InetGetInfo returns True.
    Until InetGetInfo($hDownload, $INET_DOWNLOADCOMPLETE)

    ; Retrieve the number of total bytes received and the filesize.
    Local $iBytesSize = InetGetInfo($hDownload, $INET_DOWNLOADREAD)
    Local $iFileSize = FileGetSize($sFilePath)

    ; Close the handle returned by InetGet.



Link to post
Share on other sites

I found where I managed to use FileCopy with our SharePoint site (for anyone who finds this later). I'm working in VBA, but the same principles apply. 

My company SharePoint homepage is: <CompanyName>.sharepoint.com
My department's (sub) page is:               <CompanyName>.sharepoint.com/system/<dept>
The Documents directory is:                    <CompanyName>.sharepoint.com/system/<dept>/Documents/Forms/Standard View.aspx
When copying I use:                                \\<CompanyName>.sharepoint.com@SSL\system\<dept>\Documents\*

FileCopy never has had an issue with this (assuming I pass a correct filename)

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.


My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By TheSaint
      I like and have been using TeraCopy, a third party program, for many years. Mostly it is a great program, but it does have some issues. On Windows XP for instance, Thumbs.db files could often hold up a copy or move process until the user manually responded to the error prompt. Some other issues I discuss below.
      PLEASE NOTE - I am not related to or affiliated in any way with the 3rd party TeraCopy program developers.
      ALSO NOTE - I myself have only tested TeraCopy Cure at this point, on Windows 7 (32 bit), and only with the free version of TeraCopy 2.27.
      This program, TeraCopy Cure, is related to another one of my TeraCopy assistant programs, TeraCopy Timer, but aims at being simpler and quicker to use ... if lacking its more advanced features.
      TeraCopy Cure is a frontend for TeraCopy and sets out to make up for its flaws and limitations.
      One of those flaws is queuing order, and the limitation relates to a same destination issue.
      You would think that queue order would be the same as add order, but that is not the case, and if you are doing a mix of COPY and MOVE then the COPY process could easily fail. It could fail with some source items, even if the  COPY process has already started before the same source MOVE process begins ... especially if the destination folder is on the source drive ... only the currently copying file is locked to that process.
      If you drag and drop another source for a same destination as an existing or impending COPY or MOVE process, then usually TeraCopy nicely adds it for you to that existing job. However, you might wish to avoid that, or it might occur during the testing phase of that active process, and then not be properly processed etc. But you are not given a choice and it just gets added.
      HOW TO USE
      See the right-click menu of the 'Batch List' (lowest field) and the right-click menu of the Tree field, for some useful options.
      (1) If desired, enable 'Auto Start'. NOTE - Even if enabled, this can be bypassed.
      (2) Set the destination folder, either by browsing on the tree or by dragging a folder to the Destination input field or label. Right-clicking on a folder in Explorer will also work, if enabled. The destination folder path will also show in the Tree if that right-click option is enabled.
      (3) Then drag & drop source file or folder onto one of three five areas - Folder (Drive) Tree field, or COPY or MOVE buttons ... this now also includes the source input and label.
      NOTE - If the Folder (Drive) Tree field is used, then you will additionally need to click either the COPY or MOVE button, to have that job added to the Batch List ... but this avoids 'Auto Start' if it is enabled. Drag & dropping to the buttons instead, saves on clicking, but starts the first job etc immediately if 'Auto Start' is enabled.
      (4) If needed and ready, click the START button to run the first job and those that follow.
      More information is included in program and in the NOTES etc sections below.
      WARNING - Depending on the amount of content on your destination drive, and folder level depth, and the speed of your PC, display of the full path in the Folder (Drive) Tree can take a while to show ... if you have that (right-click) option enabled ... it isn't by default. I found this feature quite tricky to get right, and I'm still not 100% sure it is now full-proof.
      TeraCopy Cure v1.5.zip  (source is included)
      TeraCopy Cure v1.6.zip  (source is included)
    • By AlMax3000
      You can make a program or solution that copies all files of a certain extension, example  > .rar (only rar),  and copies them to a directory
      too simple I solved 🙂

    • By Subz
      Our Microsoft Office Templates shared folder was changed from a DFS share to an Isilon share. example:
      Old Server: \\Domain.com\Office\Templates
      New Server: \\Templates.domain.com\Office\Templates
      The team making the changes overlooked that several hundred thousand documents, had been attached to the old template documents.  So when you open a document which has been attached, it will take a couple of minutes to open, while it tries to locate the old server path.  I've been asked to come in and fix it, so after several hours found that the data is being held in document.zip\word\_rels\settings.xml.rels, I now need to replace the old server path with the new server path.  I didn't want to use dom as that would take too long and found a tool wtc https://github.com/NeosIT/wtc which  works perfectly, takes about 8 minutes to scan a single directory with 4000 documents and fix them.  The problem is the documents are all held on sharepoint and they want to retain the file timestamp, which is easy enough, but they also don't want to keep the "Modified By" apparently they don't like seeing all the documents appearing as "Modified by: Subz"  Anyone know of way to retain the "Modified By" info,
    • By hugomito
      Hi all,
      I hope you can help me with this need. In the company I work for, every time we need to deploy a change into a Production environment, we need to add a SharePoint entry in a Calendar area. This is in order to notify Operations team that a change will be introduced on a specific date.
      The fields we need to fill out are:
      ows_Project ows_ChangeReq ows_Description ows_StartDate ows_EndDate The thing is that, I have all these values available in an AutoIT application I built called EAI Tool so, what I need is: Whenever a user click a button, a SharePoint record should be created populating SharePoint fields with the values from EAI Tool applicacion.
      If you need additional information, let me know.
    • By boy233
      I wanted to know if you have UDF for Sharepoint ?! An example?
  • Create New...