【问题标题】:Powershell script to Move files from one folder to other in same document libraryPowershell脚本将文件从一个文件夹移动到同一文档库中的另一个文件夹
【发布时间】:2020-10-09 15:21:59
【问题描述】:

我有一个包含多个文件夹的文档库“Documents1”。尝试编写一个 powershell 脚本将放置在多个子文件夹中的文件(必须从 .xlsx 读取)移动到同一 doc lib 下另一个名为“redundant”的文件夹。 对 powershell 来说很新,只是在下面尝试了一些东西,但没有帮助

$Web = Get-SPWeb "test.com"
$DocLib = $Web.Lists["Documents1"]           
$filenames  = Get-Content 'F:\sample\test4.xlsx' | Select-Object -Unique

$result = foreach($name in $filenames) {
    $move = foreach ($ListItem in ($DocLib.Items | Where-Object { $_["Name"] -like "*$name*" })) {
        $File = $DocLib.Files.Add($ListItem.Name, $ListItem, $true)
        }
    }
}

【问题讨论】:

  • 需要更多信息。听起来您正试图将文件从 Documents1 文件夹下的多个子文件夹移动到“冗余”文件夹。这个对吗?如果是这样,我在您的代码中看不到“冗余”,xlsx 文件中存在哪些信息?是选定的文件名吗?运行上述内容时您收到的错误/结果是什么?
  • 是的,试图将文件从多个子文件夹移动到冗余文件夹。并且 XLSX 文件包含 pdf 文件名(例如 adbc.pdf),需要从多个子文件夹中搜索才能将其移动到 Redundant。当我尝试在我的 powershell 窗口上方运行时,它刚刚关闭。
  • 从 xlsx 读取是一个硬性要求吗?要从 excel 中读取,您需要使用 PSExcel 模块。如果将文件名放在txt文件中会更容易。
  • no..我最初尝试使用 txt 文件...没有 xlsx 这样的硬规则。无论如何,我尝试关闭我的 powershell 窗口。我应该在哪里提到要移动到冗余文件夹的文件?

标签: powershell sharepoint document


【解决方案1】:

假设您不介意将文件列表保存在 txt 文件中。您可以使用以下内容。

$SOURCEDIR="C:\Users\YOUR SOURCE DIR\TEST1"
$TARGETDIR="C:\Users\YOUR TARGET DIR\TEST2"
$FILELIST="C:\Users\LOCATION OF FILE LIST\filelist.txt" 

$filenames  = Get-Content $FILELIST
foreach ($file in $filenames)
{
Get-ChildItem -Path $SOURCEDIR -Recurse | Where-Object -FilterScript {($_.name -match $file)}  | Move-Item -Destination $TARGETDIR
Write-Output "File moved " $file
}

【讨论】:

  • 我尝试了以下方法,但不确定如何将目标库引用到“Doc​​uments1”下的 Sharepoint 文档库中的子文件夹 $Web = Get-SPWeb "test.com" $SOURCEDIR = $ Web.Lists["Documents1"] $TARGETDIR=$Web.Lists["Redundant"] $FILELIST="F:\users\filelist.txt" $filenames = Get-Content $FILELIST foreach ($file in $filenames) { Get-ChildItem -Path $SOURCEDIR -Recurse | Where-Object -FilterScript {($_.name -match $file)} | Move-Item -Destination $TARGETDIR Write-Output "文件已移动" $file }
  • 试过 $TARGETDIR=$SOURCEDIR.RootFolder.Folders["Redundant"] 但收到错误提示无法索引到空数组
  • 下面是我尝试的另一种方式。但似乎没有帮助 $Web = Get-SPWeb "test.com" $SOURCEDIR ="Documents1" $TARGETDIR = "Documents1/Quarantine" $FILELIST="F:\users\filelist.txt" $filenames = Get-内容 $FILELIST foreach ($file in $filenames) { Get-ChildItem -Path $SOURCEDIR -Recurse | Where-Object -FilterScript {($_.name -match $file)} | Move-Item -Destination $TARGETDIR Write-Output "文件已移动" $file }
【解决方案2】:

这是一个在同一个文档库中的文件夹之间移动文件的演示,你可以添加你的判断条件,

$web=Get-SPWeb "http://sp"
    
    $listName="lib"

$list = $web.Lists[$listName]

$TargetFolder = $Web.Url + "/" + $list.RootFolder.Url + "/"+ "bb"

$sourcefolder=$list.rootfolder.SubFolders["folder"]

$files = $sourcefolder.Files

foreach ($item in $files) {

     $File=$Web.GetFile($Item.URL);

     $TargetPath=$TargetFolder+"/"+$file.name;

     $File.MoveTo($TargetPath);

     Write-Host "Moved Item:"$item.Name;

}

【讨论】:

  • $web=Get-SPWeb "test.com" $listName="Documents1" $list = $web.Lists[$listName] $filenames = Get-Content 'F:\users\filelist. txt' $TargetFolder = $Web.Url + "/" + $list.RootFolder.Url + "/"+ "冗余" $sourcefolder=$list.rootfolder.SubFolders["Documents1"] foreach ($name in $filenames) { foreach ($ListItem in ($DocLib.Items | Where-Object { $_["Name"] -like "$name" })) { $File=$Web.GetFile($Item .URL); $TargetPath=$TargetFolder+"/"+$file.name; $File.MoveTo($TargetPath); Write-Host "Moved Item:"$item.Name; } }
  • 尝试了上面的方法,但是得到一个错误,说不能索引到空数组中
  • Get-Content 可能不适合读取 excel 文件。
  • 我试过的另一种方法没有结果 $WebURL="test.com" $SourceLibrary ="/Documents1" $TargetLibrary = "/Documents1/Redundant" $filenames = Get-Content 'F:\ users\filelist.txt' #获取对象 $Web = Get-SPWeb $WebURL $SourceFile = $Web.GetFile($SourceLibrary) $Targetfile = $Web.GetFolder($TargetLibrary) foreach ($file in $filenames) { Get- ChildItem -Path $SourceFile -Recurse | Where-Object -FilterScript {($_.name -match $file)} | Move-Item -Destination $Targetfile Write-Output "文件已移动" $file }
  • $file_list = Get-Content 'F:\users\filelist.txt' $web=Get-SPWeb "test.com" $listName="Documents1" $list = $web.Lists[$ listName] $search_folder = "Documents1" $destination_folder = $Web.Url + "/" + $list.RootFolder.Url + "/"+ "Redundant" foreach($file_list 中的 $file){ $file_to_move = Get-ChildItem -路径 $search_folder -Recurse | Where-Object -FilterScript {($_.name -match $file)} if ($file_to_move) { Move-Item $file_to_move $destination_folder } }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多