Sign in to follow this  
Followers 0
gspino

Stripping trailing whitespace routine

7 posts in this topic

Hello all. I wrote this routing to strip trailing whitespace from a small interface file. Although it works, I know it's cludgy. At my age, I struggle with some of this stuff. I cut my teeth on Fortran, Basica, Clipper and DOS scripting. I'm sure there is a better way to accomplish this goal, and I would love to learn some new techniques. I learn best by example, so if you have any suggestions or code that would work better, I would appreciate if you would share that with me. Here's the code and perhaps, someone out there may be able to use a snippet for something. Thank you. -Glen

;this routine is run after the raw interface file has been downloaded to the users P: drive

;==========================================

;create backup folder

DirCreate("backup")

;open the file for reading

$file = FileOpen("ship.dat", 0)

;Check if file opened for reading OK

If $file = -1 Then

MsgBox(0, " Error ", " Unable to open file. ", 30)

Exit

EndIf

;==========================================

If FileExists("ship.txt") Then

;make backup of prior month interface file

MsgBox(0, " Attention ", " File (ship.txt) already exists, creating a backup before overwriting. ", 5)

FileCopy("ship.txt", "backup\ship_backup.dat",1)

EndIf

;create an empty working file

$file2 = FileOpen("ship.txt", 2)

FileClose($file2)

;==========================================

;remove trailing whitespace from each line and write to new file

While 1

$line = FileReadLine($file)

If @error = -1 Then ExitLoop

;delete trailing whitespace

$linenew = StringStripWS($line, 2)

FileOpen("ship.txt", 1)

FileWriteLine($file2, $linenew)

FileClose($file2)

WEnd

FileClose($file)

;==========================================

;copy the file to the A: drive

$var = DriveStatus( "a:\")

While $var = "NOTREADY" Or $var = "UNKNOWN"

MsgBox(4096, " Copy to floppy disk ", " Please insert a FORMATTED floppy disk and click OK when ready", 120)

If DriveStatus( "a:\") = "READY" Then ExitLoop

WEnd

;copy the clean interface file to floppy disk

FileCopy("ship.txt", "a:\ship.dat", 1)

MsgBox(0, " Success ", " Your interface file has been successfully created on Floppy Drive A: ", 120)

;make a backup copy of the original downloaded raw interface file

FileMove("ship.dat", "backup\ship.dat", 1)

Exit

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Here, i made the the one while loop faster. You kept closing and opening a file every loop. The faster way is to open it prior to the loop and then write to it in the loop, then when you are done close it. I also used TidyIt to clean the source code and create indents. I also nested the strip whitespace function in the FileWriteLine function. Just faster that way. ;) I also got rid of the FileClose() that you used to close the file you just created. You don't need to close it since it is going to be written to in the next part of the code. Then for the copying to the floppy drive, the Msgbox always says it successfully created the file on the floppy, even if it didn't. So i added an if then statement. If it is able to copy the file onto the floppy it says it was successful, otherwise it informs the user of the failure that just occurred.

Also, in case you are wondering, to create the colored codebox that i have created below, you simply put the word autoit in brackets to start the box and then place your code after it. Once you get to the end of your code put /autoit in brackets and that will create an autoit colored code box.

;this routine is run after the raw interface file has been downloaded to the users P: drive
;==========================================
;create backup folder
DirCreate("backup")
;open the file for reading
$file = FileOpen("ship.dat", 0)
;Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, " Error ", " Unable to open file. ", 30)
    Exit
EndIf
;==========================================
If FileExists("ship.txt") Then
    ;make backup of prior month interface file
    MsgBox(0, " Attention ", " File (ship.txt) already exists, creating a backup before overwriting. ", 5)
    FileCopy("ship.txt", "backup\ship_backup.dat", 1)
EndIf
;create an empty working file
;you don't need to close the file since you will be using it in a moment in the loop
 $file2 = FileOpen("ship.txt", 2)
;==========================================
;remove trailing whitespace from each line and write to new file
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    ;delete trailing whitespace
    FileWriteLine($file2, StringStripWS($line, 2))
WEnd
FileClose($file2)
FileClose($file)
;==========================================
;copy the file to the A: drive
$var = DriveStatus("a:\")
While $var = "NOTREADY" Or $var = "UNKNOWN"
    MsgBox(4096, " Copy to floppy disk ", " Please insert a FORMATTED floppy disk and click OK when ready", 120)
    If DriveStatus("a:\") = "READY" Then ExitLoop
WEnd
;copy the clean interface file to floppy disk
IF FileCopy("ship.txt", "a:\ship.dat", 1) Then ;Filecopy returns 1 when it copies the file correctly, giving it the success msgbox
    MsgBox(0, " Success ", " Your interface file has been successfully created on Floppy Drive A: ", 120)
Else
    MsgBox(0, " Failure ", " Your interface file wasn't successfully created on Floppy Drive A: ", 120)
Endif
;make a backup copy of the original downloaded raw interface file
FileMove("ship.dat", "backup\ship.dat", 1)
Exit
Edited by The Kandie Man

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Share this post


Link to post
Share on other sites

Here, i made the the one while loop faster. You kept closing and opening a file every loop. The faster way is to open it prior to the loop and then write to it in the loop, then when you are done close it. I also used TidyIt to clean the source code and create indents. I also nested the strip whitespace function in the FileWriteLine function. Just faster that way. ;) I also got rid of the FileClose() that you used to close the file you just created. You don't need to close it since it is going to be written to in the next part of the code. Then for the copying to the floppy drive, the Msgbox always says it successfully created the file on the floppy, even if it didn't. So i added an if then statement. If it is able to copy the file onto the floppy it says it was successful, otherwise it informs the user of the failure that just occurred.

Also, in case you are wondering, to create the colored codebox that i have created below, you simply put the word autoit in brackets to start the box and then place your code after it. Once you get to the end of your code put /autoit in brackets and that will create an autoit colored code box.

;this routine is run after the raw interface file has been downloaded to the users P: drive
;==========================================
;create backup folder
DirCreate("backup")
;open the file for reading
$file = FileOpen("ship.dat", 0)
;Check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, " Error ", " Unable to open file. ", 30)
    Exit
