Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run command in each sub directory using PowerShell

Tags:

powershell

I want to run mvn clean compile in each directory in my Eclipse workspace containing a pom.xml file.

I was able to run svn update for each directory containing .svn like this:

PS C:\workspace> Get-ChildItem -recurse -force | 
where {$_.name -eq ".svn"} | 
foreach {svn update $_.parent.FullName}

However, this gives the full path to svn and runs from the current directory. The mvn clean compile needs to be run inside the directory, afaik. Tried doing the following, but then I just get an error saying something can't be null. Seems to be the first cd call it complains about. Do I just have some syntax wrong, or am I using foreach wrong?

PS C:\workspace> Get-ChildItem -recurse | 
where {$_.name -eq "pom.xml"} | 
foreach { cd $_.parent.name; mvn clean compile; cd ..; }
like image 453
Svish Avatar asked Nov 06 '12 10:11

Svish


People also ask

How do I get a list of files in a directory and subfolders using PowerShell?

If you want to list files and directories of a specific directory, utilize the “-Path” parameter in the “Get-ChildItem” command. This option will help PowerShell list all the child items of the specified directory. The “-Path” parameter is also utilized to set the paths of one or more locations of files.

How do I loop a PowerShell script?

The While statement in PowerShell is used to create a loop that runs a command or a set of commands if the condition evaluates to true. It checks the condition before executing the script block. As long as the condition is true, PowerShell will execute the script block until the condition results in false.

Can I use xcopy in PowerShell?

xcopy is the windows command. It works with both PowerShell and cmd as well because it is a system32 utility command.


1 Answers

Seems like $_.parent.FullName property worked in the first case because I was filtering out only directories. In this case I filtered out files, so I got it working by using $_.DirectoryName instead.

PS C:\workspace> Get-ChildItem -recurse | 
where {$_.name -eq "pom.xml"} | 
foreach { cd $_.DirectoryName; mvn clean compile; cd ..; }
like image 162
Svish Avatar answered Oct 18 '22 16:10

Svish