0

i have a masterfolder with 1000 files, they are reviewed. I have the same older files in a destination folder spreaded out in 100's of subfolders. I need to move the files from the masterfolder to the destination folder based on matching filenames and replace them.

Is there someone who can help me?

my ps:

$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

error: Move-Item : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. At line:6 char:111 + ... earchdir -recurse | select fullname} | Move-Item $source $destination + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (@{FullName=C:\U...\submap\2.docx}:PSObject) [Move-Item], ParameterBindingException + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.PowerShell.Commands.MoveItemCommand

1
  • So what errors are you getting? What is not working for you? Please edit the question and be explicit.
    – DavidPostill
    Commented Apr 12, 2020 at 10:59

1 Answer 1

0

What you are trying to do is PowerShell beginner stuff.

Firstly, your search and destination are the same and that is not a thing to do. If you tired that in Windows Explorer it would yell at you saying the destination is that same. Thus that $searchfile variable is also moot.

Why are you just not using the built-in Windows robocopy.exe?

robocopy | Microsoft Docs

That is why it exists. Don't script if you don't have to, especially when the OS already provides a tool for the job. The Windows robocopy.exe is far more performant than what you are doing.

Your end-use case is to mirror the souredir to the destdir. robocopy has a mirror option.

/mir Mirrors a directory tree (equivalent to /e plus /purge). For additional information, see Remarks.

Robocopy /MIR switch – mirroring file permissions

As far as your script is concerned, you have way over complicated this. If all you are doing is mirroring the source to that user profile, robocopy will do this, but if you just want to use PowerShell, just because, this the process is far simpler that what you are doing.

# 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                                                                                                         

#>
1
  • Hi Postanote, Thanks for the answer: i get a step closer but the results isn't there. first: it says "move-item" : it cant make a file when it already exists. I solved that with the -force command at the end. But the second one - the moved files only appear in the top folder and not in the subfolder... (the topfolder whas the file that moved correctly). Can you help me with moving the files also to the subfolders where the filename based replica is? Commented Apr 15, 2020 at 8:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .