Sign in to follow this  
Followers 0
kneze

read content of txt file

16 posts in this topic

Hi,

how can i read content of txt file and get output in a window.

My txt file has the folowing content: text;text1;text2;text3;text4;text5

the informations in my txt file are delimited with ;

I would like to get the folowing output;

Username=text

LastLogon=text1

etc.

thanks kneze

Share this post


Link to post
Share on other sites



Welcome to the forum ! Posted Image

try this ?

#include <Array.au3>

$_FilePath = 'c:\file.txt'
$_Text = FileRead ( $_FilePath )
ConsoleWrite ( "$_Text : " & $_Text & @Crlf )
$_TextArray = StringSplit ( $_Text, ';' )
_ArrayDisplay ( $_TextArray )

MsgBox ( 4096, "", "username : " & $_TextArray[1] & @CRLF & _
                   "Lastlogon : " & $_TextArray[2] & @CRLF & _
                   "......... : " & $_TextArray[3] & @CRLF & _
                   "......... : " & $_TextArray[4] & @CRLF & _
                   "......... : " & $_TextArray[5] & @CRLF & _
                   "......... : " & $_TextArray[6] & @CRLF )

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

thanks. your code will work for me.

only one question

my txt file is on a network share. In the first step i would like to check if the file exist.

The filename is "User id.txt"

the complete Program

1. User Must enter Userid

2. IF Exist \\server\share\folder\%userid%.txt goto read file else message file does not exist.

3. read txt file content.

the code that i have now is;

#include <Array.au3>

$sUserName = InputBox("UserID", "Enter User ID:")

$_FilePath = 'sample.txt'

$_Text = FileRead ( $_FilePath )

ConsoleWrite ( "$_Text : " & $_Text & @Crlf )

$_TextArray = StringSplit ( $_Text, ';' )

MsgBox ( 4096, "Last Userlogin INFO", "Lastlogon Date: " & $_TextArray[1] & @CRLF & _

"Domain: " & $_TextArray[2] & @CRLF & _

"CP Name: " & $_TextArray[3] & @CRLF & _

"User ID: " & $_TextArray[4] & @CRLF & _

"User: " & $_TextArray[5] & @CRLF & _

"Operating System:" & $_TextArray[6] & @CRLF & _

"Logon Server: " & $_TextArray[7] & @CRLF )

Share this post


Link to post
Share on other sites

Something like this ?

#include <Array.au3>

$sUserName = InputBox("UserID", "Enter User ID:")

$_FilePath = 'sample.txt'

If Not FileExists (  '\\server\share\folder\' & $sUserName & '.txt' ) Then Exit MsgBox ( 0, 'Error', 'File does not Exists !', 5 )

$_Text = FileRead ( $_FilePath )
ConsoleWrite ( "$_Text : " & $_Text & @Crlf )
$_TextArray = StringSplit ( $_Text, ';' )

MsgBox ( 4096, "Last Userlogin INFO", "Lastlogon Date: " & $_TextArray[1] & @CRLF & _
"Domain: " & $_TextArray[2] & @CRLF & _
"CP Name: " & $_TextArray[3] & @CRLF & _
"User ID: " & $_TextArray[4] & @CRLF & _
"User: " & $_TextArray[5] & @CRLF & _
"Operating System:" & $_TextArray[6] & @CRLF & _
"Logon Server: " & $_TextArray[7] & @CRLF )

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

yes perfect.

$sUserName = InputBox("UserID", "Enter User ID:")

How can i Set FIle name to $_FilePath = $sUserName +.txt

so if i enter user id: peter

the file name would be peter.txt

Share this post


Link to post
Share on other sites

Like this

$sUserName & '.txt'

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

thanks a lot! works fine now =).

Share this post


Link to post
Share on other sites

Hi

the Script is a part in a "while" loop so if file not exist they will close script completly but i want to exit only this part of

script.

So i have

While

Case Button XXX

If Not FileExists ( '\\server\share\folder\' & $sUserName & '.txt' ) Then Exit MsgBox ( 0, 'Error', 'File does not Exists !', 5 )

How can i do to return in while loop ?

Share this post


Link to post
Share on other sites

Suppress the Exit !


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

I have supress it 'exit' but the script go to next step wich only have to du if the file exist.

Share this post


Link to post
Share on other sites

hi,

Try replacing Exit with ContinueCase.

-smartee

Share this post


Link to post
Share on other sites

I have supress it 'exit' but the script go to next step wich only have to du if the file exist.

Show your loop script part.


AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

Or, if you prefer, you can try something like

Case Button XXX
    If Not FileExists('\\server\share\folder\' & $sUserName & '.txt') Then
        MsgBox(0, 'Error', 'File does not Exists !', 5)
    Else
        ;Do your stuff
    EndIf

Share this post


Link to post
Share on other sites

I Have a form with a lot of Buttons. in a while loop i have different "Cases".

Here is a part of code Case $Button_batch22 i would like exit from $Button_batch22 if file doesn't exist and my Code is

waiting for next button click.

;Check Size of userprofile (Remote Computer)
Case $Button_batch22
      ;save Variable $Input as $PCNamen 
      $CPname = GuiCtrlRead($Input)
                    
    If $CPname = "" Then
         MsgBox(0, "Error", "Missing Computer Nr. ")
       Else 
             MsgBox(0, "Message", "User Profile Size will be calculated.")              

        $var = Ping($CPname,250)
         If @error = 0 Then                     
            $sUserName = InputBox("UserID", "Enter UserID:")

      If Not FileExists (  "\\" & $cpname & "\c$\users\" & $sUserName ) Then MsgBox ( 0, 'Error', "No User Profile for " & $sUserName & "on "& $cpname & " found !", 5 )
                    
MsgBox(0,"Userprofile Size", "CP Name: " & $CPname &@CRLF &@CRLF & "Userprofile Size for: " & $sUserName &@CRLF &@CRLF & Round(DirGetSize( "\\" & $cpname & "\c$\users\" & $sUserName)/1048576,2) & "MB")                       
        Else 
          MsgBox(0, "Error", "Client not Online")
    EndIf
    EndIf

Share this post


Link to post
Share on other sites

Like this ?

;Check Size of userprofile (Remote Computer)
Case $Button_batch22
    ;save Variable $Input as $PCNamen 
    $CPname = GuiCtrlRead($Input)
    If $CPname = "" Then
        MsgBox(0, "Error", "Missing Computer Nr. ")
    Else 
        MsgBox(0, "Message", "User Profile Size will be calculated.")              
        $var = Ping($CPname,250)
        If @error = 0 Then                     
            $sUserName = InputBox("UserID", "Enter UserID:")
            If Not FileExists (  "\\" & $cpname & "\c$\users\" & $sUserName ) Then 
                MsgBox ( 0, 'Error', "No User Profile for " & $sUserName & "on "& $cpname & " found !", 5 )
            Else
                MsgBox(0,"Userprofile Size", "CP Name: " & $CPname &@CRLF &@CRLF & "Userprofile Size for: " & $sUserName &@CRLF &@CRLF & Round(DirGetSize( "\\" & $cpname & "\c$\users\" & $sUserName)/1048576,2) & "MB")                       
            EndIf
        Else 
            MsgBox(0, "Error", "Client not Online")
        EndIf
    EndIf

AutoIt 3.3.14.2 X86 - SciTE 3.6.0WIN 8.1 X64 - Other Example Scripts

Share this post


Link to post
Share on other sites

Thanks. It works now.

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