![powershell 将主文件夹项目移动到与文件名匹配的目标文件夹](https://linux22.com/image/1620589/powershell%20%E5%B0%86%E4%B8%BB%E6%96%87%E4%BB%B6%E5%A4%B9%E9%A1%B9%E7%9B%AE%E7%A7%BB%E5%8A%A8%E5%88%B0%E4%B8%8E%E6%96%87%E4%BB%B6%E5%90%8D%E5%8C%B9%E9%85%8D%E7%9A%84%E7%9B%AE%E6%A0%87%E6%96%87%E4%BB%B6%E5%A4%B9.png)
我有一个包含 1000 个文件的主文件夹,它们都经过了审核。我在目标文件夹中有相同的旧文件,这些文件分散在 100 个子文件夹中。我需要根据匹配的文件名将文件从主文件夹移动到目标文件夹并替换它们。
有人能幫助我嗎?
我的附言:
$searchdir = "C:\Users\Patrick\Desktop\Testing\verhuizen"
$destination = "C:\Users\Patrick\Desktop\Testing\verhuizen"
$source = "C:\Users\Patrick\Desktop\Testing\masterfolder"
$searchfile = "*.*"
Get-ChildItem $source -recurse | ForEach-Object {Get-ChildItem -Path $searchdir -recurse | select fullname} | Move-Item $source $destination
错误:Move-Item:输入对象无法绑定到命令的任何参数,因为该命令不接受管道输入,或者输入及其属性与接受管道输入的任何参数都不匹配。 在第 6 行字符:111 + ... earchdir -recurse | 选择全名} | Move-Item $source $destination + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidArgument:(@{FullName=C:\U...\submap\2.docx}:PSObject)[Move-Item],ParameterBindingException + FullyQualifiedErrorId:InputObjectNotBound,Microsoft.PowerShell.Commands.MoveItemCommand
答案1
您正在尝试做的是 PowerShell 初学者的事情。
首先,您的搜索和目的地是相同的,这是不应该的。如果您在 Windows 资源管理器中尝试这样做,它会大喊大叫说目的地是相同的。因此,$searchfile 变量也是没有意义的。
为什么您不使用内置的 Windows robocopy.exe?
这就是它存在的原因。如果没有必要,就不要编写脚本,尤其是当操作系统已经提供了用于该工作的工具时。Windows robocopy.exe 的性能远高于你正在做的事情。
您的最终用例是将源目录镜像到目标目录。robocopy 有一个镜像选项。
/mir 镜像目录树(相当于 /e 加 /purge)。有关其他信息,请参阅备注。
就您的脚本而言,您已经将其复杂化了。如果您所做的只是将源镜像到该用户配置文件,那么 robocopy 将会执行此操作,但如果您只是想使用 PowerShell,那么这个过程比您正在做的事情要简单得多。
# See what is in the Source and Destination
'D:\Source','D:\Destination' |
ForEach {
"`n********* Checking contnets of $PSItem *********`n"
Get-ChildItem -Path $PSItem -Recurse
}
<#
# Results
********* Checking contnets of D:\Source *********
Directory: D:\Source
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 14-Mar-20 17:03 here
d----- 13-Apr-20 22:40 NewFolder
-a---- 06-Feb-20 14:02 28817 23694d1213305764-revision-number-in-excel-book1.xls
-a---- 29-Dec-19 21:50 69 5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube.url
-a---- 13-Mar-20 12:19 70 abc.txt
-a---- 02-Feb-20 23:21 0 audiolengthCLEAN.csv
Directory: D:\Source\NewFolder
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 03-Feb-20 11:55 New folder
d----- 20-Jan-20 11:17 temp
-a---- 29-Dec-19 21:50 69 5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube.url
-a---- 10-Jan-20 17:59 0 awél.txt
-a---- 04-Jan-20 02:01 39 hello.bat
-a---- 04-Jan-20 01:43 44 hello.ps1
-a---- 04-Jan-20 02:16 64 mytest - Copy.txt
-a---- 04-Jan-20 02:16 64 mytest.txt
-a---- 19-Jan-20 14:49 230 mytest.zip
-a---- 01-Jan-20 21:34 145 System Restore fails- AppxStaging %ProgramFiles%-WindowsApp 0x80070091 - Windows 10 Forums.url
-a---- 19-Jan-20 20:10 203 Test.csv
-a---- 14-Jan-20 23:03 8552 Test.xlsx
-a---- 14-Jan-20 21:11 988 Test.xlsx - Shortcut.lnk
-a---- 01-Jan-20 20:41 109 Welcome to Microsoft Edge Beta Channel.url
********* Checking contnets of D:\Destination *********
#>
# Move all things in the Source to the destination
'D:\Source\' |
ForEach {
Get-ChildItem -Path $PSItem |
Move-Item -Destination 'D:\Destination' -WhatIf
}
<#
# Results
What if: Performing the operation "Move Directory" on target "Item: D:\Source\here Destination: D:\Destination\here".
What if: Performing the operation "Move Directory" on target "Item: D:\Source\NewFolder Destination: D:\Destination\NewFolder".
What if: Performing the operation "Move File" on target "Item: D:\Source\23694d1213305764-revision-number-in-excel-book1.xls Destination: D:\Destination\23694d1213305764-revision-number-in-excel-bo
ok1.xls".
What if: Performing the operation "Move File" on target "Item: D:\Source\5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube.url Destination: D:\Destination\5 Free Software You'll Wish You
Knew Earlier! 2019 - YouTube.url".
What if: Performing the operation "Move File" on target "Item: D:\Source\abc.txt Destination: D:\Destination\abc.txt".
What if: Performing the operation "Move File" on target "Item: D:\Source\audiolengthCLEAN.csv Destination: D:\Destination\audiolengthCLEAN.csv".
#>
# Remove the whatIf to have the action happen
# Move all things in the Source to the destination
'D:\Source\' |
ForEach {
Get-ChildItem -Path $PSItem |
Move-Item -Destination 'D:\Destination'
}
# See what is in the Source and Destination
'D:\Source','D:\Destination' |
ForEach {
"`n********* Checking contnets of $PSItem *********`n"
Get-ChildItem -Path $PSItem -Recurse
}
<#
# Results
********* Checking contnets of D:\Source *********
********* Checking contnets of D:\Destination *********
Directory: D:\Destination
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 14-Mar-20 17:03 here
d----- 13-Apr-20 22:40 NewFolder
-a---- 06-Feb-20 14:02 28817 23694d1213305764-revision-number-in-excel-book1.xls
-a---- 29-Dec-19 21:50 69 5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube.url
-a---- 13-Mar-20 12:19 70 abc.txt
-a---- 02-Feb-20 23:21 0 audiolengthCLEAN.csv
Directory: D:\Destination\NewFolder
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 03-Feb-20 11:55 New folder
d----- 20-Jan-20 11:17 temp
-a---- 29-Dec-19 21:50 69 5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube.url
-a---- 10-Jan-20 17:59 0 awél.txt
-a---- 04-Jan-20 02:01 39 hello.bat
-a---- 04-Jan-20 01:43 44 hello.ps1
-a---- 04-Jan-20 02:16 64 mytest - Copy.txt
-a---- 04-Jan-20 02:16 64 mytest.txt
-a---- 19-Jan-20 14:49 230 mytest.zip
-a---- 01-Jan-20 21:34 145 System Restore fails- AppxStaging %ProgramFiles%-WindowsApp 0x80070091 - Windows 10 Forums.url
-a---- 19-Jan-20 20:10 203 Test.csv
-a---- 14-Jan-20 23:03 8552 Test.xlsx
-a---- 14-Jan-20 21:11 988 Test.xlsx - Shortcut.lnk
-a---- 01-Jan-20 20:41 109 Welcome to Microsoft Edge Beta Channel.url
#>