Chimaera Posted April 21, 2016 Share Posted April 21, 2016 Hi all Im trying to open a csv to remove some unneeded colums after i create it The csv is created like this expandcollapse popup#include <File.au3> #include <Array.au3> Global $aCSV[1] Global $OSTypeTest = _OsType(), $sWinVer, $OSType, $vReturn Global $filename = '"' & @ScriptDir & '\Logs\tasklist_logs\' & $OSTypeTest & '-' & @MDAY & '-' & @MON & '-' & @MIN & '-' & @SEC& '-tasklist.csv' & '"' ConsoleWrite( $filename & @CRLF) Global $schTest = RunWait(@ComSpec & ' /c ' & 'schtasks.exe /query /v /fo CSV >' & $filename, '', @SW_HIDE) ; make the csv Sleep(3000) Local $filetest = _FileReadToArray($filename, $aCSV, Default,',') ConsoleWrite( $filetest & ' - ' & 'error = ' & @error & @CRLF) _ArrayDisplay($aCSV) MsgBox(64, 'SchTasks Log', 'SchTasks Logging Has Completed', 2) Func _OsType() $sWinVer = FileGetVersion("winver.exe") ;~ ConsoleWrite($sWinVer & @CRLF) If StringInStr($sWinVer, '10.0.') Then ; Win 10 $OSType = 'Win_10' ElseIf StringInStr($sWinVer, '6.3.') Then ; Win 8.1 / Server 2012 R2 $OSType = 'Win_8.1' ElseIf StringInStr($sWinVer, '6.2.') Then ; Win 8.0 / Server 2012 $OSType = 'Win_8' ElseIf StringInStr($sWinVer, '6.1.') Then ; Win 7.0 / Server 2008R2 $OSType = 'Win_7' ElseIf StringInStr($sWinVer, '6.0.') Then ; Win Vista / Server 2008 $OSType = 'Win_Vista' ElseIf StringInStr($sWinVer, '5.2.') Then ; Win Server 2003 & R2 $OSType = 'Win_Server 2003' ElseIf StringInStr($sWinVer, '5.1.') Then ; Win XP $OSType = 'Win_XP' EndIf Return $OSType EndFunc ;==>_OsType and the result i get is this "G:\######\# Log PC\Capture Tasklist\Logs\tasklist_logs\Win_10-21-04-56-30-tasklist.csv" 0 - error = 1 It creates the csv fine and it opes in excel properly , i just dont understand why it wont open th file. Any suggestions please If Ive just helped you ... miracles do happen. Chimaera CopyRobo() * Hidden Admin Account Enabler * Software Location From Registry * Find Display Resolution * _ChangeServices() Link to comment Share on other sites More sharing options...
orbs Posted April 21, 2016 Share Posted April 21, 2016 On iPad, cannot test. but if any of your values has a comma (whether enclosed by double quotes or not) then you will have inconsistent fields count across your rows, which results in an error. Signature - my forum contributions: Spoiler UDF: LFN - support for long file names (over 260 characters) InputImpose - impose valid characters in an input control TimeConvert - convert UTC to/from local time and/or reformat the string representation AMF - accept multiple files from Windows Explorer context menu DateDuration - literal description of the difference between given dates Apps: Touch - set the "modified" timestamp of a file to current time Show For Files - tray menu to show/hide files extensions, hidden & system files, and selection checkboxes SPDiff - Single-Pane Text Diff Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 21, 2016 Moderators Share Posted April 21, 2016 Chimaera, Error = 1 suggests that the file cannot be opened - are you sure that the path is correctly formatted? Perhaps try a FileExists first to see if AutoIt recognises it. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Chimaera Posted April 21, 2016 Author Share Posted April 21, 2016 if i do a shellexecute right after the name stuff it opens fine in excel It doesnt make any sense why it wouldnt open. Ill keep trying to see if i can narrow down the cause If Ive just helped you ... miracles do happen. Chimaera CopyRobo() * Hidden Admin Account Enabler * Software Location From Registry * Find Display Resolution * _ChangeServices() Link to comment Share on other sites More sharing options...
alien4u Posted April 21, 2016 Share Posted April 21, 2016 (edited) Hi @Chimaera Your problem is this line: Global $filename = '"' & @ScriptDir & '\Logs\tasklist_logs\' & $OSTypeTest & '-' & @MDAY & '-' & @MON & '-' & @MIN & '-' & @SEC& '-tasklist.csv' & '"' You add " to the begin and the end of your filename and _FileReadToArray does not process that. Exactly as @Melba23 point the error show Error = 1 _FileReadToArray was not able to read the file. Solution: $filename = StringReplace($filename,'"',"") Local $filetest = _FileReadToArray($filename,$aCSV,Default,Default) Strip out the " before you use the _FileReadToArray function. Regards Alien. Edited April 21, 2016 by alien4u Link to comment Share on other sites More sharing options...
Chimaera Posted April 22, 2016 Author Share Posted April 22, 2016 17 hours ago, alien4u said: Solution: $filename = StringReplace($filename,'"',"") Local $filetest = _FileReadToArray($filename,$aCSV,Default,Default) Strip out the " before you use the _FileReadToArray function. Regards Alien. Ok thx for that i tested your code on a machine and it gave me the array but all jumbled up with no columns so i added the ',' to replace the default and its not worked again To be fair i haven't had the time to sit and go through it Im beginning to suspect there maybe something unusual with this csv when you try to display columns. I may have to make a separate script that opens the csv with excel and remove the rows and columns then but i shall get back to this as soon as i can If Ive just helped you ... miracles do happen. Chimaera CopyRobo() * Hidden Admin Account Enabler * Software Location From Registry * Find Display Resolution * _ChangeServices() Link to comment Share on other sites More sharing options...
Trong Posted April 24, 2016 Share Posted April 24, 2016 (edited) solution only for this csv file: expandcollapse popup#include <File.au3> #include <Array.au3> Global $sWinVer = _OsType() Global $sFileCSV = @ScriptDir & '\Logs\tasklist_logs\' & $sWinVer & '-' & @MDAY & '-' & @MON & '-' & @MIN & '-' & @SEC & '-tasklist.csv' ConsoleWrite("+ CSV: " & $sFileCSV & @CRLF) Local $hFile = FileOpen($sFileCSV, 2 + 8 + 128) FileClose($hFile) Global $schTest = RunWait(@ComSpec & ' /c ' & 'schtasks.exe /query /v /fo CSV >' & '"' & $sFileCSV & '"', '', @SW_HIDE) ; make the csv $hFile = FileOpen($sFileCSV, 128) Local $NewContent = FileRead($sFileCSV) FileClose($hFile) $hFile = FileOpen($sFileCSV, 2 + 8 + 128) $NewContent = StringReplace($NewContent, '",' & '"', '|') $NewContent = StringReplace($NewContent, '"', '') FileWrite($hFile, $NewContent) FileClose($hFile) Local $ArrayCSV[1] Local $sReturn = _FileReadToArray($sFileCSV, $ArrayCSV, Default, '|') Local $Error = @error ConsoleWrite("- Return: " & $sReturn & ' - Error = ' & $Error & @CRLF) If $Error Then ConsoleWrite("! SchTasks Logging Has failed" & @CRLF) MsgBox(64, 'SchTasks Log', 'SchTasks Logging Has failed', 2) Else ConsoleWrite("! SchTasks Logging Has Completed" & @CRLF) _ArrayDisplay($ArrayCSV) MsgBox(64, 'SchTasks Log', 'SchTasks Logging Has Completed', 2) EndIf Func _OsType() Local $sWinVer = FileGetVersion("winver.exe") If StringInStr($sWinVer, '10.0.') Then ; Win 10 $sWinVer = 'Win_10' ElseIf StringInStr($sWinVer, '6.3.') Then ; Win 8.1 / Server 2012 R2 $sWinVer = 'Win_8.1' ElseIf StringInStr($sWinVer, '6.2.') Then ; Win 8.0 / Server 2012 $sWinVer = 'Win_8' ElseIf StringInStr($sWinVer, '6.1.') Then ; Win 7.0 / Server 2008R2 $sWinVer = 'Win_7' ElseIf StringInStr($sWinVer, '6.0.') Then ; Win Vista / Server 2008 $sWinVer = 'Win_Vista' ElseIf StringInStr($sWinVer, '5.2.') Then ; Win Server 2003 & R2 $sWinVer = 'Win_Server 2003' ElseIf StringInStr($sWinVer, '5.1.') Then ; Win XP $sWinVer = 'Win_XP' EndIf ConsoleWrite("! OSVersion: " & $sWinVer & @CRLF) Return $sWinVer EndFunc ;==>_OsType S2: expandcollapse popup#include <File.au3> #include <Array.au3> Global $sWinVer = _OsType() Global $sFileCSV = @ScriptDir & '\Logs\tasklist_logs\' & $sWinVer & '-' & @MDAY & '-' & @MON & '-' & @MIN & '-' & @SEC & '-tasklist.csv' ConsoleWrite("+ CSV: " & $sFileCSV & @CRLF) Local $hFile = FileOpen($sFileCSV, 2 + 8 + 128) FileClose($hFile) Global $schTest = RunWait(@ComSpec & ' /c ' & 'schtasks.exe /query /v /fo CSV >' & '"' & $sFileCSV & '"', '', @SW_HIDE) ; make the csv $hFile = FileOpen($sFileCSV, 128) Local $NewContent = FileRead($sFileCSV) FileClose($hFile) $hFile = FileOpen($sFileCSV, 2 + 8 + 128) $NewContent = StringReplace($NewContent, '",' & '"', '"|"') $NewContent = StringReplace($NewContent, "'", '´');`´ FileWrite($hFile, $NewContent) FileClose($hFile) Local $ArrayCSV[1] Local $sReturn = _FileReadToArray($sFileCSV, $ArrayCSV, Default, '|') Local $Error = @error ConsoleWrite("- Return: " & $sReturn & ' - Error = ' & $Error & @CRLF) If $Error Then ConsoleWrite("! SchTasks Logging Has failed" & @CRLF) MsgBox(64, 'SchTasks Log', 'SchTasks Logging Has failed', 2) Else ConsoleWrite("! SchTasks Logging Has Completed" & @CRLF) _ArrayDisplay($ArrayCSV) MsgBox(64, 'SchTasks Log', 'SchTasks Logging Has Completed', 2) EndIf Func _OsType() Local $sWinVer = FileGetVersion("winver.exe") If StringInStr($sWinVer, '10.0.') Then ; Win 10 $sWinVer = 'Win_10' ElseIf StringInStr($sWinVer, '6.3.') Then ; Win 8.1 / Server 2012 R2 $sWinVer = 'Win_8.1' ElseIf StringInStr($sWinVer, '6.2.') Then ; Win 8.0 / Server 2012 $sWinVer = 'Win_8' ElseIf StringInStr($sWinVer, '6.1.') Then ; Win 7.0 / Server 2008R2 $sWinVer = 'Win_7' ElseIf StringInStr($sWinVer, '6.0.') Then ; Win Vista / Server 2008 $sWinVer = 'Win_Vista' ElseIf StringInStr($sWinVer, '5.2.') Then ; Win Server 2003 & R2 $sWinVer = 'Win_Server 2003' ElseIf StringInStr($sWinVer, '5.1.') Then ; Win XP $sWinVer = 'Win_XP' EndIf ConsoleWrite("! OSVersion: " & $sWinVer & @CRLF) Return $sWinVer EndFunc ;==>_OsType S3: expandcollapse popup#include <File.au3> #include <Array.au3> Global $sWinVer = _OsType() Global $sFileCSV = @ScriptDir & '\Logs\tasklist_logs\' & $sWinVer & '-' & @MDAY & '-' & @MON & '-' & @MIN & '-' & @SEC & '-tasklist.csv' ConsoleWrite("+ CSV: " & $sFileCSV & @CRLF) Local $hFile = FileOpen($sFileCSV, 2 + 8 + 128) FileClose($hFile) Global $schTest = RunWait(@ComSpec & ' /c ' & 'schtasks.exe /query /v /fo CSV >' & '"' & $sFileCSV & '"', '', @SW_HIDE) ; make the csv $hFile = FileOpen($sFileCSV, 128) Local $NewContent = FileRead($sFileCSV) FileClose($hFile) $hFile = FileOpen($sFileCSV, 2 + 8 + 128) $NewContent = StringReplace($NewContent, "'", '´') FileWrite($hFile, $NewContent) FileClose($hFile) Local $ArrayCSV = _SplitCSV($sFileCSV, ',') Local $Error = @error ConsoleWrite("- Return: " & $ArrayCSV & ' - Error = ' & $Error & @CRLF) _ArrayDisplay($ArrayCSV) Func _SplitCSV($sFilePath, $sDelimiter = ",") If $sDelimiter = "" Then Return SetError(1, 0, 0) Local $aLines = FileReadToArray($sFilePath) If @error Then Return SetError(@error > 0, 0, 0) Local $sPattern = '(["''][^"'']+["'']|[^' & $sDelimiter & ']+)' & $sDelimiter & '?\s*' Local $bLines = StringRegExp($aLines[0], $sPattern, 3) Local $iDim_1 = UBound($aLines) + 0 Local $iDim_2 = UBound($bLines) Local $aTemp_Array[$iDim_1][$iDim_2] Local $iFields, $aSplit For $i = 0 To $iDim_1 - 1 $aSplit = StringRegExp($aLines[$i], $sPattern, 3) $iFields = UBound($aSplit) If $iFields <> $iDim_2 Then ConsoleWrite("!" & $iFields & "-" & $aLines[$i - 1] & @CRLF) ConsoleWrite("!" & $iDim_2 & "-" & $aLines[$i] & @CRLF) Return SetError(2, 0, 0) EndIf For $j = 0 To $iFields - 1 $aTemp_Array[$i + 0][$j] = $aSplit[$j] Next Next If $iDim_2 < 2 Then Return SetError(3, 0, 0) Return $aTemp_Array EndFunc ;==>_SplitCSV Func _OsType() Local $sWinVer = FileGetVersion("winver.exe") If StringInStr($sWinVer, '10.0.') Then ; Win 10 $sWinVer = 'Win_10' ElseIf StringInStr($sWinVer, '6.3.') Then ; Win 8.1 / Server 2012 R2 $sWinVer = 'Win_8.1' ElseIf StringInStr($sWinVer, '6.2.') Then ; Win 8.0 / Server 2012 $sWinVer = 'Win_8' ElseIf StringInStr($sWinVer, '6.1.') Then ; Win 7.0 / Server 2008R2 $sWinVer = 'Win_7' ElseIf StringInStr($sWinVer, '6.0.') Then ; Win Vista / Server 2008 $sWinVer = 'Win_Vista' ElseIf StringInStr($sWinVer, '5.2.') Then ; Win Server 2003 & R2 $sWinVer = 'Win_Server 2003' ElseIf StringInStr($sWinVer, '5.1.') Then ; Win XP $sWinVer = 'Win_XP' EndIf ConsoleWrite("! OSVersion: " & $sWinVer & @CRLF) Return $sWinVer EndFunc ;==>_OsType Edited April 24, 2016 by VIP add new solution Regards, Link to comment Share on other sites More sharing options...
alien4u Posted April 24, 2016 Share Posted April 24, 2016 (edited) @VIP Exactly where your solution is different from the one already post? Is not the same that was already said about the colons? Regards Alien. Edited April 24, 2016 by alien4u Link to comment Share on other sites More sharing options...
alien4u Posted April 24, 2016 Share Posted April 24, 2016 I don't read enough, your solution adding | works. Thanks and sorry.. Regards Alien. Link to comment Share on other sites More sharing options...
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