none
My array should contain files in a directory but when I ask it if it does, it says false to everything in the directory RRS feed

  • Question

  • I enter all file names in the directory into the deleter but it says they don't exist. WHY?!! PLEASE END MY SUFFERING!!!
    Sub del
      delfiles = File.GetFiles("C:\Write Out Text Files")
      Go:
      TextWindow.WriteLine("Enter file to delete.")
      filedel = TextWindow.Read()
      If (Array.ContainsValue("delfiles",filedel) = "True") Then
        File.DeleteFile("C:\Write Out Text Files\" + filedel)
        Goto done
      EndIf
      TextWindow.WriteLine("Sorry, try again.")
      Goto Go
      done:
      TextWindow.WriteLine("More to delete?(y/n)")
      delretry = TextWindow.Read()
      If (delretry = "y") Then
        Goto Go
      EndIf
      TextWindow.WriteLine("Do more?(y/n)")
    EndSub
    *"delfiles" not sure about speechmarks but it makes no difference to my error.
    • Changed type abish Tuesday, August 4, 2009 8:41 PM
    • Edited by abish Tuesday, August 4, 2009 8:46 PM
    Tuesday, August 4, 2009 8:32 PM

Answers

  • The reason is that filedel is just the filename entered by the user, while delfiles is an array of files containing the path as well, hence filedel is is not an exact match inside the array delfiles.

    Also delfiles is an array so it should not be inside quotes.

    Try the following in your code to see this:

      TextWindow.WriteLine(delfiles)
      TextWindow.WriteLine(filedel)
      If (Array.ContainsValue(delfiles,filedel) = "True") Then

    Here is an alternative with the code I used to test what you are doing.

    Be very careful with the DeleteFile command!

    delretry = "y"
    While (delretry = "y")
      del()
      TextWindow.WriteLine("More to delete?(y/n)")
      delretry = TextWindow.Read()
    EndWhile

    Sub del
      delfiles = File.GetFiles("C:\Write Out Text Files")
      For i = 1 To Array.GetItemCount(delfiles)
        TextWindow.WriteLine(i+" : "+delfiles[i])
      EndFor
      TextWindow.WriteLine("Enter file number to delete.")
      ifiledel = TextWindow.ReadNumber()
      If (ifiledel > 0 And ifiledel <= Array.GetItemCount(delfiles)) Then
        File.DeleteFile(delfiles[ifiledel])
      Else
        TextWindow.WriteLine("Sorry, try again.")
      EndIf
    EndSub

    • Marked as answer by abish Wednesday, August 5, 2009 12:21 PM
    Tuesday, August 4, 2009 9:10 PM
    Moderator
  • Hi Abish,

    filedel is whatever you enter through your keyboard from the TextWindow, so it would be in a form of a file name, because your next line is suggesting that when you deleting the file, you are adding the folder name.

                   filedel = TextWindow.Read()
                     If (Array.ContainsValue("delfiles",filedel) = "True") Then
                     File.DeleteFile("C:\Write Out Text Files\" + filedel)

    On the other hand, delfile is an array. So when you're tying find a file name in a string "delfile", it's always false.
    That's why you're never able to get inside the File.DeleteFile statement.

    Either way, litdev code is safer than what you have. it would bypass the scenario where user enter the whole path.
    Suppose you have a file called test.txt inside C:\Write Out Text Files\ folder, such that delfile array have an entry of C:\Write Out Text Files\test.txt, but when user enter "C:\Write Out Text Files\test.txt", it would not find "C:\Write Out Text Files\C:\Write Out Text Files\test.txt" to delete, since your code is adding the folder name again in the DeleteFile operation.

    Hopefully, you can learn both, why your code is not working and how to write securer code, not just finding a snippet that would make your program works. :) 
    • Marked as answer by abish Wednesday, August 5, 2009 2:20 PM
    Wednesday, August 5, 2009 12:59 PM

All replies

  • The reason is that filedel is just the filename entered by the user, while delfiles is an array of files containing the path as well, hence filedel is is not an exact match inside the array delfiles.

    Also delfiles is an array so it should not be inside quotes.

    Try the following in your code to see this:

      TextWindow.WriteLine(delfiles)
      TextWindow.WriteLine(filedel)
      If (Array.ContainsValue(delfiles,filedel) = "True") Then

    Here is an alternative with the code I used to test what you are doing.

    Be very careful with the DeleteFile command!

    delretry = "y"
    While (delretry = "y")
      del()
      TextWindow.WriteLine("More to delete?(y/n)")
      delretry = TextWindow.Read()
    EndWhile

    Sub del
      delfiles = File.GetFiles("C:\Write Out Text Files")
      For i = 1 To Array.GetItemCount(delfiles)
        TextWindow.WriteLine(i+" : "+delfiles[i])
      EndFor
      TextWindow.WriteLine("Enter file number to delete.")
      ifiledel = TextWindow.ReadNumber()
      If (ifiledel > 0 And ifiledel <= Array.GetItemCount(delfiles)) Then
        File.DeleteFile(delfiles[ifiledel])
      Else
        TextWindow.WriteLine("Sorry, try again.")
      EndIf
    EndSub

    • Marked as answer by abish Wednesday, August 5, 2009 12:21 PM
    Tuesday, August 4, 2009 9:10 PM
    Moderator

  • Because suppose you have a test.txt file, in the if statement, you are comparing string "delfiles" to the entry. Meanwhile the array contain "C:\\Write Out Text Files\\test.txt"

    try to modify the if statement to

    If (Array.ContainsValue(delfiles, "C:\Write Out Text Files\" + filedel) = "True")

    Tuesday, August 4, 2009 9:11 PM
  • Khumara

    filedel is an array not a path, it doesn't need it. Thanks anyway
    • Edited by abish Wednesday, August 5, 2009 12:24 PM unclear who refered to
    Wednesday, August 5, 2009 12:22 PM
  • Hi Abish,

    filedel is whatever you enter through your keyboard from the TextWindow, so it would be in a form of a file name, because your next line is suggesting that when you deleting the file, you are adding the folder name.

                   filedel = TextWindow.Read()
                     If (Array.ContainsValue("delfiles",filedel) = "True") Then
                     File.DeleteFile("C:\Write Out Text Files\" + filedel)

    On the other hand, delfile is an array. So when you're tying find a file name in a string "delfile", it's always false.
    That's why you're never able to get inside the File.DeleteFile statement.

    Either way, litdev code is safer than what you have. it would bypass the scenario where user enter the whole path.
    Suppose you have a file called test.txt inside C:\Write Out Text Files\ folder, such that delfile array have an entry of C:\Write Out Text Files\test.txt, but when user enter "C:\Write Out Text Files\test.txt", it would not find "C:\Write Out Text Files\C:\Write Out Text Files\test.txt" to delete, since your code is adding the folder name again in the DeleteFile operation.

    Hopefully, you can learn both, why your code is not working and how to write securer code, not just finding a snippet that would make your program works. :) 
    • Marked as answer by abish Wednesday, August 5, 2009 2:20 PM
    Wednesday, August 5, 2009 12:59 PM
  • Thanks, I thought it only had the test.txt. I look at litdev's sub of it and it does store the whole thing.
    Wednesday, August 5, 2009 2:22 PM
  • thanks litdev, ive used that idea for my reader too. look at it import SLV245
    Wednesday, August 5, 2009 2:54 PM
  • sorry khumara, I tried your way and it just didn't do it. thanks anyway
    Wednesday, August 5, 2009 2:55 PM
  • abish,

    Khumara's comments and method are correct and I suggest you also investigate why his suggestions are not working for you - they should.

    If you test by adding TextWindow.WriteLine statements you can follow what your code is doing and why it is not doing what you want.  This is debugging and is one of the most important programming skills - how to systematically test your code and determine what is not working and why.  It can even be fun!  This skill takes practice and experience and is every bit as important as writing the code in the first place.

    As a point in your program, this if will never be true, even if "C:\ Write Out Text Files" exits, even when you remove the "! ".  I put a click sound that never happens to test this.

    folder = File.GetDirectories("C:\")
    If (folder = "Write Out Text Files! ") Then
      Sound.PlayClickAndWait()
      Goto Start
    EndIf


    Wednesday, August 5, 2009 6:24 PM
    Moderator
  • Weird, I will try to implement them again but I'm not sure it will work, but I will try. I cannot get the click as the music & sound download isn't working anymore, unless you have a different location, the only one I know is Steve's SkyDrive, although I can't find it again now. 
    Wednesday, August 5, 2009 7:51 PM
  • litdev
    your first section exposed the problem and I have solved it now with (filedel = "C:\Write Out Text Files\" + Filedel)
    Wednesday, August 5, 2009 8:00 PM
  • The Sound.PlayClickAndWait() command is part of standard SB and does not need any extension.  I only used it as a debugging method to show whether the if statement was being called or not while debugging.  It doesn't sound because the if is never true even if the directory is present because folder is an array of directories and is never equal to "Write Out Text Files" or even "C:\Write Out Text Files" : it may contain the latter as one of its array elements, but this is different to being equal to it - hence using TextWindow.WriteLine and the Sound statement can help to debug this.

    Try using 'Array.ContainsValue(folder ,"C:\Write Out Text Files")' in the if statement.

    Try using the Sound command on its own to make sure it is working for you - perhaps you have your volume on mute.

    The Sound and Music is an extension not needed here, but it is at: (google 'Small Basic Wiki')

    http://cid-92f7df0e57232aba.skydrive.live.com/browse.aspx/.Public/SmallBasic/SpeechMusic


    There was a problem recently dowloading the dll so I bundled the required files in a zip - post on my skydrive if you have problems with this.
    Wednesday, August 5, 2009 8:14 PM
    Moderator