Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista Tutorial - get-child item question from a newbie

Reply
 
Old 08-28-2007   #1 (permalink)
powersheller
Guest


 
 

get-child item question from a newbie

Ok, this is my first day working with powershell. I'm sure this
question couldn't be easier, but I can't seem to figure it out.

All I want to do is run a script which would accomplish the following.

run a command in each sub directory

here is what I would do at a command line.
c:\somedir> cd\subdir
c:\somedir\subdir>
c:\somedir\subdir>somecommand.exe
c:\somedir\subdir>cd..
c:\somedir\>cd\subdir2
c:\somedir\subdir2>somecommand.exe

etc. it's probably very easy, but I can't figure out how to pass the
get-child item result to set-location


any suggestions would be greatly appreciated.

ej


My System SpecsSystem Spec
Old 08-28-2007   #2 (permalink)
Marco Shaw
Guest


 
 

Re: get-child item question from a newbie

powersheller@xxxxxx wrote:
Quote:

> Ok, this is my first day working with powershell. I'm sure this
> question couldn't be easier, but I can't seem to figure it out.
>
> All I want to do is run a script which would accomplish the following.
>
> run a command in each sub directory
>
> here is what I would do at a command line.
> c:\somedir> cd\subdir
> c:\somedir\subdir>
> c:\somedir\subdir>somecommand.exe
> c:\somedir\subdir>cd..
> c:\somedir\>cd\subdir2
> c:\somedir\subdir2>somecommand.exe
>
> etc. it's probably very easy, but I can't figure out how to pass the
> get-child item result to set-location
>
>
> any suggestions would be greatly appreciated.
>
> ej
>
Welcome!

get-item
*|foreach-object{if($_.psiscontainer){$_}}|foreach-object{set-location
$_;$pwd}

Here "$pwd" is my command I'm running in the current dir. Replace with
your own...

Marco


--
----------------
PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
My System SpecsSystem Spec
Old 08-28-2007   #3 (permalink)
Keith Hill [MVP]
Guest


 
 

Re: get-child item question from a newbie

"Marco Shaw" <marco.shaw@_NO_SPAM_gmail.com> wrote in message
news:#rTB#Qd6HHA.3264@xxxxxx
Quote:

> powersheller@xxxxxx wrote:
>
> Welcome!
>
> get-item
> *|foreach-object{if($_.psiscontainer){$_}}|foreach-object{set-location
> $_;$pwd}
As you'll soon discover with PowerShell, there are usually quite a few ways
to accomplish a task:

get-childitem -recurse | where {$_.PSIsContainer} | set-location -passthru |
foreach {du $_.providerpath}

or the shortened (aliased) version:

gci -r | ?{$_.PSIsContainer} | set-location -pas | %{du.exe $_.providerpath}

The thing to note here is that you should consider using "get-member" at the
various stages of the pipeline to see what kind of object you get. For
instance:

gci -r | gm

will show you DirectoryInfo and FileInfo objects.

gci -r | ?{$_.PSIsContainer} | gm

will show just DirectoryInfo objects because the FileInfo objects get
filtered out.

gci -r | ?{$_.PSIsContainer} | set-location -pas | gm

shows that set-location -passthru converts the DirectoryInfo object into a
PathInfo object with a property called ProviderPath that you will want to
pass to legacy apps. If you want to see the property values of these
PathInfo objects try this:

gci -r | ?{$_.PSIsContainer} | set-location -pas | format-list *

--
Keith

My System SpecsSystem Spec
Old 08-29-2007   #4 (permalink)
Brandon Shell
Guest


 
 

Re: get-child item question from a newbie

I read your question as you just want to run some specific EXE's in
subfolders... if that is this case. These will help.

If your intent is to run a specific list of exe's in each subfolder you and
do this.
$include = "somecommand.exe","somecommand2.exe"
get-childitems c:\somedir -include $include -recursive | %{invoke-express
$_.fullname}

Info: Get-Childitem has an include parameter that allows you match a string
or an array of string. In this case an array of strings that contains
filename of the EXE(s)

If you just want to run all of them
get-childitems c:\somedir -filter *.exe -recursive | %{invoke-express
$_.fullname}

Info: Get-Childitem has a Filte parameter that lets you filter the result
set by a specific string. In this case *.exe

NOTE: You dont have to be in the folder of the EXE to execute. It is just
like cmd prompt in that aspect. As long as you qualify the path you are
good.

