none
No Error on File Read RRS feed

  • Question

  • If I try to do any of the file read operations to a file that is not there I don't get an error.  Even if I look at file.lasterror there isn't an error posted.  Wouldn't it be better if SB put out an error saying something like "File not found"?  I know I can get that particular error if I try to do a filecopy to a file that isn't there.  So, how should I find out if a file has been previously created or not?

    Thanks,

    JR 
    Wednesday, March 4, 2009 4:43 PM
    Answerer

Answers

  • JR, 

    Try the following piece of code. Make sure you replace the filepath and filename with something that exists on your machine; then change the filename to one that doesn't exist and check what happens.

    If (File.GetFiles("C:\Pen Drive Copy Temp","fileArr") = "SUCCESS") Then
      TextWindow.WriteLine("Success")
    Else
      TextWindow.WriteLine("Failure")
    EndIf

    TextWindow.Write("Number of files found = ")
    TextWindow.WriteLine(Array.GetItemCount("fileArr"))

    if(Array.ContainsValue("fileArr", "C:\Pen Drive Copy Temp\Info.txt") = "True") Then
      TextWindow.WriteLine("File exists")
    Else
      TextWindow.WriteLine("File does not exist")
    EndIf

    For i = 1 To Array.GetItemCount("fileArr") 
      TextWindow.WriteLine(Array.GetValue("fileArr", i))
    EndFor

    The TextWindow output will look something like this

    Success
    Number of files found = 3
    File exists
    C:\Pen Drive Copy Temp\Eh8cum4B.pdf
    C:\Pen Drive Copy Temp\Info.txt
    C:\Pen Drive Copy Temp\main.c
    Press any key to continue...

    Stendec.


    Note,
        The filepath and filename in the Array.ContainsValue() method are case sensitive. You may wish to use something like Text.ConvertToUpperCase() on each array item in turn and compare with a corresponding upper case version of your filepath/filename.
    • Proposed as answer by Stendec Wednesday, March 4, 2009 6:56 PM
    • Edited by Stendec Wednesday, March 4, 2009 7:07 PM Added Note
    • Marked as answer by gungan37Editor Sunday, October 21, 2012 7:18 PM
    Wednesday, March 4, 2009 6:54 PM

All replies

  • JR, 

    Try the following piece of code. Make sure you replace the filepath and filename with something that exists on your machine; then change the filename to one that doesn't exist and check what happens.

    If (File.GetFiles("C:\Pen Drive Copy Temp","fileArr") = "SUCCESS") Then
      TextWindow.WriteLine("Success")
    Else
      TextWindow.WriteLine("Failure")
    EndIf

    TextWindow.Write("Number of files found = ")
    TextWindow.WriteLine(Array.GetItemCount("fileArr"))

    if(Array.ContainsValue("fileArr", "C:\Pen Drive Copy Temp\Info.txt") = "True") Then
      TextWindow.WriteLine("File exists")
    Else
      TextWindow.WriteLine("File does not exist")
    EndIf

    For i = 1 To Array.GetItemCount("fileArr") 
      TextWindow.WriteLine(Array.GetValue("fileArr", i))
    EndFor

    The TextWindow output will look something like this

    Success
    Number of files found = 3
    File exists
    C:\Pen Drive Copy Temp\Eh8cum4B.pdf
    C:\Pen Drive Copy Temp\Info.txt
    C:\Pen Drive Copy Temp\main.c
    Press any key to continue...

    Stendec.


    Note,
        The filepath and filename in the Array.ContainsValue() method are case sensitive. You may wish to use something like Text.ConvertToUpperCase() on each array item in turn and compare with a corresponding upper case version of your filepath/filename.
    • Proposed as answer by Stendec Wednesday, March 4, 2009 6:56 PM
    • Edited by Stendec Wednesday, March 4, 2009 7:07 PM Added Note
    • Marked as answer by gungan37Editor Sunday, October 21, 2012 7:18 PM
    Wednesday, March 4, 2009 6:54 PM
  • Stendec,

    Works great!  I knew I could see it by listing them with getfiles, but I didn't think of the Array.ContainsValue.

    Thanks,

    JR
    Thursday, March 5, 2009 12:11 AM
    Answerer