1
votes

I'm trying to create an array that will remove files from the destination path and then copy from the source path to the destination path. I've created a .txt document on the build server with a list of files with their relative path. When I run the below block of code it's removing all contents in folder B and copy Folder A(without any contents) to Folder B.

This is what I'm running

$files = get-content "C:\files.txt"
foreach ($filepath in $files)
{
    $source = '\\Server1\Folder A' + $filepath
    $dest = '\\Server2\Folder B' + $filepath
    foreach ($removefile in $dest)
    {
       rd $removefile -recurse -force
    }
    foreach ($addfile in $source)
    {
        cp $addfile -destination $dest
    }
}

Soda,

I've tried your suggestion but it's trying to remove from/copy to the incorrect directory.

Code:

$targetList = Get-Content "C:\MCSfiles.txt"

foreach ($target in $targetList) {

    $destPath = Join-Path "\\Server2\MCSWebTest" $target
    $destFiles = Get-ChildItem $destPath

    foreach ($file in $destFiles) {
       Remove-Item $file -Recurse -Force
    }

    $sourcePath = Join-Path "\\Server1\WebSites\McsWeb2" $target
    $sourceFiles = Get-ChildItem $sourcePath

    foreach ($file in $sourceFiles) {
        Copy-Item $file -Destination $destPath
    }
}

Error:

Remove-Item : Cannot find path 'C:\Program Files (x86)\Jenkins\jobs\MCSTest\workspace\App_Code' because it does not exist. At C:\Users\SVC-VI~1\AppData\Local\Temp\jenkins5893875881898738781.ps1:9 >char:1 9 + Remove-Item <<<< $file -Recurse -Force + CategoryInfo : ObjectNotFound: (C:\Program >File...kspace\App_Co de:String) [Remove-Item], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.Remov eItemCommand

Copy-Item : Cannot find path 'C:\Program Files (x86)\Jenkins\jobs\MCSTest\works pace\App_Code' because it does not exist. At C:\Users\SVC-VI~1\AppData\Local\Temp\jenkins5893875881898738781.ps1:16 >char: 18 + Copy-Item <<<< $file -Destination $destPath + CategoryInfo : ObjectNotFound: (C:\Program >File...kspace\App_Co de:String) [Copy-Item], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.CopyI temCommand

Soda,

Neither of the suggestions work. It's still removing everything in the destination directory and adding the source directory folder to the destination directory without files. I'm a little lost here.

2
Any new error message?sodawillow

2 Answers

0
votes
  1. you are missing backslashes in your paths, use Join-Path to build your strings to avoid that (or maybe leading backslashes are included in files.txt)

  2. you iterate on $dest and $source but these are strings, not file collections, which you should be able to retrieve with Get-ChildItem $dest and Get-ChildItem $source, for instance

Also, for readability, you should not use aliases in scripts (rd and cp)

PS: I believe your script produces errors, you should include them in your question (you can edit it)

EDIT regarding comments:

Try this (untested, remove -WhatIf's to process):

$targetList = Get-Content "D:\files.txt"

foreach ($target in $targetList) {

    $destPath = Join-Path "D:\destination" $target
    Remove-Item $destPath -Recurse -Force -WhatIf

    $sourcePath = Join-Path "D:\source" $target
    Copy-Item $sourcePath -Destination $destPath -Recurse -WhatIf

}

EDIT2: I have corrected and simplified the code above, my logic was slightly off.

This could even be simpler, and better, to group remove statements and run them before the copy statements, like:

$targetList = Get-Content "D:\files.txt"

#remove all
$targetList | ForEach-Object {
    Remove-Item (Join-Path "D:\destination" $_) -Recurse -Force -WhatIf
}

#copy all
$targetList | ForEach-Object {
    Copy-Item (Join-Path "D:\source" $_) (Join-Path "D:\destination" $_) -Recurse -Force -WhatIf
}

Both snippets have been tested with a sample folder structure.

Just for the sake of completeness, the error you got with my first attempt was due to passing the $file objects to the processing cmdlets, instead of full paths ($file.FullName).

0
votes

The issue is you are trying to loop too many times. The outer loop processes the files one at a time. There should be no inner loop. Also, you aren't validating the file exists before issuing a deletion.

$files = Get-Content "C:\test.txt"
$source_base="C:\TEMP\source\"
$dest_base="C:\TEMP\dest\"

foreach ($filepath in $files)
{
    $source = $source_base + $filepath
    $dest = $dest_base + $filepath

    if(Test-Path $dest) {rd $dest -recurse -force}
    cp $source -destination $dest
}