For more info
PS> get-help Get-Childitem -full

<powersheller@xxxxxx> wrote in message
news:1188345673.374024.311640@xxxxxx
Quote:

> Ok, this is my first day working with powershell. I'm sure this
> question couldn't be easier, but I can't seem to figure it out.
>
> All I want to do is run a script which would accomplish the following.
>
> run a command in each sub directory
>
> here is what I would do at a command line.
> c:\somedir> cd\subdir
> c:\somedir\subdir>
> c:\somedir\subdir>somecommand.exe
> c:\somedir\subdir>cd..
> c:\somedir\>cd\subdir2
> c:\somedir\subdir2>somecommand.exe
>
> etc. it's probably very easy, but I can't figure out how to pass the
> get-child item result to set-location
>
>
> any suggestions would be greatly appreciated.
>
> ej
>
My System SpecsSystem Spec
Old 08-29-2007   #5 (permalink)
powersheller
Guest


 
 

Re: get-child item question from a newbie

On Aug 29, 8:19 am, "Brandon Shell" <tshell.m...@xxxxxx> wrote:
Quote:

> I read your question as you just want to run some specific EXE's in
> subfolders... if that is this case. These will help.
>
> If your intent is to run a specific list of exe's in each subfolder you and
> do this.
> $include = "somecommand.exe","somecommand2.exe"
> get-childitems c:\somedir -include $include -recursive | %{invoke-express
> $_.fullname}
>
> Info: Get-Childitem has an include parameter that allows you match a string
> or an array of string. In this case an array of strings that contains
> filename of the EXE(s)
>
> If you just want to run all of them
> get-childitems c:\somedir -filter *.exe -recursive | %{invoke-express
> $_.fullname}
>
> Info: Get-Childitem has a Filte parameter that lets you filter the result
> set by a specific string. In this case *.exe
>
> NOTE: You dont have to be in the folder of the EXE to execute. It is just
> like cmd prompt in that aspect. As long as you qualify the path you are
> good.
>
> For more info
> PS> get-help Get-Childitem -full
>
> <powershel...@xxxxxx> wrote in message
>
> news:1188345673.374024.311640@xxxxxx
>
>
>
Quote:

> > Ok, this is my first day working with powershell. I'm sure this
> > question couldn't be easier, but I can't seem to figure it out.
>
Quote:

> > All I want to do is run a script which would accomplish the following.
>
Quote:

> > run a command in each sub directory
>
Quote:

> > here is what I would do at a command line.
> > c:\somedir> cd\subdir
> > c:\somedir\subdir>
> > c:\somedir\subdir>somecommand.exe
> > c:\somedir\subdir>cd..
> > c:\somedir\>cd\subdir2
> > c:\somedir\subdir2>somecommand.exe
>
Quote:

> > etc. it's probably very easy, but I can't figure out how to pass the
> > get-child item result to set-location
>
Quote:

> > any suggestions would be greatly appreciated.
>
Quote:

> > ej- Hide quoted text -
>
> - Show quoted text -
First of all, I want to thank everyone that has offered suggestions.
I'm still a bit stumped on this.
as far as I can tell the solutions don't address the fact that I just
want to step one level deep from the current dir and run a specific
command (in this case it's actually a script that must be run from
current dir, I can't just run it from a different dir with the correct
path.) and then I need to go back up one dir and then back down into
the next dir and run the same command.
I don't think I need -recurse because I don't want to go deeper than
the one level.
I know this is probably really easy, I just don't see it. I'm spinning
my wheels here on something I know shouldn't take more than a minute
or two.

thanks in advance.

My System SpecsSystem Spec
Old 08-29-2007   #6 (permalink)
Keith Hill [MVP]
Guest


 
 

Re: get-child item question from a newbie

<powersheller@xxxxxx> wrote in message
news:1188399013.650799.260310@xxxxxx
Quote:

> First of all, I want to thank everyone that has offered suggestions.
> I'm still a bit stumped on this.
> as far as I can tell the solutions don't address the fact that I just
> want to step one level deep from the current dir and run a specific
> command (in this case it's actually a script that must be run from
> current dir, I can't just run it from a different dir with the correct
> path.) and then I need to go back up one dir and then back down into
> the next dir and run the same command.
> I don't think I need -recurse because I don't want to go deeper than
> the one level.
Missed that part - sorry. Then this should work for you:

