Paulo
Paulo

Reputation: 361

Powershell Function Copy Files

I´m facing to an issue that i can´t figure it out, how can i solve. Basically i´ve an function that copy files from one directory to another directory, and rename it, if they already exists.

In the mean while, if any file with case sensitive name, it don´t copy it at all.

example:

TEXT.xml

text.xml

It just copy one of them. i need to copy both files. Now, is the Rename-Item or the Copy-Item, that can´t deal with this case sensitive? Any idea how can i solve this?

Thanks for any help

My code:

Function Copy-File {
[CmdletBinding()]
param(
    [Parameter(Mandatory = $true, Position = 0)]
    [string]$Origin, 

    [Parameter(Mandatory = $true, Position = 1)]
    [string]$Destination  
)

# check if a file with that name already exists within the destination
$fileName = Join-Path $Destination ([System.IO.Path]::GetFileName($Origin))
if (Test-Path $fileName -PathType Leaf){
    $baseName  = [System.IO.Path]::GetFileNameWithoutExtension($Origin)
    $extension = [System.IO.Path]::GetExtension($Origin)                # this includes the dot
    $allFiles  = Get-ChildItem $Destination | Where-Object {$_.PSIsContainer -eq $false} |  Foreach-Object {$_.Name}
    $newName = $baseName + $extension
    $count = 1
    while ($allFiles -contains $newName) {
        $newName = [string]::Format("{0}({1}){2}", $baseName, $count.ToString(), $extension)
        $count++
    }
    # rename the file already in the destination folder
   # Write-Verbose -Message "Renaming file '$fileName' to '$newName'"
    Rename-Item $fileName -NewName $newName
    }

   #Write-Verbose -Message "Moving file '$Origin' to folder '$Destination'"
   Copy-Item $Origin $Destination 
 }

Upvotes: 0

Views: 880

Answers (1)

postanote
postanote

Reputation: 16076

All of what you are after here is just doing this... (always use validation testing for all destructive commands/code --- (Delete/Rename/Modify/Update/Move))

Clear-Host
$SourceFiles = 'D:\Temp\Source'
$Destination = 'D:\Temp\Destination'
$Counter     = 0

Get-ChildItem -Path $SourceFiles | 
ForEach{
    Try
    {
        If(Test-Path -Path $Destination\$PSItem)
        {
            $Counter++
            Write-Warning -Message "$($PSItem.Name) already exits. Renaming destination file."
            Rename-Item -Path $Destination\$PSItem -NewName "$($PSItem.Basename)_$Counter$($PSitem.Extension)" -WhatIf
            # Copy-Item -Path $PSItem.FullName -Destination $Destination -WhatIf
        }
        Else
        {
            Write-Verbose -Message "$($PSItem.Name) does not exist. Copying file." -Verbose
            Copy-Item -Path $PSItem.FullName -Destination $Destination -WhatIf      
        }
    }
    Catch {$PSItem.Exception.Message}
}
# Results
<#
VERBOSE: 5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube.url does not exist. Copying file.
What if: Performing the operation "Copy File" on target "Item: D:\Temp\Source\5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube.url Destination: D:\Temp\Destination\5 Free Software You'll Wish You Knew Earlier! 2019 - YouTube.url".
WARNING: audiolengthCLEAN.csv already exits. Renaming destination file.
What if: Performing the operation "Rename File" on target "Item: D:\Temp\Destination\audiolengthCLEAN.csv Destination: D:\Temp\Destination\audiolengthCLEAN_2.csv".
...
#>

Update as per your function question.

Function Sync-FileArchive
{
    [CmdletBinding(SupportsShouldProcess)]
    [Alias('sfa')]

    Param
    (
        [Parameter(Mandatory = $true, Position = 0)]
        [string]$SourceFiles, 

        [Parameter(Mandatory = $true, Position = 1)]
        [string]$Destination 
    )

    $Counter     = 0

    Get-ChildItem -Path $SourceFiles | 
    ForEach{
        Try
        {
            If(Test-Path -Path $Destination\$PSItem)
            {
                $Counter++
                Write-Warning -Message "$($PSItem.Name) already exits. Renaming destination file."
                Rename-Item -Path $Destination\$PSItem -NewName "$($PSItem.Basename)_$Counter$($PSitem.Extension)" -WhatIf
            }
            Else
            {
                Write-Verbose -Message "$($PSItem.Name) does not exist. Copying file." -Verbose
                Copy-Item -Path $PSItem.FullName -Destination $Destination     
            }
        }
        Catch {$PSItem.Exception.Message}
    }
}

# Example1 - Remove the -WhatIf to fully execute.
Sync-FileArchive -SourceFiles 'D:\Temp\Source' -Destination 'D:\Temp\Destination' -WhatIf

# Example2
sfa 'D:\Temp\Source' 'D:\Temp\Destination' -WhatIf

Upvotes: 1

Related Questions