TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > Tech Support Archives > Microsoft > Microsoft Windows Powershell

Looping through each file in a directory: Powershell noob question

Microsoft Windows Powershell

 
 
Thread Tools Display Modes
Unread 15-05-2008, 11:24 PM   #1
fr0man
Guest
 
Posts: n/a
Looping through each file in a directory: Powershell noob question

I've been using Powershell for a few months now as my command line interface
and I'm loving it. But I haven't actually scripted anything yet and don't
even know where to begin.
I need a simple powershell script to loop through each file in a directory
and execute a command with that filename as one parameter, and with the
filename with a new extension as a different parameter. Like:
foreach filename.yxdb in dir
RunThisCommand.exe "<filename.yxdb SomeOtherStuff filename.csv>"
next

My google searches aren't really turning anything up for this. It seems
pretty basic.
Thanks in advance.
 
Unread 16-05-2008, 01:31 AM   #2
Marco Shaw [MVP]
Guest
 
Posts: n/a
Re: Looping through each file in a directory: Powershell noob question

fr0man wrote:
> I've been using Powershell for a few months now as my command line interface
> and I'm loving it. But I haven't actually scripted anything yet and don't
> even know where to begin.
> I need a simple powershell script to loop through each file in a directory
> and execute a command with that filename as one parameter, and with the
> filename with a new extension as a different parameter. Like:
> foreach filename.yxdb in dir
> RunThisCommand.exe "<filename.yxdb SomeOtherStuff filename.csv>"
> next
>
> My google searches aren't really turning anything up for this. It seems
> pretty basic.
> Thanks in advance.


Interactively, this would look something like:

PSH>$files=get-childitem .
PSH>foreach ($file in $files) {
>> write-host "run.exe `"<$file something filename.csv>`""
>> }
>>


There's some problems with the above though, depending on what you have
in the current directory or whether you want to do this recursively.
Most the changes are just relating to the first line where you get your
files.

# Get me just *.yxdb files
$files=get-childitem . *.yxdb|where-object {!($_.psiscontainer)}

# Do the above recursively
$files=get-childitem . *.yxdb -rec|where-object {!($_.psiscontainer)}

So I've used write-host to demonstrate.

Also, if you start going through directories recursively, you may need
to use "$file.fullname" versus just "$file".

Now as for creating scripts, you just put the above commands into a .ps1
file and you're off...

Good luck!

Marco



--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

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

Blog:
http://marcoshaw.blogspot.com
 
Unread 16-05-2008, 01:32 AM   #3
Kiron
Guest
 
Posts: n/a
Re: Looping through each file in a directory: Powershell noob question

# -< script.ps1 >-
# 1
param ($extension = $(throw '-Extension can''t be null'),
$newExt = 'csv')

# 2
get-childItem . *.$extension |
# 3
where-object {!$_.psIsContainer} |
# 4
foreach {
# 5
# v1
$newName = $_ -replace '(.+)\..*$', ('$1.'+ $newExt)
# v2
# $newName = $_.baseName + '.' + $newExt
# 6
"RunThisCommand.exe `"<$_ SomeOtherStuff $newName>`""
}
# -< script.ps1 >-

Save the above in a PS1 file and call it like:
./script yxdb

1. Define the script's parameters:
-Extension to filter files on. By assigning a Throw statement to it by
default, the parameter becomes mandatory
-NewExt is set to 'csv' by default

2. Get items with extension $extension from the current location
3. Filter files only --not necessary, just in case there is a subDir
with same extension. PSIsContainer returns a [bool], by negating it
with the -not operator --or its alias '!'-- only files go through
the filter.
4. Process items.
5. The $_ is an automatic variable that contains the current pipeline
object. When an [IO.FileInfo] object is cast as [String] it returns
the file's name and extension. In v1 there isn't a property that has
the file name without the extension. The -replace operator captures
just the name of the file and assigns it to $1. Then the $newExt is
appended to '$1.' and the new name is assigned to $newName.
If your using v2 CTP, the name of the file without the extension is
available in the BaseName property.
6. This would be the commandline to execute on each file. I've double
quoted it to expand the variables in it to demonstrate the result.

--
Kiron
 
Unread 16-05-2008, 03:28 PM   #4
fr0man
Guest
 
Posts: n/a
Re: Looping through each file in a directory: Powershell noob ques

Awesome! Both of these help a ton. I'm glad I didn't attempt this with
Python, which was choice #2 and about which I know even less.



