Copying folders to empty folders of a certain name

Welcome Forums General PowerShell Q&A Copying folders to empty folders of a certain name

This topic contains 6 replies, has 3 voices, and was last updated by

 
Participant
2 weeks, 4 days ago.

  • Author
    Posts
  • #179001

    Participant
    Topics: 2
    Replies: 5
    Points: 38
    Rank: Member

    Hello. I am trying to look for folders with "Admin" at the end of their name. If they have no folders in them I want PowerShell to copy a group of folders and their subfolders and put them in said folder. Seems like it should be fairly straight forward but it seems to not work, I don't get an error but when I go back to check the folders they are still empty. Can anyone tell me what I am doing wrong? Thank you for the help

    $EmptyFolders = Get-ChildItem -include *Admin, -recurse -directory 
    
    ForEach ($item in $EmptyFolders) 
    
    {
    if ($_.GetDirectories().count -eq 0) 
    {
    Copy-Item -path "Z:\TECH\(1) PROJECTS & PORTFOLIOS\UPDATED Example\4. Admin","Z:\TECH\(1) PROJECTS & PORTFOLIOS\UPDATED Example\ItemsToCheck",
    "Z:\TECH\(1) PROJECTS & PORTFOLIOS\UPDATED Example\1. Customers","Z:\TECH\(1) PROJECTS & PORTFOLIOS\UPDATED Example\2. Outputs",
    "Z:\TECH\(1) PROJECTS & PORTFOLIOS\UPDATED Example\3. Teams" -Recurse -Destination $_.FullName -Confirm}}
  • #179073

    Participant
    Topics: 8
    Replies: 1213
    Points: 756
    Helping Hand
    Rank: Major Contributor

    $_.FullName should be $item.FullName

    • #179337

      Participant
      Topics: 2
      Replies: 5
      Points: 38
      Rank: Member

      Thank you for the assist. Still looks like it's doing the same thing, not moving the folders. I apologize, there are details that may have been helpful to put in. 1. Is I don't have admin privileges, the Set-ExecutionPolicy -Scope -CurrentUser even gives me an error. 2. The files are on a shared drive. These may not be relevant but thought I should put them in anyways.

  • #179475

    Participant
    Topics: 8
    Replies: 1213
    Points: 756
    Helping Hand
    Rank: Major Contributor

    A couple of things to try. You should not need to change Powershell execution privileges unless you save the file as a PS1, but you can test in ISE. I would start by just highlighting and executing the first 2 lines. The emptyFolders doesn't have a path, so you have to be in the current directory to return the folders, but if you are not, then it would be NULL:

    PS C:\Users\rasim> $emptyFolders = Get-ChildItem -include *Admin, -Recurse -Directory
    
    PS C:\Users\rasim> $emptyFolders
    
    PS C:\Users\rasim> 
    

    If it's NULL, you are not processing anything. It's also better if you get the items you want to copy to ensure you are copying what you expect. Then you should add some debug lines to make sure the script is processing as expected and nothing is null, like your directory count because it will never run that block.

    $emptyFolders = Get-ChildItem -include *Admin, -Recurse -Directory
    $templateFolders = Get-ChildItem "Z:\TECH\(1) PROJECTS & PORTFOLIOS\UPDATED Example\*"
    
    foreach ($folder in $EmptyFolders) {
    
        'Processing directory {0} with {1} directories' -f $folder.FullName, $folder.GetDirectories().count
    
        if ($folder.GetDirectories().count -eq 0) {
            'Found empty directory {0}, beginning copy operations' -f $folder.FullName
            $templateFolders | Copy-Item -Recurse -Destination $folder.FullName -Confirm
        }
    
    }
    
    • #179895

      Participant
      Topics: 2
      Replies: 5
      Points: 38
      Rank: Member

      Thank you sir, this code works as intended as well. Had 2 question for you just for my knowledge:

      1. These two lines"

       'Processing directory {0} with {1} directories' -f $folder.FullName, $folder.GetDirectories().count

      and

      'Found empty directory {0}, beginning copy operations' -f $folder.FullName

      Still trying to understand how code is formatted. Are these just informative comments?

      2. Also the 260 character limit for path names in windows... I assume there's no simple way around that with PowerShell?
      Thank you again for the help.

  • #179550

    Participant
    Topics: 4
    Replies: 76
    Points: 91
    Helping Hand
    Rank: Member

    This one is interesting. I can come up at least one thing to solve in my daily-ish work.

    $folders = Get-ChildItem -Recurse -Directory c:\temp | where {$_.name -like "*admin"} | select fullname,@{N='SubDirectories';E={[boolean]($_ | get-childitem -directory).count}}
    $folders
    $folders | where {-not $_.subdirectories} | foreach {Copy-Item C:\temp\fwnet\ $_.fullname -verbose}
    

    Of course you can make it even a one liner, but I prefer to keep it more readable and split the stuff to multiple lines.

    • #179880

      Participant
      Topics: 2
      Replies: 5
      Points: 38
      Rank: Member

      Thank you. It worked. One reason I may have not been getting all files was because the path names were too long. When I ran your code I got:

      Copy-Item : The specified path, file name, or both are too long. The fully qualified file name must be less than 260 
      characters, and the directory name must be less than 248 characters.

      Your code works though from what I see I will just have to minimize our folder structure in the future, thank you again.

You must be logged in to reply to this topic.