Windows Vista Forums

Looping through each file in a directory: Powershell noob question

  1. #1


    fr0man Guest

    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.

      My System SpecsSystem Spec

  2. #2


    Marco Shaw [MVP] Guest

    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

      My System SpecsSystem Spec

Looping through each file in a directory: Powershell noob question

Similar Threads
Thread Forum
noob question
I am having some trouble making a cd from a dvd. I need to slow the song down for dance practice. Can anyone tell me how to do this? I have tried to...
General Discussion
Noob question....sorry
I have just moved from XP to Vista, and I cannot for the life of me find the "back up one level" arrow that I am used to using in Windows Explorer...
General Discussion
Noob Question
I'm new to Powershell as well as scripting (to a degree), and I'm in need of some advice: Here's my code: $sourcedir =...
PowerShell
Looping through directory
I have a directory named USERS which contains about 100 unique user folders. Each user folder contains a folder named RECYCLER and I would like to...
VB Script
Noob question about colours
Hello guys, im new with Vista, and i just want to know where can i change the color of the task or tool bar (the black one in the bottom), cant find...
General Discussion
Question for creating a directory file Using Vista Ultimate..
Hello, I need to create a directory folder. What I need is a main folder with subfolders that pertain to weeks. I'm using Vista Ultimate Ex:...
General Discussion
Noob question!
Ok when it comes to the deep specs on a video card, i'm a noob. Otherwise i'm an advanced computer user, so i'm asking this out of an abundance of...
Graphic cards