Windows Vista Forums

Possible to Read only Selected lines from multiple files into anotherfile deleting source files when processed?
  1. #1


    joe.hurzeler Guest

    Possible to Read only Selected lines from multiple files into anotherfile deleting source files when processed?

    My experience with powershell is measured in hours, but I've got to
    say I'm STOKED at what I've seen so far!
    I've used my so far limited skills in an attempt to do the following
    without success:

    I have a folder containing zero to many files named like *.in their
    contents are roughly the following format:

    Header
    dataline xxxxxxxxxxxxxxxxxxxxxxxxxxUseMexxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    dataline xxxxxxxxxxxxxxxxxxxxxxxxxxUseMexxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    EOF

    I need to read only the data lines from each file into a single file
    (data.out) and delete each .in file as it has been processed.
    I have gotten pretty close filtering by $_.Length -gt 6, but getting
    it into a single output file and then deleting the source file eludes
    me.

    Is this possible and if so how?

    Your help is greatly appreciated.



    Joe

      My System SpecsSystem Spec

  2. #2


    Marco Shaw [MVP] Guest

    Re: Possible to Read only Selected lines from multiple files intoanother file deleting source files when processed?

    > I need to read only the data lines from each file into a single file

    > (data.out) and delete each .in file as it has been processed.
    > I have gotten pretty close filtering by $_.Length -gt 6, but getting
    > it into a single output file and then deleting the source file eludes
    > me.
    >
    > Is this possible and if so how?
    >
    > Your help is greatly appreciated.
    >
    > Joe
    Give this a try (change your paths accordingly):

    get-childitem c:\demo2\tmp -recurse *.in|foreach-object{
    select-string $_ -pattern "^dataline"|foreach-object{
    add-content "c:\demo2\tmp\data.out" $_.tostring().split(":")[3]
    }
    remove-item $_.fullname -whatif
    }

    Welcome to PowerShell! ;-)

    It may take a more than a few hours to get your head around things, but
    its worth the ride!

    --
    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

  3. #3


    joe.hurzeler Guest

    Re: Possible to Read only Selected lines from multiple files intoanother file deleting source files when processed?

    On Mar 9, 6:43*pm, "Marco Shaw [MVP]" <marco.shaw@_NO_SPAM_gmail.com>
    wrote:

    > > I need to read only the data lines from each file into a single file
    > > (data.out) and delete each .in file as it has been processed.
    > > I have gotten pretty close filtering by $_.Length *-gt 6, but getting
    > > it into a single output file and then deleting the source file eludes
    > > me.
    >

    > > Is this possible and if so how?
    >

    > > Your help is greatly appreciated.
    >

    > > Joe
    >
    > Give this a try (change your paths accordingly):
    >
    > get-childitem c:\demo2\tmp -recurse *.in|foreach-object{
    > * *select-string $_ -pattern "^dataline"|foreach-object{
    > * * *add-content "c:\demo2\tmp\data.out" $_.tostring().split(":")[3]
    > * *}
    > * *remove-item $_.fullname -whatif
    >
    > }
    >
    > Welcome to PowerShell! ;-)
    >
    > It may take a more than a few hours to get your head around things, but
    > its worth the ride!
    >
    > --
    > Microsoft MVP - Windows PowerShellhttp://www.microsoft.com/mvp
    >
    > PowerGadgets MVPhttp://www.powergadgets.com/mvp
    >
    > Blog:http://marcoshaw.blogspot.com
    I'm looking forward to the ride! It's been a real eye-opener so far.

    Thanks for your response - intriguing!
    I couldn't get it to actually put the detail line into the file
    data.out until I removed '.split(":")[3]' - what does that do?

    Thanks again for your help,
    Joe

      My System SpecsSystem Spec

  4. #4


    Marco Shaw [MVP] Guest

    Re: Possible to Read only Selected lines from multiple files intoanother file deleting source files when processed?


    > Thanks for your response - intriguing!
    > I couldn't get it to actually put the detail line into the file
    > data.out until I removed '.split(":")[3]' - what does that do?
    Hmmmm... I'm using the v2 CTP so perhaps select-string is doing
    something different for me.

    ..split is a method where the string passed is split-up into elements by
    the character passed to it (":" in this case). "[3]" simply means that
    once the string is split, get me the 4th element (4th because PowerShell
    counts starting at 0 in this case).

    Marco

      My System SpecsSystem Spec

Possible to Read only Selected lines from multiple files into anotherfile deleting source files when processed? problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete all .processed files Stacey VB Script 3 05 May 2009
Remove lines from multiple text files Peter E PowerShell 5 09 Apr 2009
Read multiple files with script Gavin VB Script 2 06 Oct 2008
Retrieving selected lines from all text files in a directory Shawn PowerShell 8 03 Jul 2008
Can One Folder display Files from multiple source folders? Ben604 Vista file management 0 09 Aug 2007