• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Remove lines from multiple text files

P

Peter E

#1
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 Computer

A

Alex K. Angelopoulos

#2
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" <[email protected]> wrote in message
news:[email protected]

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

P

Peter E

#3
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:

> 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" <[email protected]> wrote in message
> news:[email protected]

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

V

Vadims Podans [MVP]

#4
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:[email protected]"...

> 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" <[email protected]> wrote in message
> news:[email protected]

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

A

Alex K. Angelopoulos

#5
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" <[email protected]> wrote in message
news:[email protected]

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

>> 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" <[email protected]> wrote in message
>> news:[email protected]

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

R

Robert Robelo

#6
> 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 Computer