Powershellcommand for jumping to next Folder

Viewed 64

Is there a command in Powershell to jump to the next folder? My problem is that Powershell should search a folder with several subfolders for a particular file and when it is found moves it. Example:

C:\Test\1\error\test.data

This should be moved to

C:\Test\1\data

and then I go back with cd. . cd. .

C:\Test\

Here I would need a command that jumg automatically in

C:\Test\2\error

And here’s my problem, how do I automatically get to the next folder without specifying it. So that I insert the command into a loop?

Thanks for the help :)

1 Answers

Use Get-ChildItem to enumerate the directories nested under C:\Test then use a loop to go through them one by one:

$targetFolders = Get-ChildItem -Path C:\test -Directory

foreach($targetFolder in $targetFolders){
  # Switch to target folder
  Push-Location -LiteralPath $targetFolder.FullName

  # Check if file is there
  if(Test-Path error\test.data){
    # If so, move it
    Move-Item error\test.data -Destination data
  }

  # Leave directory again
  Pop-Location
}
Related