sakej Posted November 7, 2018 Posted November 7, 2018 (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 November 7, 2018 by sakej
dmob Posted November 7, 2018 Posted November 7, 2018 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.
Nine Posted November 7, 2018 Posted November 7, 2018 Instead of a while, you should have used For $i = 1 to $iCountLines ... Next No need to declare $i, nor initialize it, nor having the increment $i = $i + 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
sakej Posted November 7, 2018 Author Posted November 7, 2018 (edited) Thanks for feedback So improved version is: expandcollapse popup#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 November 7, 2018 by sakej Posts below
Nine Posted November 7, 2018 Posted November 7, 2018 One last thing I would recommend. In your first if, it is ending with Exit, so the else is useless. It will make your script more readable. Or maybe i am too picky “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
sakej Posted November 7, 2018 Author Posted November 7, 2018 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.
Nine Posted November 7, 2018 Posted November 7, 2018 endif should be move at the place of the else tho “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy Interface Object based on Tag
sakej Posted November 7, 2018 Author Posted November 7, 2018 Yes it can be, but Tidy works better when it's down below Anyway, I've updated earlier post with your sugestion and also did a bit more complex error checking on Quote Local $array = StringSplit($nazwa, ";")
dmob Posted November 7, 2018 Posted November 7, 2018 (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 November 7, 2018 by dmob
sakej Posted November 7, 2018 Author Posted November 7, 2018 (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 November 7, 2018 by sakej
dmob Posted November 7, 2018 Posted November 7, 2018 (edited) Yes it would. I hadn't seen the changes you'd made... Edited November 7, 2018 by dmob
Subz Posted November 7, 2018 Posted November 7, 2018 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)
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now