title: PowerShell nested loop for renaming files in directory
body: <p>I am trying to do a nested loop in PowerShell.  I am renaming images in a several sub directories but i need the loop to reset based on it finding a new directory.</p>
  
  <p>Here is the code I wrote but I can't get it to work as it gives me an error saying the file doesn't exist.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function rename-cruise {
      
      Param([string]$parkname, [string]$extension=".jpg")
      
      if(!$parkname){
          $park = $args[0];
      } else {
          $park = $parkname;
      }
  
      $dirs = Get-ChildItem -Recurse | ?{ $_.PSIsContainer };
  
      foreach($dir in $dirs){
          if($dir -cnotlike "177"){
  
             
  
              $i = 0;
              $currentDir = get-location;
              $location = "$currentDir\$dir";
  
               write-host $currentDir
               write-host $location
  
               Get-ChildItem -Path $location -Filter "*$extension" |
               ForEach-Object {
  
                 if($i -lt 10){
  
                      $newName = "$park-0$i$extension";
                      Rename-Item $_.Name -NewName $newName;
              
                 } else {
  
                      $newName = "$park-$i$extension";
                      Rename-Item $_.Name -NewName $newName;
  
                 }
  
                 $i++;
         
              }
  
          }
      }
      
  
  }</code></pre>
  </div>
  </div>
  </p>