Mkdir or folder to all directories which have a folder called "copyhere"

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Bjørn Olav Vangen Aure Bjørn Olav Vangen Aure 1 year, 3 months ago.

  • Author
  • #27036
    Profile photo of Mel VW
    Mel VW

    I have a requirement.

    I have to create a folder called test001. The issues is this:
    – It is on the C Drive
    – Folder is C:\folder001
    – However inside folder001, there are subfolders:

    Within each of these, there is the folder called "copyhere". This is where I want the folder, test001 to be created.

    How can I do this? Is this possible with mkdir?

    PLEASE NOTE: If it exits, then dont create it.

  • #27041

    This should do the trick, worked for me when i tested.
    I guess it could be done with mkdir but i like to use only powershell commandlets in my scripts if possible.

    New-Item -ItemType Directory -Path C:\newfolder works as well as mkdir

    Get-ChildItem .\folder001 -Recurse | Where-Object {$_.PsIsContainer} | % {
        If($_.Name -eq "copyhere"){
            If((Test-Path "$($_.FullName)\test001") -eq $false) {
                New-Item -ItemType Directory -Force -Path "$($_.FullName)\test001"
  • #27079
    Profile photo of Nathan Hartley
    Nathan Hartley

    Could use wild cards with Get-ChildItem to find the folders you want to work with:

    Get-ChildItem C:\folder001\???\copyhere | Foreach { New-Item -Path (Join-Path $_ test001) -ItemType Directory -Force }

    I tested this after creating your directory structure this way:

    $paths = 1..4 | Foreach { 'C:\folder001\{0:000}\copyhere' -f $_ }
    New-Item -Path $paths -ItemType Directory -Force
  • #27085

    That's a much nicer solution there Nathan 🙂

You must be logged in to reply to this topic.