如何使用元数据(列)从 Sharepoint 文件夹中检索文件以检查文件是否存在?
How to retrieve files from a Sharepoint Folder with metadata(column) to check wether the file exists or not?
我想使用 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.
}
}
您可以尝试以下方法,它应该适合您。
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.
}
}
我想使用 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.
}
}
您可以尝试以下方法,它应该适合您。
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.
}
}