【问题标题】:How to retrieve files from a Sharepoint Folder with metadata(column) to check wether the file exists or not?如何从带有元数据(列)的 Sharepoint 文件夹中检索文件以检查文件是否存在?
【发布时间】:2021-01-21 07:48:53
【问题描述】:

我希望使用 Add-PnPFile 将文件上传到 SharePoint,但它每次都会覆盖文件。 我想检查文件是否存在?如果是,那么是否在最后一天进行了修改? 如果是,那么我将上传文件(使用 add-pnpfile 覆盖现有文件)否则我跳过文件并检查下一个文件?

还有吗?

#install and import module
Import-Module SharePointPnPPowerShellOnline 

#Get Connection to the url
Connect-PnPOnline "Some SharePoint Url" -UseWebLogin

#get the files from a local folder
$Files = Get-ChildItem "C:\Some Local FolderPath" -Recurse

#Now check wether the file is being modified or not ? 
foreach($File in $Files){
    
#Logic to check if the file exists in the sharepoint folder or not ? If it exist then i want to check wether it is being modified or not ? if yes then i will add the file
#if not then i will skip that file and check for other files.
        
        #Calculating LastWritetime for the file.
        $LastWriteTimeForThisFile = $File.LastWriteTime.Day
        $difference = (Get-Date).Day - $LastWriteTimeForThisFile
    
        if(file exists ? then check the difference in Lastmodified dates)
        if ( $difference -lt 1) 
        {
         
         $upload = Add-PnPFile -Path $File.FullName -Folder $SharePointFolderPath
         $message = "Successfully Uploaded"  #this i will be having an entry in log file.
        }
        else{
         $message = Not Modified so not uploading again #this i will be having an entry in log file.
        }
    }

【问题讨论】:

    标签: powershell sharepoint file-upload sharepoint-online last-modified


    【解决方案1】:

    您可以尝试以下方法,它应该适合您。

    foreach($File in $Files){
        #Calculating LastWritetime for the file.
        $LastWriteTimeForThisFile = $File.LastWriteTime
        $difference = (Get-Date) - $LastWriteTimeForThisFile
        
        #Check if File exists Already
        $fileURL= $SharePointFolderPath+"/"+$file.name
    
        $FileExists = Get-PnPFile -Url $fileURL -ErrorAction SilentlyContinue
        
        if($FileExists){
            if ( $difference.days -lt 1) {         
             $upload = Add-PnPFile -Path $File.FullName -Folder $SharePointFolderPath
             $message = "Successfully Uploaded"  #this i will be having an entry in log file.
            }
            else{
             $message = Not Modified so not uploading again #this i will be having an entry in log file.
            }
        }
        #If File doesnot exist, upload the file.
        Else{
            $upload = Add-PnPFile -Path $File.FullName -Folder $SharePointFolderPath
            $message = "Successfully Uploaded"  #this i will be having an entry in log file.
        }
    }
    

    【讨论】:

    • 非常感谢 :) 它确实做到了应有的效果
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-15
    • 2012-02-05
    • 2010-12-10
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多