0

What I have so far:

Get-ChildItem -Path "\\Networkdrive\Folder1\PROD- TEST\Successful" -Recurse |
Where-Object name -match '\w{10}_\w{3}' |
Move-Item -destination "\\Networkdrive\Folder1\Archive- TEST\Archive_PROD_TEST\Successful" -Verbose

Inside of : \\Networkdrive\Folder1\PROD- TEST\Successful are folders that look like: 9ce8eab8-cefe-4ec9-9158-ff40612d5c47 and then in them are more folders that have criteria 10 numbers with an underscore (_) and then followed by 3 numbers.

I want to move the 9ce8eab8-cefe-4ec9-9158-ff40612d5c47 folder if any subfolder contains the criteria of 10 characters followed by an _ and then and other 3 characters to \\Networkdrive\Folder1\Archive- TEST\Archive_PROD_TEST\Successful, but I want to leave the Structure and files alone.

Right now the code just grabs the folders inside and then moves them, but leaves the 9ce8eab8-cefe-4ec9-9158-ff40612d5c47 folders alone. I want 9ce8eab8-cefe-4ec9-9158-ff40612d5c47 to be moved too.

James
  • 4,211
  • 1
  • 18
  • 34
  • To maybe be more clear. I want to move a folder based on the names of the subfolders. – TheRapture Oct 14 '21 at 15:34
  • Right now the current code makes the 0920102907_001 folders move but not the parent. – TheRapture Oct 14 '21 at 15:47
  • If your target objects are folders, then add switch `-Directory` to the `Get-ChildItem` cmdlet, otherwise it will also return files. – Theo Oct 14 '21 at 18:43
  • Thank you for the suggestion. I tried that but it still only moved the 10 characters followed by an _ and then and other 3 characters folders. Not the 9ce8eab8-cefe-4ec9-9158-ff40612d5c47 folders. – TheRapture Oct 14 '21 at 19:16
  • Kept it as: Get-ChildItem -Directory -Path "\\Networkdrive\Folder1\PROD- TEST\Successful" -Recurse | – TheRapture Oct 14 '21 at 19:16

2 Answers2

1

Although not really clear what the subfolder naming convention is (you talk about numbers), so below I'm using this pattern: \d{10}_\d{3}.
If however these are HEX digits, you need to use pattern [0-9a-f]{10}_[0-9a-f]{3}

Try:

$sourcePath    = '\\Networkdrive\Folder1\PROD- TEST\Successful'
$destination   = '\\Networkdrive\Folder1\Archive- TEST\Archive_PROD_TEST\Successful'
$folderPattern = '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
$subdirPattern = '\d{10}_\d{3}'  # if the name must match FULLY, use anchors: '^\d{10}_\d{3}$'

Get-ChildItem -Path $sourcePath -Directory | Where-Object {$_.Name -match $folderPattern} | ForEach-Object {
    # capture the folders FullName for when we hit the catch block in case of error
    $targetFolder = $_.FullName
    if (@(Get-ChildItem -Path $targetFolder -Directory | Where-Object { $_.Name -match $subdirPattern }).Count) {
        # one or more subfolders found with name matching the $subdirPattern, so move the folder
        Write-Host "Moving folder '$targetFolder'"
        try {
            Move-Item -Path $targetFolder -Destination $destination -Force -ErrorAction Stop
        }
        catch {
            Write-Warning "Error moving folder '$targetFolder':`r`n$($_.Exception.Message)"
        }
    }
}
Theo
  • 57,719
  • 8
  • 24
  • 41
  • Folder naming convention is: a bunch of folders that look like: cefe-4ec9-9158-ff40612d5c47 ---- then under that is sometimes folders with jpgs ect in them that have the XXXXXXXXX_XXX – TheRapture Oct 14 '21 at 19:49
  • @TheRapture huh?? Your question definitively states (4 times) that foldernames look like `9ce8eab8-cefe-4ec9-9158-ff40612d5c47`, not as you now say `cefe-4ec9-9158-ff40612d5c47` What happened to the first 8 hex characters and the hyphen? – Theo Oct 14 '21 at 19:52
  • Wow!! I got a lot more to learn! Your code performed exactly output was it found the three folders with the XXXXXXXXX_XXX out of the thousands of folders and moved them. Thank you! I will look up a good powershell course as I got more to learn :) Thank you again! – TheRapture Oct 14 '21 at 19:53
  • Yeah They do follow the pattern. Sorry. Misread – TheRapture Oct 14 '21 at 19:54
  • @TheRapture I'm glad to hear that it worked for you! – Theo Oct 14 '21 at 19:56
0

Assuming that the folders named XXXXXXXXX_XXX are the immediate children of the folders named XXXXXXX-XXXX-XXXX-XXXXXXXXXXXX, then you can:

  1. find the folders that match your desired pattern, and
  2. get the Parent property on these objects.

For example, I would use this:

Get-ChildItem -Path "\\Networkdrive\Folder1\PROD-TEST\Successful\*\*" -Directory |
Where-Object name -match '\w{10}_\w{3}' |
Get-ItemPropertyValue -Name Parent |
Move-Item -Destination "\\Networkdrive\Folder1\Archive- TEST\Archive_PROD_TEST\Successful" -Verbose

Note that this simple strategy works only if XXXXXXXXXX_XXX directories are always at the same depth relative to the folders that needs to be moved, because Parent get's you exactly one level.

James
  • 4,211
  • 1
  • 18
  • 34
  • Thank you for the suggestion. I tried the above approach but got: Cannot find path 'C:\Users\me\Desktop\2f5c52ee-daf7-44d5-a4ab-f30149456753' -- Maybe it is looking for a folder with the name? – TheRapture Oct 14 '21 at 19:37
  • More of the message says: + CategoryInfo : ObjectNotFound: (C:\Users\me...ab-f30149456753:String) [Move-Item], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.MoveItemCommand – TheRapture Oct 14 '21 at 19:37
  • Also yes the XXXXXXXXX_XXX is an immediate child of the folders named XXXXXXX-XXXX-XXXX-XXXXXXXXXXXX – TheRapture Oct 14 '21 at 19:38
  • The tag on your question mention PowerShell 3.0... Is this exact? That's a pretty old release... Also, what is the output if you remove the `Move-Item` part in the previous snippet? – James Oct 14 '21 at 19:40
  • Sorry I am new here (and to powershell) and selected tags that seem to best fit. My version with command: Get-Host | Select-Object Version -- is 5.1.14393.4583 -- If I remove the move item from my script I just says -destination is not recognized as a valid cmdlet. Thank you for your help!! – TheRapture Oct 14 '21 at 19:48
  • No worry, and by the way, welcome on StackOverflow. I fixed the tag for you. As for removing `Move-Item`, I really meant removing it as well as everything after it, and the pipe just before it. – James Oct 14 '21 at 19:53
  • Still, at this point, I would suggest that you give a try to Theo's proposal, which is more robust. – James Oct 14 '21 at 19:54