Jump to content

StripWS from file


Recommended Posts

Below is my function that I am trying to write. I am doing something wrong and I don't know what. At the bottom I have the file which I want to alter. The script below will remove the white space within a line, but how do I get rid of the blank lines in between. I have tried stringreplace as well. I must be missing something simple I assume. Part two is the lines in the file could be fine but when I pull the arraytostring from my _stringbetween, it will bring back the blank lines which I do not understand either since I am pulling the items between the [] into an array. Any help would be greatly appreciated.

Func _getDHCPServer()
    $dhcpNetsh = RunWait(@ComSpec & " /c netsh dhcp show server dump > " & '"C:\Documents and Settings\bsiegm\Desktop\Scripting\AutoIT Scripts\DHCP\dhcpserver.txt"')
    
    ;open the file for reading
    $file = FileOpen("dhcpserver.txt", 0)
    
;Check if file opened for reading OK
    If $file = -1 Then
        MsgBox(0, " Error ", " Unable to open file. ", 30)
        Exit
    EndIf

     $file2 = FileOpen("newDHCPServer.txt", 2)
;StringStripCR($file2) 
    _FileWriteToLine("dhcpserver.txt",1,"",1)
    _FileWriteToLine("dhcpserver.txt",1,"",1)

    While 1
    $line = FileReadLine($file)

    If @error = -1 Then ExitLoop
    FileWriteLine($file2, StringStripWS($line, 8))

    WEnd

    FileClose($file2)
    FileClose($file)
    

    $dhcpServer_file = FileOpen("dhcpserver.txt",0)
;StringStripCR($dhcpServer_file)
    
    If $dhcpServer_file = -1 Then
   ;MsgBox(0, "Error", "Unable to open file.")
    Exit
    EndIf

    
    While 1
        $line = FileReadLine($dhcpServer_file)
        $aArray1 = _StringBetween($line, '[', ']');Not using SRE
        $sArrayString = _ArrayToString($aArray1,@TAB)
        MsgBox(4096, "_ArrayToString() Test", $sArrayString )
        If @error = -1 Then ExitLoop

    WEnd
    FileClose($dhcpServer_file)
EndFunc

Server[server10]Address[10.1.1.1]

Server[server11]Address[10.1.1.2]

Server[server12]Address[10.1.1.3]

Server[server13]Address[10.1.1.4]

Link to comment
Share on other sites

  • Moderators

Had you tried (Example only, you'll need to implement):

$sString = StringReplace($sString, @CR, '')
$sString = StringReplace($sString, @LF, '')
MsgBox(64, 'No Line Feeds', $sString)
?

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

not understand either since I am pulling the items between the [] into an array. Any help would be greatly appreciated.

Something like this?

#include <array.au3>
#include <File.au3>

Dim $lines

$filename = "c:\temp\au3\40530.txt"
_FileReadToArray($filename,$lines)
_ArrayDisplay($lines,"File")

Dim $newlines[1]

$n = 0
for $i =  1 to $lines[0]
    $lines[$i] = StringStripWS($lines[$i],8)
    if (StringLen($lines[$i])) then
        $n = $n + 1
        _ArrayAdd($newlines,$lines[$i])
    endif
next

$newlines[0] = $n
_ArrayDisplay($newlines,"File")

_FileWriteFromArray($filename & ".new", $newlines,1)

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...