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 - Insert Header into Source Files

Reply
 
Old 08-01-2008   #1 (permalink)
David


 
 

Insert Header into Source Files

What would be the best method to insert a header into existing source files?
A simple method is to get-content (cat) the header and source file into a new
file. I was hoping to be able to insert the header into the existing source
files.

....
Get-ChildItem -Recurse * | foreach {
# check for directory
if (!($_.PSIsContainer))
{
# check for filename.extension format filename
if (($_.Name.Split(".")).count -eq 2)
{
$basename,$extension = $_.Name.Split(".")
# check for .c or .h files
if ($extension -eq "c" -or $extension -eq "h")
{
get-content header.txt > $newpath + $_
get-content $_ >> $newpath + $_
}
}
}
}
....

My System SpecsSystem Spec
Old 08-01-2008   #2 (permalink)
Kiron


 
 

Re: Insert Header into Source Files

You can combine Get-Content and Set-Content to write header.txt's content and the piped file's content 'back' to the piped file, but Set-Content will really overwrite the piped file with the modified content. Note the use of the optional -Encoding parameter, by default it's set to Unicode.

Encoding acceptable values:

Name Val
---- ---
Unknown 0
String 1
Unicode 2
Byte 3
BigEndianUnicode 4
UTF8 5
UTF7 6
Ascii 7

In this example the new files' encoding will be UTF-8. First get the header's fully qualified path through Get-Item. Get child items and filter those files with a .c or .h extension. Get header content and the piped .c or .h file and overwrite the piped file through Set-Content.

$hdr = (gi header.txt).fullname
ls -r | ? {!$_.psIsContainer -and $_.extension -match '^\.[ch]$'} |
% {sc $_.fullname (gc $hdr, $_.fullname) -en 5}

--
Kiron
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
when insert a disc does not come to view files Vista General
WLM insert scripts in code source Live Mail
Possible to Read only Selected lines from multiple files into anotherfile deleting source files when processed? PowerShell
CD Insert/Jewel Case Insert Maker ignored? Vista music pictures video
Source files? Vista General


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