0

I'm trying to copy one folder with every subfolder and files to another directory. Now what I want to do is rename the copied folder after a specific scheme.

For example the folder is for the category "Train", I want to add "B_" to the name.

What I have for now is following:

$PathFolder = $FileBrowser.SelectedPath
# $PathFolder2 = "B_" + $FileBrowser.SelectedPath

$PathDestination = "C:\Users\*\Desktop\"

$charCount = ($PathFolder.ToCharArray() | Where-Object {$_ -eq '\'} | Measure-Object).Count

$FolderName = $PathFolder.split('\')[$charCount]

if ($CB_Bahnlinien.Checked) {
    $x = Copy-Item -Path $PathFolder -Destination $PathDestination -Recurse -Force -PassThru | Rename-Item -NewName "B_$FolderName" -Verbose
    # $x = Rename-Item -Path "$PathFolder" -NewName "B_$FolderName" -PassThru | Copy-Item -Path $PathFolder2 -Destination $PathDestination -Recurse 
    if($x) {
        $error_msg = $error[0].exception.message
        [System.Windows.Forms.MessageBox]::Show($error_msg,"Achtung!",0)
    } else {
        [System.Windows.Forms.MessageBox]::Show("Erfolgreich kopiert " + $FolderName,"",0)
    }
}

The Problem is that it copies the folder with the content and besides that it creates a new folder with the name as it should be but without the content. Also it doesn't even copy everything from the folder?!

Lukas
  • 1
  • 2
    Your `Rename-Item` will rename all files to the same name which is `B_$Foldername`. You may be better off using `gci` to get a list of files and then give the new name as the target to `Copy-Item`. – Mark Elvers Feb 01 '21 at 11:37
  • Here is an [example](https://stackoverflow.com/a/65428281/7571258) of mine for a `Get-ChildItem | Copy-Item` pipeline. Remove the `Where-Object` line and adjust `$destinationSubDirPath` as needed. – zett42 Feb 01 '21 at 12:28

0 Answers0