EndIf
;==========================================
If FileExists("ship.txt") Then
    ;make backup of prior month interface file
    MsgBox(0, " Attention ", " File (ship.txt) already exists, creating a backup before overwriting. ", 5)
    FileCopy("ship.txt", "backup\ship_backup.dat", 1)
EndIf
;create an empty working file
;you don't need to close the file since you will be using it in a moment in the loop
 $file2 = FileOpen("ship.txt", 2)
;==========================================
;remove trailing whitespace from each line and write to new file
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    ;delete trailing whitespace
    FileWriteLine($file2, StringStripWS($line, 2))
WEnd
FileClose($file2)
FileClose($file)
;==========================================
;copy the file to the A: drive
$var = DriveStatus("a:\")
While $var = "NOTREADY" Or $var = "UNKNOWN"
    MsgBox(4096, " Copy to floppy disk ", " Please insert a FORMATTED floppy disk and click OK when ready", 120)
    If DriveStatus("a:\") = "READY" Then ExitLoop
WEnd
;copy the clean interface file to floppy disk
IF FileCopy("ship.txt", "a:\ship.dat", 1) Then ;Filecopy returns 1 when it copies the file correctly, giving it the success msgbox
    MsgBox(0, " Success ", " Your interface file has been successfully created on Floppy Drive A: ", 120)
Else
    MsgBox(0, " Failure ", " Your interface file wasn't successfully created on Floppy Drive A: ", 120)
Endif
;make a backup copy of the original downloaded raw interface file
FileMove("ship.dat", "backup\ship.dat", 1)
Exit
Thanks very much. I learned something new today. If you had a similar project would you have used arrays instead?

Share this post


Link to post
Share on other sites

Thanks very much. I learned something new today. If you had a similar project would you have used arrays instead?

Yes, i use arrays quite frequently. You will find them much faster when it comes to manipulating data. If you would like an example, please take a look at my script stripper(it is in my sig). I believe i used arrays to manipulate data in that. This was relatively simple though, so a simple filereadline works fine. If you wanted to change the data of the lines in the file in a specific way, an array would be the way to go.


"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Share this post


Link to post
Share on other sites

I got a chance to review your program with the use of arrays. I'm impressed. I'm going to give it a shot. Thank goodness for the help files - they're great as well as the personal support that so many give willingly. Thank you very much for your help and kindness. - Glen

Share this post


Link to post
Share on other sites

I just happened to manipulate file data as i described by using arrays. You can see the code at this post:

http://www.autoitscript.com/forum/index.ph...st&p=278847

-The Kandie Man


"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I added a couple of extra routines. The final code follows. I'll continue to experiment with using arrays. Thanks again for your assistance.

;gspino 12.09.2006
;this routine is run after the raw interface file has been downloaded to the users P: drive
;compile with AutoIT
;==========================================
$g_szVersion = "Interface 1.0"
If WinExists($g_szVersion) Then Exit
AutoItWinSetTitle($g_szVersion)
;==============================================
;SET OPTIONS
AutoItSetOption("TrayIconHide", 1)
;==============================================
;open the raw interface file for reading
$file = FileOpen("ship.dat", 0)
;check if file opened for reading OK
If $file = -1 Then
    MsgBox(0, " Fatal Error ", " Unable to open the downloaded interface file. " & @CRLF & " The interface file must reside in the same folder as this application. ", 30)
    Exit
EndIf
;create backup folder
DirCreate("backup")
;create an empty log file for import routine
$log = FileOpen("shiplog.dat", 2)
FileClose($log)
;display status message to the user
$message = " Creating Clean Interface File, Please wait... "
SplashTextOn(" Job Executing ", $message, 325, 50, -1, -1, 33, "", 12)
Sleep(3000)
;==========================================
If FileExists("ship.txt") Then
    ;make backup of prior month interface file
    FileCopy("ship.txt", "backup\ship_backup.dat", 1)
EndIf
;create an empty working file
$file2 = FileOpen("ship.txt", 2)
;==========================================
;remove trailing whitespace from each line
While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    FileWriteLine($file2, StringStripWS($line, 2))
WEnd
FileClose($file2)
FileClose($file)
;==========================================
If FileCopy("ship.txt", "ship.dat", 1) Then
    ;display successful status message to the user
    $message2 = " The Interface File is now ready for import "
    SplashTextOn(" Job Complete ", $message2, 325, 50, -1, -1, 33, "", 12)
    Sleep(5000)
    SplashOff()
    ;make a backup of the clean interface file
    $fname = String(@YEAR) & String(@MON) & String(@MDAY) & String (@HOUR) & String(@MIN) & String(@SEC) & ".dat"
    FileCopy("ship.txt", "backup\" & $fname, 1)
Else
    ;display failed status message to the user
    MsgBox(0, " Failure ", " Your interface file was NOT successfully created " & @CRLF & " Please notify the I.S. Dept. Immediately ")
EndIf
;==========================================
Exit
Edited by gspino

Share this post


Link to post
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
Sign in to follow this  
Followers 0