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 - Possible to Read only Selected lines from multiple files into anotherfile deleting source files when processed?

Reply
 
Old 03-09-2008   #1 (permalink)
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
Old 03-09-2008   #2 (permalink)
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
Quote:

> (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
Old 03-09-2008   #3 (permalink)
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:
Quote:
Quote:

> > 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.
>
Quote:

> > Is this possible and if so how?
>
Quote:

> > Your help is greatly appreciated.
>
Quote:

> > 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
Old 03-09-2008   #4 (permalink)
Marco Shaw [MVP]
Guest


 
 

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

Quote:

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

Thread Tools


Similar Threads
Thread Forum
Delete all .processed files VB Script
Remove lines from multiple text files PowerShell
Read multiple files with script VB Script
Retrieving selected lines from all text files in a directory PowerShell
Can One Folder display Files from multiple source folders? Vista file management


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