将仅新文件移动到不同目录 – Powershell

huangapple go评论76阅读模式
英文:

Move only New files to a different directory - Powershell

问题

在PowerShell中运行以下命令,目标是将文件从文件夹2移动到文件夹1,但只移动新文件。同时,希望知道移动了哪些文件,类似于日志文件的排序,如果可能的话。

目前的代码如下:

$DownloadFolder = 'C:\FOLDER1'
$KeepFolder = 'C:\folder2'
$DownloadFiles = Get-ChildItem -Path $DownloadFolder

$KeepFiles = Get-ChildItem -Path $KeepFolder
$FileDiffs = Compare-Object -ReferenceObject $DownloadFiles -DifferenceObject $KeepFiles
$FileDiffs | foreach {
    $copyParams = @{
        'path' = $_.InputObject.Fullname
    }
    $Downloadll = $copyParams.path
    if ($_.SideIndicator -eq '=>')
    {
        Copy-Item $Downloadll -Destination $KeepFolder -Force
    }
}

尝试比较文件夹1和文件夹2,并只移动新文件到文件夹1。但出现了以下错误:

"" Cannot overwrite with the item filename.txt with itself
英文:

Running the following in powershell. Goal is to move files from folder 2 to folder 1, but only new files. Also would like to know which files were moved, sort like a log file if possible

Here is what i have so far :

 $DownloadFolder = 'C:\FOLDER1' 

 $KeepFolder = 'C:\folder2'

 $DownloadFiles = Get-ChildItem -Path $DownloadFolder


    $KeepFiles = Get-ChildItem -Path $KeepFolder
    $FileDiffs = Compare-Object -ReferenceObject $DownloadFiles -DifferenceObject $KeepFiles
    $FileDiffs | foreach {
    $copyParams = @{
     'path' = $_.InputObject.Fullname
       }
 $Downloadll = $copyParams.path
 if ($_.SideIndicator -eq '=>')
  {
   Copy-Item $Downloadll -Destination $KeepFolder   -force
 }
}

Trying to compare folder 1 and folder 2 and move ONLY new files to folder1. But getting the following error:

  "" Cannot overwrite with the item filename.txt   with itself

答案1

得分: 4

我会考虑使用"robocopy"命令。

它可以仅移动更新的文件,并提供所需的日志。

https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy
https://serverfault.com/questions/129098/how-to-get-robocopy-running-in-powershell

英文:

I would look into using the "robocopy" command.

It can move the newer files only, and it gives you the log you wanted.

https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy
https://serverfault.com/questions/129098/how-to-get-robocopy-running-in-powershell

答案2

得分: 0

第一行$downloadfolder后面有一个冒号。

拼写错误。

你想要使用<=而不是=>。否则它会尝试复制到第一个文件夹。

在copy-item后添加-passthru以获取输出。

英文:

There is a colon after $downloadfolder on the first line.

It's spelled wrong also.

You want &lt;= not =&gt;. Otherwise it tries to copy to the first folder.

Add -passthru to copy-item to get the output.

huangapple
  • 本文由 发表于 2020年1月7日 02:49:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定