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 - Remove lines from multiple text files

Reply
 
Old 04-09-2009   #1 (permalink)
Peter E


 
 

Remove lines from multiple text files

Hi,

Bit of a noob question - I came across this bit of code the other day:

$f=${C:\Somepath\file.txt}
$f[0]=$null
${C:\Somepath\file.txt}=$f

...and I'm trying to modify it to change all files in a certain directory:

$files = dir "C:\Files\SQL Server Sandbox\*.txt"
foreach ($file in $files)
{
$f=${$file.fullname}
$f[0]=$null
${$file.fullname}=$f
}

but get an error:

Cannot index into a null array. At C:\psh
scripts\removelinesfromtextfiles.ps1:18 char:4 + $f[0 <<<< ]

What have I done wrong?

My System SpecsSystem Spec
Old 04-09-2009   #2 (permalink)
Alex K. Angelopoulos


 
 

Re: Remove lines from multiple text files

What you're doing there is performing an implicit "get-content" on
c:\somepath\file.txt. Assuming that line 18 is your
$f[0] = $null
line, it appears that the problem is either
c:\somepath\file.txt
does not exist, or
the file has no content.

I get it to work fine on my system.

"Peter E" <ubergeek@xxxxxx> wrote in message
news:09589C47-6391-4D97-A4EC-2C375910D3BE@xxxxxx
Quote:

> Hi,
>
> Bit of a noob question - I came across this bit of code the other day:
>
> $f=${C:\Somepath\file.txt}
> $f[0]=$null
> ${C:\Somepath\file.txt}=$f
>
> ..and I'm trying to modify it to change all files in a certain directory:
>
> $files = dir "C:\Files\SQL Server Sandbox\*.txt"
> foreach ($file in $files)
> {
> $f=${$file.fullname}
> $f[0]=$null
> ${$file.fullname}=$f
> }
>
> but get an error:
>
> Cannot index into a null array. At C:\psh
> scripts\removelinesfromtextfiles.ps1:18 char:4 + $f[0 <<<< ]
>
> What have I done wrong?
My System SpecsSystem Spec
Old 04-09-2009   #3 (permalink)
Peter E


 
 

Re: Remove lines from multiple text files

Hi Alex thanks for the reply.

I changed the loop to print the file names, here's part of the output:

C:\Files\SQL Server Sandbox\Full_Non_System_Subplan_2_20090407180004.txt
Cannot index into a null array.
At C:\psh scripts\removelinesfromtextfiles.ps1:18 char:4
+ $f[0 <<<< ]=$null
C:\Files\SQL Server Sandbox\Full_Non_System_Subplan_2_20090408060022.txt
Cannot index into a null array.
At C:\psh scripts\removelinesfromtextfiles.ps1:18 char:4
+ $f[0 <<<< ]=$null

So the files exist and I've checked they aren't empty.


"Alex K. Angelopoulos" wrote:
Quote:

> What you're doing there is performing an implicit "get-content" on
> c:\somepath\file.txt. Assuming that line 18 is your
> $f[0] = $null
> line, it appears that the problem is either
> c:\somepath\file.txt
> does not exist, or
> the file has no content.
>
> I get it to work fine on my system.
>
> "Peter E" <ubergeek@xxxxxx> wrote in message
> news:09589C47-6391-4D97-A4EC-2C375910D3BE@xxxxxx
Quote:

> > Hi,
> >
> > Bit of a noob question - I came across this bit of code the other day:
> >
> > $f=${C:\Somepath\file.txt}
> > $f[0]=$null
> > ${C:\Somepath\file.txt}=$f
> >
> > ..and I'm trying to modify it to change all files in a certain directory:
> >
> > $files = dir "C:\Files\SQL Server Sandbox\*.txt"
> > foreach ($file in $files)
> > {
> > $f=${$file.fullname}
> > $f[0]=$null
> > ${$file.fullname}=$f
> > }
> >
> > but get an error:
> >
> > Cannot index into a null array. At C:\psh
> > scripts\removelinesfromtextfiles.ps1:18 char:4 + $f[0 <<<< ]
> >
> > What have I done wrong?
>
>
My System SpecsSystem Spec
Old 04-09-2009   #4 (permalink)
Vadims Podans [MVP]


 
 

Re: Remove lines from multiple text files

it doesn't work for me (PS V2).
--
WBR, Vadims Podans
MVP: PowerShell
PowerShell blog - www.sysadmins.lv

"Alex K. Angelopoulos" <aka(at)mvps.org> rakstīja ziņojumā
"news:u$b0iPQuJHA.2596@xxxxxx"...
Quote:

