Jump to content

CSV File reading then use value


rikho
 Share

Recommended Posts

Good afternoon all from Paris

I'm a young administrator system and i have to learn programing with autoit (i'm sorry for my bad english use).

My first job is to make automaticly an install of security box, the software is already in package, and the tool i

use to deploy the software needs, before install some files and folders tuning.

I have 3 parameters include in a *.csv file with the geograhique code of users, the name of user, and the coputername of users like this :

exemple of csv file :

NN6598,Paris02;ICDF0215

NN6599,Paris02;ICDF0216

NN6600,Paris04;ICDF0220

I need to read each line and create some variables with each value. i know this is really easy for all of you and i appreciate your help

If someone can give me a source code to start my script may be i can continue alone with help chm.

thanks.

Link to comment
Share on other sites

Good afternoon all from Paris

I'm a young administrator system and i have to learn programing with autoit (i'm sorry for my bad english use).

My first job is to make automaticly an install of security box, the software is already in package, and the tool i

use to deploy the software needs, before install some files and folders tuning.

I have 3 parameters include in a *.csv file with the geograhique code of users, the name of user, and the coputername of users like this :

exemple of csv file :

NN6598,Paris02;ICDF0215

NN6599,Paris02;ICDF0216

NN6600,Paris04;ICDF0220

I need to read each line and create some variables with each value. i know this is really easy for all of you and i appreciate your help

If someone can give me a source code to start my script may be i can continue alone with help chm.

thanks.

This will read whole file into Array. Check for _FileReadToArray explanation in AutoIt helpfile.

#include <file.au3>
Dim  $aRecords
If Not _FileReadToArray("error.log",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array      error:" & @error)
   Exit
EndIf
For $x =  1 to $aRecords[0]
    Msgbox(0,'Record:' & $x, $aRecords[$x])
Next

Now use StringSplit to split each line to get values. As a delimeter use ",".

Hope this helps for a start.

My little company: Evotec (PL version: Evotec)

Link to comment
Share on other sites

Here's a way:

;~ csv file:
;~ NN6598,Paris02;ICDF0215
;~ NN6599,Paris02;ICDF0216
;~ NN6600,Paris04;ICDF0220
$Line=1
$path="myfile.csv"
While 1 
    $String=FileReadLine($path,$Line)
    If $String="" Then Exit
    $Snam=StringSplit($String,",")
        $Znam=StringSplit($Snam[2],";")
        MsgBox(0,"","Geograhique code :"&$Snam[1]&" User: "&$Znam[1]&" Computername: "&$Znam[2])
    $Line+=1
WEnd

Haha, I took too long...

Edited by Nahuel
Link to comment
Share on other sites

Or this :)

#include <file.au3>
#include <array.au3>

Global $csv_file = "C:\file.csv"
Global $aRecords

If Not _FileReadToArray($csv_file,$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array error:" & @error)
   Exit
EndIf
For $x = 1 to $aRecords[0]
    Msgbox(0,'Record:' & $x, $aRecords[$x]) ; Shows the line that was read from file
    $csv_line_values = StringSplit($aRecords[$x], ",",1) ; Splits the line into 2 or more variables and puts them in an array
    _ArrayDisplay($csv_line_values) ; Shows what's in the array you just created.
    ; $csv_line_values[0] holds the number of elements in array
    ; $csv_line_values[1] holds the value
    ; $csv_line_values[2] holds the value
    ; etc
Next

My little company: Evotec (PL version: Evotec)

Link to comment
Share on other sites

Thx all !

i found this method, i dont know if it's the best one, but it's work perfectly :)

#include <file.au3>
Dim  $aRecords
Dim $bRecords[1][4]
$aRecords = "liste.csv"
If Not _FileReadToArray(@scriptdir & "\liste.csv",$aRecords) Then
   MsgBox(4096,"Error", " Error reading log to Array      error:" & @error)
   Exit
EndIf
ReDim $bRecords[$aRecords[0]+1][4]
For $x = 1 to $aRecords[0]
    $temp1=StringSplit($aRecords[$X],";")
    For $y = 1 to 3
        $bRecords[$x][$y]=$temp1[$y]
    Next 
Next
For $i=1 to UBound($bRecords,1) - 1
    For $J=1 to UBound($bRecords,2) - 1
        MsgBox(0,"Ligne " & String($i),$bRecords[$i][$j])
    next
Next
Link to comment
Share on other sites

  • 5 months later...

hello,

Your code was interesting. I tried it and found another response to it, more fluent in a way.

With the small changes i've done, you can use each value in a csv. In my case, it's 3 values separated by a ";" separator.

See below.

$Line=1
$path="file.csv"
While 1
    $String=FileReadLine($path,$Line)
    If $String="" Then Exit
    $Snam=StringSplit($String, ";")
        MsgBox(0,"","Serveur : " & $Snam[1] & @CRLF & " Disque : " & $Snam[2] & @CRLF & " Valeur : " & $Snam[3])
    $Line+=1
WEnd
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...