locked
Robocopy Files Selectively RRS feed

  • Question

  • I have a requirement to copy files from one location to another.  I am accomplishing this task using RoboCopy.

    robocopy $item.FullName $destination /E /XO /R:2 /W:2 /MT[:n] /LOG:$log_files

    Unfortunately, this copies everything and I only need to copy only certain files.  

    Example:

    There are duplicate files and some have .OLD at the end. I only want to copy given these conditions:

    1. If files have .OLD copy.
    2. If another folder does not have a duplicate of .OLD copy this folder.

    I am unable to get past the second part. For the first part its simple because I just use a if statement and use -LIKE ".OLD".

    Thanks in Advance!

    Wednesday, August 12, 2015 5:33 PM

Answers

  • Sure, here's a starter:

    Get-ChildItem .\ -Directory | ForEach {
    
        If ($_.Name -notlike '*.OLD') {
    
            If (!(Test-Path -Path "$($_.FullName).OLD")) {
    
                Write-Host "Folder without an OLD detected - $($_.FullName)" -ForegroundColor Green
            }
    
        } Else {
    
            Write-Host "OLD detected - $($_.FullName)" -ForegroundColor Red
    
        }
    
    }
    
    
    Example output:
    OLD detected - C:\Scripts\PowerShell Scripts\Misc Testing\8-12-2015\TEST\folder1.OLD
    OLD detected - C:\Scripts\PowerShell Scripts\Misc Testing\8-12-2015\TEST\folder2.OLD
    Folder without an OLD detected - C:\Scripts\PowerShell Scripts\Misc Testing\8-12-2015\TEST\folder3
    


    • Marked as answer by LluvianBlack Wednesday, August 12, 2015 6:15 PM
    Wednesday, August 12, 2015 5:59 PM

All replies

  • Hi,

    Perhaps I'm missing it, but what's your question?


    Wednesday, August 12, 2015 5:41 PM
  • Hi Mike,

    Is there an easy way to copy files where there are duplicates and I only want the one that has .OLD at the end and copy the one that does not have .OLD if a folder with .OLD at the end does not exist?

    folder1.OLD

    folder1

    folder2.OLD

    folder2

    folder3

    The script would copy only folder1.OLD, folder2.OLD, and folder3.

    Wednesday, August 12, 2015 5:49 PM
  • Sure, here's a starter:

    Get-ChildItem .\ -Directory | ForEach {
    
        If ($_.Name -notlike '*.OLD') {
    
            If (!(Test-Path -Path "$($_.FullName).OLD")) {
    
                Write-Host "Folder without an OLD detected - $($_.FullName)" -ForegroundColor Green
            }
    
        } Else {
    
            Write-Host "OLD detected - $($_.FullName)" -ForegroundColor Red
    
        }
    
    }
    
    
    Example output:
    OLD detected - C:\Scripts\PowerShell Scripts\Misc Testing\8-12-2015\TEST\folder1.OLD
    OLD detected - C:\Scripts\PowerShell Scripts\Misc Testing\8-12-2015\TEST\folder2.OLD
    Folder without an OLD detected - C:\Scripts\PowerShell Scripts\Misc Testing\8-12-2015\TEST\folder3
    


    • Marked as answer by LluvianBlack Wednesday, August 12, 2015 6:15 PM
    Wednesday, August 12, 2015 5:59 PM