0

I need to copy a lot of files and use the same sort of folder structure where the files needs to go. So for instance if I have the following two documents:

\\Server1\Projects\OldProject\English\Text_EN.docx
\\Server1\Projects\OldProject\English\Danish\Text_DA.docx

I would need to move them to a new place on the server, but they need to be in the same "language folder". So I need to move them like this:

\\Server1\Projects\OldProject\English\Text_EN.docx -> \\Server1\Projects\NewProject\English\Text_EN.docx
\\Server1\Projects\OldProject\English\Danish\Text_DA.docx -> \\Server1\Projects\NewProject\English\Danish\Text_DA.docx

The issue here is, that I would need to take names of the "language" folder and create them in the NewProject folder.

How would I be able to take and remove everything before the \, so I end up with only having the "language" folders like English\ and English\Danish

wads
  • 123
  • 11
  • 1
    Assuming your current directory is ``\\Server1\Projects\OldProject\``, doesn't `Copy-Item .\* -Destination \\Server1\Projects\NewProject\ -Recurse` work? – Santiago Squarzon Nov 07 '22 at 14:07
  • Use [`-Container` on Copy-Item](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/copy-item?view=powershell-7.2#example-2-copy-directory-contents-to-an-existing-directory). StackOverflow [answer](https://stackoverflow.com/questions/5432290/how-to-use-powershell-copy-item-and-keep-structure) – Ash Nov 07 '22 at 14:08
  • The problem is that I need to create the folders with `New-Item` and in order to that, I'd need to know the names of the folder I need to create. I have 5 "main language" folders, where the main language file goes in and within that "main language" folder, I need to have "secondary language" folder. So let's say, someone has a file called `Test_EN` as the main, it would need to go to the `\English\Test_EN` and if he has a secondary one called `Test_DA` then it needs to go into `\English\Danish\Test_DA` – wads Nov 07 '22 at 14:46

2 Answers2

0

If the goal it to just replace the 'OldProject' folder with 'NewProject' in the file path you could use replace to make the change to the file path:

$filePath = Get-ChildItem \\Server1\Projects\OldProject\English\Text_EN.docx
Copy-Item $filePath.FullName -Destination ($filepath.FullName -replace "\bOldProject\b", "NewProject")

The '\b' is used to do a regex EXACT match for anything inside the tags.

Shamus Berube
  • 466
  • 3
  • 12
0

Try the following, which, for each input file:

  • constructs the target dir. path by replacing the old project dir.'s root path with the new one's, thereby effectively replicating the old dir.'s subdirectory structure.

  • makes sure that the target dir. exists

  • then copies the input file to the target dir.

$oldProjectRoot = '\\Server1\Projects\OldProject'
$newProjectRoot = '\\Server1\Projects\NewProject'

Get-ChildItem -Recurse -Filter *.docx -LiteralPath $oldProjectRoot |
  ForEach-Object {
    # Construct the target dir. path, with the same relative path 
    # as the input dir. path relative to the old project root.
    $targetDir = 
      $newProjectRoot + $_.Directory.FullName.Substring($oldProjectRoot.Length)
    # Create the target dir., if necessary (-Force returns any preexisting dir.)
    $null = New-Item -Force -Type Directory $targetDir
    $_ # Pass the input file through.
  } |
  Copy-Item -Destination { $targetDir } -WhatIf

Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.

mklement0
  • 382,024
  • 64
  • 607
  • 775