4

I'm pretty new to scripting, as well as to PowerShell. What I'm trying to do is deploy files from one folder (C:\Deployments\QA\QADEP-1504\Processor Handlers\Release) to another folder on a remote server (\HQDEVAPP004\C$\Lonestar\ProcessorHandlers\Test). And within that 'Test' folder, there are 3 subfolders (1, 2, and RejectedTransactionHandler).

So I want all of the files to deploy to folders 1 and 2, but not the RejectedTransactionHandler folder. I've tried using the '-exclude' function but that seems to be only good for files, not folders, but I could be wrong. The files deploy to all three folders every time. Here is what I have so far:

Location of starting directory

$_SourcePath = "C:\Deployments\QA\QADEP-1504\Processor Handlers\Release"

List all folders in local directory

$PH = Get-ChildItem '\\HQDEVAPP004\c$\LoneStar\ProcessorHandlers\Test\' -Directory
Clear-Host

foreach ($folder in $PH.Name) {
Get-ChildItem -recurse ($_SourcePath) | Copy-Item -Destination "\\HQDEVAPP004\C`$\Lonestar\ProcessorHandlers\Test\$folder" -PassThru 
}
```
Zero596
  • 83
  • 1
  • 10
  • 1
    I'm not sure if I understand your problem, but can you use `if ($folder -ne 'RejectedTransactionHandler'`? – Tuor Jan 27 '22 at 23:56
  • So you want the exact same files in 1 and 2 and not the other folder? – Abraham Zinala Jan 28 '22 at 00:51
  • @AbrahamZinala That is correct. – Zero596 Jan 28 '22 at 15:51
  • @Tuor Sorry, my editing on this platform is pretty bad. New to the site, as well as any forum. I can see if I can edit it out. Thanks! – Zero596 Jan 28 '22 at 15:52
  • @Tuor Basically I'm trying to move the files from "C:\Deployments\QA\QADEP-1504\Processor Handlers\Release" to the destination "\\HQDEVAPP004\C`$\Lonestar\ProcessorHandlers\Test" but I want the files to go into the subfolders '1' and '2.' There is a third subfolder I don't want the files to go into, which is RejectedTransactionHandler. – Zero596 Jan 28 '22 at 15:55
  • 1
    @Tuor Your method also worked like a charm and was easier to understand. Thank you as well! – Zero596 Feb 01 '22 at 00:44

2 Answers2

2

Add if ($folder -ne 'RejectedTransactionHandler'), so when looping through the folders, it won't copy to the RejectedTransactionHandler folder.
The PowerShell -ne comparison operator returns $true if the two values are not equal, and $false if they are. Here are the relevant docs.

Tuor
  • 273
  • 1
  • 13
1

To knock this task out [K.O. style] you could...

  1. Create an array variable to contain the folder name(s) to omit.
  2. Create another variable containing the directory names of the subfolders beneath the source directory excluding the omitted subfolders specified.
  3. Then loop over the source variable to...
    • Create destination subfolder(s) if it does not exist
    • Copy the source subfolder(s) content to the correlate destination folder(s)

PowerShell

#$ExcludeFolders = "RejectedTransactionHandler","FolderXYZ"
$ExcludeFolders = "RejectedTransactionHandler"
$_Source = Get-ChildItem 'C:\Folder\Source' -Directory | ? { $_.Name -notin $ExcludeFolders };
$_Destination = "C:\Folder\Destination\Test";

$_Source | % { 
    If (!(Test-Path "$_Destination\$($_.Name)")){ New-Item -Path "$_Destination\$($_.Name)" -ItemType Directory -Force; }
    Copy-Item "$($_.FullName)\*" -Destination "$_Destination\$($_.Name)" -PassThru
    }

Supporting Resources

Vomit IT - Chunky Mess Style
  • 40,038
  • 27
  • 84
  • 117
  • Thank you for this. I will try and use this to see if it works for me. I've heard about the array variable but wasn't sure how to use it just yet. I will comment back if I have any questions or got it working! – Zero596 Jan 28 '22 at 15:57
  • 1
    Thanks a bunch! You were a great help! I got it to work with your help! – Zero596 Jan 31 '22 at 17:20