get-item * | where {$_.PSIsContainer} | set-location -passthru | foreach {
c:\myscript.ps1}

get-item * will get the items (files/folders) in the current directory and
pass them to the where filter which filters out the files.

--
Keith

My System SpecsSystem Spec
Old 08-29-2007   #7 (permalink)
Marco Shaw
Guest


 
 

Re: get-child item question from a newbie

Quote:

> First of all, I want to thank everyone that has offered suggestions.
> I'm still a bit stumped on this.
> as far as I can tell the solutions don't address the fact that I just
> want to step one level deep from the current dir and run a specific
> command (in this case it's actually a script that must be run from
> current dir, I can't just run it from a different dir with the correct
> path.) and then I need to go back up one dir and then back down into
> the next dir and run the same command.
> I don't think I need -recurse because I don't want to go deeper than
> the one level.
> I know this is probably really easy, I just don't see it. I'm spinning
> my wheels here on something I know shouldn't take more than a minute
> or two.
You *specifically* need to 'cd ..', and 'cd new_dir'? May we ask why?
The solutions put you *in* the dir, but might not cd in and out.

You don't need -recurse like you mentioned.

This is the learning curve... Enjoy the ride!

Marco

--
----------------
PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com
My System SpecsSystem Spec
Old 08-29-2007   #8 (permalink)
powersheller
Guest


 
 

Re: get-child item question from a newbie

On Aug 29, 11:00 am, Marco Shaw <marco.shaw@_NO_SPAM_gmail.com> wrote:
Quote:
Quote:

> > First of all, I want to thank everyone that has offered suggestions.
> > I'm still a bit stumped on this.
> > as far as I can tell the solutions don't address the fact that I just
> > want to step one level deep from the current dir and run a specific
> > command (in this case it's actually a script that must be run from
> > current dir, I can't just run it from a different dir with the correct
> > path.) and then I need to go back up one dir and then back down into
> > the next dir and run the same command.
> > I don't think I need -recurse because I don't want to go deeper than
> > the one level.
> > I know this is probably really easy, I just don't see it. I'm spinning
> > my wheels here on something I know shouldn't take more than a minute
> > or two.
>
> You *specifically* need to 'cd ..', and 'cd new_dir'? May we ask why?
> The solutions put you *in* the dir, but might not cd in and out.
>
> You don't need -recurse like you mentioned.
>
> This is the learning curve... Enjoy the ride!
>
> Marco
>
> --
> ----------------
> PowerGadgets MVPhttp://www.powergadgets.com/mvp
>
> Blog:http://marcoshaw.blogspot.com
Hi Marco, Of course you can ask why I want to do it this way.

I am kicking off some complex scripts that take anywhere from a few
seconds to several hours to run. Due to the way the scripts run, they
need to be launched from the actual directory (I know, strange, but I
was asked to work with them, not change them) So I just need to be in
the actual directory before I launch it. then once the job finishes, I
move up one dir, go into the next dir and kick off the next script. I
could very easily have created a bat file that just went thru the
steps I listed above for each dir, but I really want to start using
powershell and thought this would be a perfectly simple thing to do. I
know once I see the solution I'll be kicking myself for even asking
this question, but right now i just don't see how to accomplish this

My System SpecsSystem Spec
Old 08-29-2007   #9 (permalink)
Kiron
Guest


 
 

Re: get-child item question from a newbie

Extending Keith's solution to change dir to each subDir, execute multiple
scripts from there and change dir back to the parent afterwards in each
loop.

$scriptsPath = "C:\Scripts"
# scripts to execute
$scripts = 'myScript1', 'myScript2', 'myScript3'
# filter first level subDirs
get-item * | where {$_.PSIsContainer} |
# push the current location to the default stack and cd to the next subDir
foreach {push-location $($_.fullName)
# send the script names down the pipe
$scripts |
# use the call operator to execute each script
foreach {& "$scriptPath\$_"}
# pop the parent's location from the stack and cd back to it
pop-location
}

--
Kiron

My System SpecsSystem Spec
Old 08-29-2007   #10 (permalink)
Kiron
Guest


 
 

Re: get-child item question from a newbie

Typo:
foreach {& "$scriptPath\$_"}

....should be:

foreach {& "$scriptsPath\$_"}

--
Kiron
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
how to show the name of child item after its in the pipeline? PowerShell
Re: Newbie Question Live Mail
Newbie Question #3 General Discussion
RE: Newbie Question PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46