> What you're doing there is performing an implicit "get-content" on
> c:\somepath\file.txt. Assuming that line 18 is your
> $f[0] = $null
> line, it appears that the problem is either
> c:\somepath\file.txt
> does not exist, or
> the file has no content.
>
> I get it to work fine on my system.
>
> "Peter E" <ubergeek@xxxxxx> wrote in message
> news:09589C47-6391-4D97-A4EC-2C375910D3BE@xxxxxx
Quote:

>> Hi,
>>
>> Bit of a noob question - I came across this bit of code the other day:
>>
>> $f=${C:\Somepath\file.txt}
>> $f[0]=$null
>> ${C:\Somepath\file.txt}=$f
>>
>> ..and I'm trying to modify it to change all files in a certain directory:
>>
>> $files = dir "C:\Files\SQL Server Sandbox\*.txt"
>> foreach ($file in $files)
>> {
>> $f=${$file.fullname}
>> $f[0]=$null
>> ${$file.fullname}=$f
>> }
>>
>> but get an error:
>>
>> Cannot index into a null array. At C:\psh
>> scripts\removelinesfromtextfiles.ps1:18 char:4 + $f[0 <<<< ]
>>
>> What have I done wrong?
>
My System SpecsSystem Spec
Old 04-09-2009   #5 (permalink)
Alex K. Angelopoulos


 
 

Re: Remove lines from multiple text files

My mistake; I didn't know what line 18 was, and simply tried to confirm that
the implicit get-content worked. The issue is as Bob points out, the name
not being correctly parsed.

"Peter E" <ubergeek@xxxxxx> wrote in message
news:6682F29F-F59C-40B1-8E27-38FA74DF267B@xxxxxx
Quote:

> Hi Alex thanks for the reply.
>
> I changed the loop to print the file names, here's part of the output:
>
> C:\Files\SQL Server Sandbox\Full_Non_System_Subplan_2_20090407180004.txt
> Cannot index into a null array.
> At C:\psh scripts\removelinesfromtextfiles.ps1:18 char:4
> + $f[0 <<<< ]=$null
> C:\Files\SQL Server Sandbox\Full_Non_System_Subplan_2_20090408060022.txt
> Cannot index into a null array.
> At C:\psh scripts\removelinesfromtextfiles.ps1:18 char:4
> + $f[0 <<<< ]=$null
>
> So the files exist and I've checked they aren't empty.
>
>
> "Alex K. Angelopoulos" wrote:
>
Quote:

>> What you're doing there is performing an implicit "get-content" on
>> c:\somepath\file.txt. Assuming that line 18 is your
>> $f[0] = $null
>> line, it appears that the problem is either
>> c:\somepath\file.txt
>> does not exist, or
>> the file has no content.
>>
>> I get it to work fine on my system.
>>
>> "Peter E" <ubergeek@xxxxxx> wrote in message
>> news:09589C47-6391-4D97-A4EC-2C375910D3BE@xxxxxx
Quote:

>> > Hi,
>> >
>> > Bit of a noob question - I came across this bit of code the other day:
>> >
>> > $f=${C:\Somepath\file.txt}
>> > $f[0]=$null
>> > ${C:\Somepath\file.txt}=$f
>> >
>> > ..and I'm trying to modify it to change all files in a certain
>> > directory:
>> >
>> > $files = dir "C:\Files\SQL Server Sandbox\*.txt"
>> > foreach ($file in $files)
>> > {
>> > $f=${$file.fullname}
>> > $f[0]=$null
>> > ${$file.fullname}=$f
>> > }
>> >
>> > but get an error:
>> >
>> > Cannot index into a null array. At C:\psh
>> > scripts\removelinesfromtextfiles.ps1:18 char:4 + $f[0 <<<< ]
>> >
>> > What have I done wrong?
>>
>>
My System SpecsSystem Spec
Old 04-09-2009   #6 (permalink)
Robert Robelo


 
 

RE: Remove lines from multiple text files

> What have I done wrong?

"C:\Files\SQL Server Sandbox\*.txt" contains spaces, get the file's short
path instead.
${$var} needs to be expanded, use Invoke-Expression.

# try this:
$fso = new-object -c scripting.fileSystemObject
foreach ($file in dir "C:\Files\SQL Server Sandbox\*.txt") {
trap {write-verbose "$path is empty"; continue}
$path = $fso.getFile($file.fullname).shortPath
[array]$f = iex "`${$path}"
$f[0] = ''
iex "`${$path} = `$f"
}
[void][Runtime.InteropServices.Marshal]::releaseComObject($fso)

--
Robert
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Replace text in multiple text files VB Script
How to merge teh lines of two text files? VB Script
Newbie question: replace multiple strings in multiple text files VB Script
Retrieving selected lines from all text files in a directory PowerShell
Possible to Read only Selected lines from multiple files into anotherfile deleting source files when processed? PowerShell


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