"Kiron" wrote:

> # -< script.ps1 >-
> # 1
> param ($extension = $(throw '-Extension can''t be null'),
> $newExt = 'csv')
>
> # 2
> get-childItem . *.$extension |
> # 3
> where-object {!$_.psIsContainer} |
> # 4
> foreach {
> # 5
> # v1
> $newName = $_ -replace '(.+)\..*$', ('$1.'+ $newExt)
> # v2
> # $newName = $_.baseName + '.' + $newExt
> # 6
> "RunThisCommand.exe `"<$_ SomeOtherStuff $newName>`""
> }
> # -< script.ps1 >-
>
> Save the above in a PS1 file and call it like:
> ./script yxdb
>
> 1. Define the script's parameters:
> -Extension to filter files on. By assigning a Throw statement to it by
> default, the parameter becomes mandatory
> -NewExt is set to 'csv' by default
>
> 2. Get items with extension $extension from the current location
> 3. Filter files only --not necessary, just in case there is a subDir
> with same extension. PSIsContainer returns a [bool], by negating it
> with the -not operator --or its alias '!'-- only files go through
> the filter.
> 4. Process items.
> 5. The $_ is an automatic variable that contains the current pipeline
> object. When an [IO.FileInfo] object is cast as [String] it returns
> the file's name and extension. In v1 there isn't a property that has
> the file name without the extension. The -replace operator captures
> just the name of the file and assigns it to $1. Then the $newExt is
> appended to '$1.' and the new name is assigned to $newName.
> If your using v2 CTP, the name of the file without the extension is
> available in the BaseName property.
> 6. This would be the commandline to execute on each file. I've double
> quoted it to expand the variables in it to demonstrate the result.
>
> --
> Kiron
>

 
Unread 17-05-2008, 06:15 AM   #5
Leo Tohill
Guest
 
Posts: n/a
Re: Looping through each file in a directory: Powershell noob ques

You might prefer to use the .net "Path.ChangeExtension" method. Instead of

$newName = $_ -replace '(.+)\..*$', ('$1.'+ $newExt)

you would code

$newName = [System.IO.Path]::ChangeExtension($_, $newExt)

"ChangeExtension" is a static method, requiring the [classname]::methodname
syntax.


"fr0man" wrote:

> Awesome! Both of these help a ton. I'm glad I didn't attempt this with
> Python, which was choice #2 and about which I know even less.
>
>
>
> "Kiron" wrote:
>
> > # -< script.ps1 >-
> > # 1
> > param ($extension = $(throw '-Extension can''t be null'),
> > $newExt = 'csv')
> >
> > # 2
> > get-childItem . *.$extension |
> > # 3
> > where-object {!$_.psIsContainer} |
> > # 4
> > foreach {
> > # 5
> > # v1
> > $newName = $_ -replace '(.+)\..*$', ('$1.'+ $newExt)
> > # v2
> > # $newName = $_.baseName + '.' + $newExt
> > # 6
> > "RunThisCommand.exe `"<$_ SomeOtherStuff $newName>`""
> > }
> > # -< script.ps1 >-
> >
> > Save the above in a PS1 file and call it like:
> > ./script yxdb
> >
> > 1. Define the script's parameters:
> > -Extension to filter files on. By assigning a Throw statement to it by
> > default, the parameter becomes mandatory
> > -NewExt is set to 'csv' by default
> >
> > 2. Get items with extension $extension from the current location
> > 3. Filter files only --not necessary, just in case there is a subDir
> > with same extension. PSIsContainer returns a [bool], by negating it
> > with the -not operator --or its alias '!'-- only files go through
> > the filter.
> > 4. Process items.
> > 5. The $_ is an automatic variable that contains the current pipeline
> > object. When an [IO.FileInfo] object is cast as [String] it returns
> > the file's name and extension. In v1 there isn't a property that has
> > the file name without the extension. The -replace operator captures
> > just the name of the file and assigns it to $1. Then the $newExt is
> > appended to '$1.' and the new name is assigned to $newName.
> > If your using v2 CTP, the name of the file without the extension is
> > available in the BaseName property.
> > 6. This would be the commandline to execute on each file. I've double
> > quoted it to expand the variables in it to demonstrate the result.
> >
> > --
> > Kiron
> >

 
 

Thread Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT. The time now is 01:43 PM.


vBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2013, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional