1

When defining the path I want to do numerous things, including skip if the folder already exists this is my attempt so far:

Get-ChildItem -Path $featureDirectory -Recurse | Where-Object {$_.PSIsContainer -eq $true} | 
    New-Item -ItemType Directory -Path {
        Write-Host "Checking if directory needed for " $_.Name
        # checking to see path is part of Copy To directory
        if($copyTo -Match $_.Name)
        {
            # checking if directory already exists
            $alreadyExists = Test-Path $copyTo
            if($alreadyExists)
            {
                Continue
            }
            Write-Host "Creating full directory path for directory for: " $copyTo
            $copyTo
        }else{
            Write-Host "Creating directory for: " $copyTo $_.Name
            Join-Path $copyTo $_.Name 
        }

    } -Force

However Continue breaks me out of the loop entirely. I guess it's not a true loop was wondering if there is a better way to achieve the above or not?

Second Attempt - Breaking it up

foreach ($directory in $directories)
{
    if($copyTo -match $directory.Name)
    {
        # checking if directory already exists
        $alreadyExists = Test-Path $copyTo
        if($alreadyExists)
        {
            continue
        }else{
            New-Item -ItemType Directory -Path $copyTo -Force
        }           
    }else{
        $path = Join-Path $copyTo $directory.Name 
        New-Item -ItemType Directory -Path $path -Force
    }
}
Anicho
  • 2,647
  • 11
  • 48
  • 76
  • https://stackoverflow.com/questions/11568221/using-foreach-with-get-childitem-recurse maybe split your command in two parts. – Ben H Jul 28 '17 at 11:24
  • I don't understand why you need the statement at all, can't you just do `if (!$alreadyexists) {...}`? – arco444 Jul 28 '17 at 11:24
  • with continue you can skip the last part and jump just to the start (skip the item) – Ben H Jul 28 '17 at 11:26
  • @BenH if you're already inside the first if and you only do the write-host if the file does *not* exist, you will skip back to the first part anyway because the else won't trigger – arco444 Jul 28 '17 at 11:28
  • You appear to be suffering from pipelineitis -- a condition where you want to stuff everything in a pipeline, even where a loop would be a better choice. You *are* allowed to use the `foreach` statement. – Jeroen Mostert Jul 28 '17 at 11:30
  • @arco444 yes I can but the result is no argument provided to -Path in that scenario – Anicho Jul 28 '17 at 11:30
  • @arco444 this is correct, just the "creating full..." will be skipped if(alreadyexists) – Ben H Jul 28 '17 at 11:31
  • @JeroenMostert forgive me I am new to scripting fully in Powershell I am using link Ben H provided – Anicho Jul 28 '17 at 11:31
  • Updated question, I get same result doing the second part – Anicho Jul 28 '17 at 11:38
  • Where do you set `$copyTo`? – arco444 Jul 28 '17 at 11:44
  • `C:/Test` @arco444 your answer is right. I just need to replace $_. with $directory. in my second example – Anicho Jul 28 '17 at 11:46
  • @arco444 If you write answer I will accept :) Thanks for your help all – Anicho Jul 28 '17 at 11:46

1 Answers1

0

I fail to understand why you need a continue in the scenario you've given. Taking your second part, you could just do:

foreach ($directory in $directories)
{
    if($copyTo -match $directory.Name)
    {
        # checking if directory already exists
        $alreadyExists = Test-Path $copyTo
        if(!$alreadyExists){
          New-Item -ItemType Directory -Path $copyTo -Force
        }
    }else{
        $path = Join-Path $copyTo $directory.Name 
        New-Item -ItemType Directory -Path $path -Force
    }
}
Anicho
  • 2,647
  • 11
  • 48
  • 76
arco444
  • 22,002
  • 12
  • 63
  • 67