Sign in to follow this  
Followers 0
Systems By

Need help reading from text file

5 posts in this topic

Hi everyone. I am in need of ideas. I am trying to read the first line of a text file and have used the following code:

$file = FileOpen("C:\temp\tempdir.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    SplashTextOn( "Autobackup", "No Previous Home Backup File Found")
    Sleep(1000)
    SplashOff()
EndIf
While $file = 1

    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    SplashTextOn( "Autobackup", $line & " found. Transferring file to Office PC")
    Sleep(5000)
    SplashOff()


$filetransfer=$line


FileCopy($bckupextdirhome & $filetransfer, $homepcbudir & "*.*")
Sleep(3000)

  SplashTextOn( "Autobackup", "Transfer of " & $filetransfer & " complete")
    Sleep(5000)
    SplashOff()
wend
FileClose($file)

If my text file has something in it to read, the code works just fine; however, if the text file has no data, the program skips the part of the code that says

If $file = -1 Then

and just continues on to

$line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    SplashTextOn( "Autobackup", $line & " found. Transferring file to Office PC")
    Sleep(5000)
    SplashOff()

I need the program to be able to see that the file is empty and return the:

If $file = -1 Then
    SplashTextOn( "Autobackup", "No Previous Home Backup File Found")
    Sleep(1000)
    SplashOff()

Can anyone give me any hints? I am new to AutoIt, but have a pretty good understanding of how it works.

Thanks in advance.

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

Firstly

I recommend changing the "SplashTextOn"/"Off" to

MsgBox(0, "File Not Found","No Previous Home Backup File Found.)

^That save you from guessing how long it takes the user to read the message

Anyway, try

If $line = "" then
MsgBox(0, "No file", "Backup File Empty")
Endif

Edit Forgot an Endif

Edited by Paulie

Share this post


Link to post
Share on other sites

Thank you Paulie. I will give that a try.

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

If my text file has something in it to read, the code works just fine; however, if the text file has no data, the program skips the part of the code that says

If $file = -1 Then

and just continues on to

$line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    SplashTextOn( "Autobackup", $line & " found. Transferring file to Office PC")
    Sleep(5000)
    SplashOff()
$file contains the handle of the file, not the value of it's contents if any. Checking $file is to find if the file was unable to open as to being missing or in use by another program. In the helpfile you may see examples of using Exit if the check has failed. You can also use an Else condition to continue the script by skipping the While loop and FileClose as those lines are obsolete when FileOpen has failed.

I need the program to be able to see that the file is empty and return the:

If $file = -1 Then
    SplashTextOn( "Autobackup", "No Previous Home Backup File Found")
    Sleep(1000)
    SplashOff()

Can anyone give me any hints? I am new to AutoIt, but have a pretty good understanding of how it works.

Thanks in advance.

Checking the handle is no good, so just add a variable to the end of the loop to flag as having read an actual line to process. I have used $linecheck in the example below to do this.

Global $linecheck

$file = FileOpen("C:\temp\tempdir.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    SplashTextOn( "Autobackup", "No Previous Home Backup File Found")
    Sleep(1000)
    SplashOff()
    
Else
    While 1
        
        $line = FileReadLine($file)
        If @error = -1 Then ExitLoop
        
        $line = StringStripWS($line, 3) ; Remove any excess whitespace from line ends
        If $line = "" Then ContinueLoop ; Skip empty lines
        
        SplashTextOn( "Autobackup", $line & " found. Transferring file to Office PC")
        Sleep(5000)
        SplashOff()
        
        
        $filetransfer = $line
        
        
        If FileCopy($bckupextdirhome & $filetransfer, $homepcbudir) Then
            Sleep(3000)
        
            SplashTextOn( "Autobackup", "Transfer of " & $filetransfer & " complete")
            Sleep(5000)
            SplashOff()
            
        Else
            SplashTextOn( "Autobackup", "Transfer of " & $filetransfer & " failed")
            Sleep(5000)
            SplashOff()
        EndIf
        
        $linecheck = 1 ; Flag as non empty file
        
    WEnd
    FileClose($file)
    
    If $linecheck <> 1 Then
        SplashTextOn( "Autobackup", "Backup File Found With No Lines To Process")
        Sleep(1000)
        SplashOff()
    EndIf

EndIf

:D

Edit:

Added Sleep after a SplashOn for failed FileCopy

Edited by MHz

Share this post


Link to post
Share on other sites

$file contains the handle of the file, not the value of it's contents if any. Checking $file is to find if the file was unable to open as to being missing or in use by another program. In the helpfile you may see examples of using Exit if the check has failed. You can also use an Else condition to continue the script by skipping the While loop and FileClose as those lines are obsolete when FileOpen has failed.

Checking the handle is no good, so just add a variable to the end of the loop to flag as having read an actual line to process. I have used $linecheck in the example below to do this.

Global $linecheck

$file = FileOpen("C:\temp\tempdir.txt", 0)

; Check if file opened for reading OK
If $file = -1 Then
    SplashTextOn( "Autobackup", "No Previous Home Backup File Found")
    Sleep(1000)
    SplashOff()
    
Else
    While 1
        
        $line = FileReadLine($file)
        If @error = -1 Then ExitLoop
        
        $line = StringStripWS($line, 3) ; Remove any excess whitespace from line ends
        If $line = "" Then ContinueLoop ; Skip empty lines
        
        SplashTextOn( "Autobackup", $line & " found. Transferring file to Office PC")
        Sleep(5000)
        SplashOff()
        
        
        $filetransfer = $line
        
        
        If FileCopy($bckupextdirhome & $filetransfer, $homepcbudir) Then
            Sleep(3000)
        
            SplashTextOn( "Autobackup", "Transfer of " & $filetransfer & " complete")
            Sleep(5000)
            SplashOff()
            
        Else
            SplashTextOn( "Autobackup", "Transfer of " & $filetransfer & " failed")
            Sleep(5000)
            SplashOff()
        EndIf
        
        $linecheck = 1 ; Flag as non empty file
        
    WEnd
    FileClose($file)
    
    If $linecheck <> 1 Then
        SplashTextOn( "Autobackup", "Backup File Found With No Lines To Process")
        Sleep(1000)
        SplashOff()
    EndIf

EndIf

:D

Edit:

Added Sleep after a SplashOn for failed FileCopy

Thank you MHz, I understand what you are saying. I'll give your example a try :D

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