none
OpenTextFile permission denied RRS feed

  • Question

  • Hello,

    I am having issues trying to run the script below, which reads a CSV file into a 2-dimensional array.  The error is thrown here:   Set inCsv    = inCsvSys.OpenTextFile(CSVFile,"1",True)  

    This is the error that I get on this line: Permission denied Code:800A0046

    Function CSVArray(CSVFile)
     
      Dim comma, quote
      comma = ","
      quote = Chr(34)
     
      Dim charPos, charVal
     
      Dim cellPos, colMax, colNum
      colMax  = -1
     
      Dim cellArray(), cellComplete, cellQuoted, csvRecord
     
      Dim inCsvSys, inCsv, inRow(), rowCount
      rowCount     = -1
      Set inCsvSys = CreateObject("Scripting.FileSystemObject")
      Set inCsv    = inCsvSys.OpenTextFile(CSVFile)',"1",True)
      Do While Not inCsv.AtEndOfStream
        rowCount = rowCount + 1
        Redim Preserve inRow(rowCount)
        inRow(rowCount) = inCsv.ReadLine
      Loop
      inCsv.Close
     
      For r = 0 to rowCount
     
        csvRecord = inRow(r)
        colNum = -1
        charPos = 0
        cellComplete = True
        
        Do While charPos < Len(csvRecord)
     
          If (cellComplete = True) Then
            colNum       = colNum + 1
            cellPos      = 0
            cellQuoted   = False
            cellComplete = False
            If colNum > colMax Then
              colMax = colNum
              Redim Preserve cellArray(rowCount,colMax)
            End If              
          End If
     
          charPos = charPos + 1
          cellPos = cellPos + 1
          charVal = Mid(csvRecord, charPos, 1)
          If (charVal = quote) Then
            If (cellPos = 1) Then
              cellQuoted = True
              charVal    = ""
            Else
              Select Case Mid(csvRecord, charPos+1, 1)
              Case quote
                charPos = charPos + 1
              Case comma
                charPos = charPos + 1
                cellComplete = True
              End Select
            End If
          ElseIf (charVal = comma) And (cellQuoted = False) Then
            cellComplete = True
          End If
          If (cellComplete = False) Then
            cellArray(r,colNum) = cellArray(r,colNum)&charVal
          End If
     
        Loop
     
      Next
      CSVArray = cellArray
    End Function



    • Edited by afboucher Thursday, January 3, 2019 1:31 PM
    Thursday, January 3, 2019 1:11 AM

Answers

  • You canot open a file you do not hve permission on and you cannot open a file that is already opne.

    The line of code is wrong also.

    Set inCsv    = inCsvSys.OpenTextFile(CSVFile,1,True)

    To open a file for reading this is the line:

    Set inCsv    = inCsvSys.OpenTextFile(CSVFile)


    \_(ツ)_/

    • Marked as answer by afboucher Thursday, January 3, 2019 4:09 PM
    Thursday, January 3, 2019 1:29 AM

All replies

  • Please fix your post.  Turn off Grammarly and post code with the code posting tool.


    \_(ツ)_/

    Thursday, January 3, 2019 1:26 AM
  • You canot open a file you do not hve permission on and you cannot open a file that is already opne.

    The line of code is wrong also.

    Set inCsv    = inCsvSys.OpenTextFile(CSVFile,1,True)

    To open a file for reading this is the line:

    Set inCsv    = inCsvSys.OpenTextFile(CSVFile)


    \_(ツ)_/

    • Marked as answer by afboucher Thursday, January 3, 2019 4:09 PM
    Thursday, January 3, 2019 1:29 AM
  • Please edit your original post and fix both the text and the code.  The post is unreadable.


    \_(ツ)_/

    Thursday, January 3, 2019 12:26 PM
  • I know I have permission to the file - I actually have it saved on my desktop right now.  And just to be sure, I granted full permissions to the everyone group.  I made the change you suggested above, and now I get "File not found."  
    Thursday, January 3, 2019 12:45 PM
  • I know I have permission to the file - I actually have it saved on my desktop right now.  And just to be sure, I granted full permissions to the everyone group.  I made the change you suggested above, and now I get "File not found."  

    Then you have a mistake in your file name parameter.

    Try just running a vbscript file with two lines.

    Set inCsvSys = CreateObject("Scripting.FileSystemObject")
    Set inCsv    = inCsvSys.OpenTextFile("c:\test.txt")
    WScript.Echo "FIle found and opened successfully"
    

    First please fix you original post and delete the second one.


    \_(ツ)_/

    Thursday, January 3, 2019 12:59 PM
  • Note that that while VBScript can be done in one line in PowerShell:

    $csvArray = Import-Csv $csvFile


    \_(ツ)_/

    Thursday, January 3, 2019 1:14 PM
  • This is weird.  I have created a script using your suggestion above, and I changed the filename to the one I am trying to open, and it completes successfully.  I also put in a MsgBox to make sure that the CSVFile variable is being passed to the function correctly.  
    Thursday, January 3, 2019 1:16 PM
  • Thankyou for fixing the code.  Now fix the text at the beginning of the post.  It is filled with HTML code.  To use this forum you must either turn off Grammarly for this site or upgrade Grammarly to the fixed version.

    \_(ツ)_/

    Thursday, January 3, 2019 1:17 PM
  • Text should be fixed now.  
    Thursday, January 3, 2019 1:32 PM
  • Text should be fixed now.  

    Thankyou.


    \_(ツ)_/

    Thursday, January 3, 2019 1:34 PM
  • The actual error I am getting is "File not found" Code: 800A0035
    Thursday, January 3, 2019 1:45 PM
  • The actual error I am getting is "File not found" Code: 800A0035

    What is the exact line of code causing the error?


    \_(ツ)_/

    Thursday, January 3, 2019 1:54 PM
  • Here is how to convert a CSV file to a 2 dimensional array in VBScript:

    Function CSVArray(CSVFile)
     	
    	Set fso = CreateObject("Scripting.FileSystemObject")
    	Set csv    = fso.OpenTextFile(CSVFile)
    	
    	Do While Not csv.AtEndOfStream
    	
    		idx = idx + 1
    		ReDim Preserve inRow(idx)
    		line = csv.ReadLine()
    		line = Replace(line,""",""","|")
    		line = Replace(line,"""","")
    		inRow(idx) = Split(line,"|")
         
    	Loop
     	
    	CSVArray = inRow
    	
    End Function
    
    CSVArray "d:\scripts\StateCity2.csv"


    \_(ツ)_/


    • Edited by jrv Thursday, January 3, 2019 3:16 PM
    Thursday, January 3, 2019 3:12 PM
  • I think I figured it out.  I needed to include the full file path, even though it is in the same directory.  Thanks for your help!
    Thursday, January 3, 2019 4:10 PM