![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | 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 Specs![]() |
| | #3 (permalink) |
| | 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 Specs![]() |
| | #4 (permalink) |
| | 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 Specs![]() |
| | #5 (permalink) |
| | 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 Specs![]() |
| | #6 (permalink) |
| | 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 Specs![]() |
![]() |
| 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 | |||