Jump to content

Recommended Posts

Posted (edited)

Hi guys.

This time I'm not comming with a question, more to share, open disscusion and learn more.

My code so far looks like this:

 

#include <FileConstants.au3>
#include <File.au3>

Local $sNowyPlik = @ScriptDir & "\new.txt"

    Local $sMessage = "Choose file to phrase."
    Local $sFileOpenDialog = FileOpenDialog($sMessage, @ScriptDir, "CSV (*.csv)", 3)

    If @error Then
        MsgBox($MB_SYSTEMMODAL, "", "No files selected.")
        Return
        Else
        $fileOpen = FileOpen($sNowyPlik, 1)
        Local $iCountLines = _FileCountLines($sFileOpenDialog)
        Local $i = 1
        While $i <= $iCountLines

            $nazwa = FileReadLine($sFileOpenDialog, $i)
            Local $array = StringSplit($nazwa, ";")

            $kategoria = $array[1]
            $nazwaDokumentu = $array[2]
            $kod = $array[3]

            FileWrite($sNowyPlik, 'some text' & $kategoria & 'more text' & $kod & 'even more text' & $nazwaDokumentu & 'end')

            $i = $i + 1

        WEnd
        FileClose($fileOpen)
    EndIf

It's tested and works as intended. 

I would like to share this code maybe it can help someone.

Also if anyone know this can be done in other, more efficient, cleaner or more fullproof way, please let me know so I can learn :)

Thanks!

 

BTW. FileWrite is not mistake over FileWriteLine it was intended like this.

 

Edited by sakej
Posted

One way to improve your code (and make it more robust) is to add @error checking.

For example, if 

StringSplit($nazwa, ";")

fails for whatever reason (file is empty, etc), your script will crash.

Posted (edited)

Thanks for feedback :)

So improved version is:

#include <FileConstants.au3>
#include <File.au3>

Local $sNowyPlik = @ScriptDir & "\new.txt"

Local $sMessage = "Choose file to phrase."
Local $sFileOpenDialog = FileOpenDialog($sMessage, @ScriptDir, "CSV (*.csv)", 3)

If @error Then
    MsgBox($MB_SYSTEMMODAL, "", "No files selected.")
    Exit
Endif

    $fileOpen = FileOpen($sNowyPlik, 1)
    Local $iCountLines = _FileCountLines($sFileOpenDialog)
    If $iCountLines = 0 Then
        MsgBox(0, "", "Error, file is blank!")
        Exit
    EndIf

    For $i = 1 To $iCountLines

        $nazwa = FileReadLine($sFileOpenDialog, $i)
        Local $array = StringSplit($nazwa, ";")
        Local $noOfStrings = $array[0]
        If $noOfStrings <> 3 Then
            MsgBox(0, "", "Check your input file."&@CR&"Error on line : " & $noOfStrings &@CR& "I was hoping for 3 arguments each line!")
            Exit
        EndIf

        $kategoria = $array[1]
        $nazwaDokumentu = $array[2]
        $kod = $array[3]

        FileWrite($sNowyPlik, 'some text' & $kategoria & 'more text' & $kod & 'even more text' & $nazwaDokumentu & 'end')

    Next

    FileClose($fileOpen)
EndIf

Any other ideas how to make it better?

Edited by sakej
Posts below
Posted

No you're right :)

If you look at other parts of this code you will notice that

in same conditions "else" wasen't used, so...  :) 

To not make much fuss code from last post was updated. 

Posted

Yes it can be, but Tidy works better when it's down below :P

Anyway, I've updated earlier post with your sugestion and also 

did a bit more complex error checking on 

Quote

Local $array = StringSplit($nazwa, ";")

 

Posted (edited)

One small point; your code dangerously assumes that StringSplit will always return 4 rows,

unless you are dead sure every line in the file contains:

value1;value2;value3

If for example one line in your file is:

value1;value2

your script will fail.

 

Edited by dmob
Posted (edited)

@dmob shouldn't this part check for this scenario?

Quote

 Local $noOfStrings = $array[0]
        If $noOfStrings <> 3 Then
            MsgBox(0, "", "Check your input file."&@CR&"Error on line : " & $noOfStrings &@CR& "I was hoping for 3 arguments each line!")
            Exit
        EndIf

 

Edited by sakej
Posted

Since the Csv file requires 3 columns otherwise it fails, I would probably use something like:

#include <File.au3>

Local $sMessage = "Choose file to phrase."
Local $sCsvFilePath = FileOpenDialog($sMessage, @ScriptDir, "CSV (*.csv)", 3)
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "", "No files selected.")

Local $aCsvFilePath
    _FileReadToArray($sCsvFilePath, $aCsvFilePath, 1, ";")
    Switch @error
        Case 1
            MsgBox(4096, "Error:" & @error, "Error opening specified file")
        Case 2
            MsgBox(4096, "Error:" & @error, "Error Unable to split the file")
        Case 2
            MsgBox(4096, "Error:" & @error, "Error File lines have different numbers of fields")
        Case 2
            MsgBox(4096, "Error:" & @error, "Error No delimiters found")
    EndSwitch

Local $sNowyPlik = @ScriptDir & "\new.txt"

Local $hFileOpen = FileOpen($sNowyPlik, 1)
    If $hFileOpen = -1 Then Exit MsgBox(0, "", "Error, reading file " & $sCsvFilePath)
For $i = 1 To $aCsvFilePath[0][0]
    FileWrite($hFileOpen, "some text " & $aCsvFilePath[$i][0] & " more text " & $aCsvFilePath[$i][2] & " even more text " & $aCsvFilePath[$i][1] & " end " & @CRLF)
Next
FileClose($hFileOpen)

 

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
×
×
  • Create New...