Jump to content

_FileReadToArray giving error


Recommended Posts

Hi all

Im trying to open a csv to remove some unneeded colums after i create it

The csv is created like this

#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

Link to comment
Share on other sites

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

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 by alien4u
Link to comment
Share on other sites

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

Link to comment
Share on other sites

solution only  for this csv file:

#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:

#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:

#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 by VIP
add new solution

Regards,
 

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...