Remove lines from multiple text files


  1.    09 Apr 2009 #1
    Peter E Guest

    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

  2.    09 Apr 2009 #2
    Alex K. Angelopoulos Guest

    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

    > 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

  3.    09 Apr 2009 #3
    Peter E Guest

    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:

    > 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

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

  4.    09 Apr 2009 #4
    Vadims Podans [MVP] Guest

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

    > 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

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

  5.    09 Apr 2009 #5
    Alex K. Angelopoulos Guest

    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

    > 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" <ubergeek@xxxxxx> wrote in message
    >> news:09589C47-6391-4D97-A4EC-2C375910D3BE@xxxxxx

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

  6.    09 Apr 2009 #6
    Robert Robelo Guest

    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

Remove lines from multiple text files

Similar Threads
Thread Forum
Replace text in multiple text files
Im trying to Replace text in multiple text files using the below mentioned code...but im not able to do it. Ex: Text File Content Line 1 Line 2...
VB Script
How to merge teh lines of two text files?
Assume I have two text files with multiple (long) lines each. Now I want to merge them insofar that duplicate parts are eliminated and the...
VB Script
Newbie question: replace multiple strings in multiple text files
Hi there -- I've never created a script before now, and what I want to do is periodically have a script go into fifteen text files and do...
VB Script
replacing text on multiple text files
Hello. I am looking for a way to automatically replace text on multiple text files. I am writing my first script for work, but I've hit a brick...
VB Script
Retrieving selected lines from all text files in a directory
I need to extract all lines that begin with "B3" from text files that all reside in the same directory on a server and export it to a comma...
PowerShell
Howto: Add lines of text from a specific point in a text file..
Hi all, I wonder if you can help me... I have been doing allot of manual updates to ini files that i am using and would like to make things a...
VB Script
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...
PowerShell

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 14:36.
    .