none
Download file - network command RRS feed

  • Question

  • I'm trying to follow a tutorial (PowerPoint Slide 3.1 Show what you know of tech net tutorial...)

    I'm trying to download a file, move it to a folder and edit it... But I'm currently stuck at the download stage. I can download something, but when I add it to the folder it's just a temp file and not the actual TXT file that's associated with the link that I'm referencing. 

    here's the code:

    [code]

    TextWindow.WriteLine("Input name") 
    InputName = TextWindow.Read()

    directory = File.CreateDirectory("C:\Users\George Lenton\Desktop\" + InputName)

    DLFile = Network.DownloadFile("https://androidnetworktester.googlecode.com/files/1mb.txt")

    NuPath = File.CopyFile(DLFile, "C:\Users\George Lenton\Desktop\" + InputName)

    [/code]

    It currently puts a temp file in the folder that the user names. I'm not sure if I'm doing something wrong or whether Small Basic just isn't going to download this file type? 

    Thanks 

     
    Friday, November 29, 2013 7:06 PM

Answers

  • No I just used my C:\temp folder because I don't have the same desktop location, but this works for me too.

    TextWindow.WriteLine("Input name") 
    InputName = TextWindow.Read()
    
    DirName = "C:\Users\Steve\Desktop\"+InputName
    directory = File.CreateDirectory(DirName)
    
    DLFile = Network.DownloadFile("https://androidnetworktester.googlecode.com/files/1mb.txt")
    
    status = File.CopyFile(DLFile,DirName + "/myFile.txt")
    TextWindow.WriteLine(status)

    Friday, November 29, 2013 9:52 PM
    Moderator
  • Both codes seem to work as expected,  the File.GetFiles does just that and if there is only one file then it is held in the first element of the files array "files[1]".  I guess the reason it was done this way was that the file was copied to an user input file folder and the full name was easily known by the code (just a temp file), so it had to just get the files in the folder and assume the first file was the one downloaded.

    Both versions are fine, the array one is maybe easier to extend to the case where there are several files, but either is just as good since they do the same thing and both are quite clear to read. 

    I prefer yours since it doesn't assume there is only one file in the copy folder and its extension is set correctly.

    Saturday, November 30, 2013 10:18 AM
    Moderator

All replies

  • The following code works correctly for me, the TextWindow outputs SUCCESS and the file is downloaded to my temp folder.

    DLFile = Network.DownloadFile("https://androidnetworktester.googlecode.com/files/1mb.txt")
    status = File.CopyFile(DLFile,"C:/temp/1mb.txt")
    TextWindow.WriteLine(status)

    The file is downloaded to a temp folder with a .tmp extension at location DLFile.

    The File.CopyFile needs to be given the full path (and extension) of where you want to save it - this will save the file with the extension you give.


    Friday, November 29, 2013 7:41 PM
    Moderator
  • Right, so temp (tmp) files can hold other files (like a text files) within them then..? And kind of need to be 'unzipped'?

    So If I follow the code you've but change

    status = File.CopyFile(DLFile,"C:/temp/1mb.txt")
    

    to 

    status = File.CopyFile(DLFile,"C:\Users\George Lenton\Desktop\" + InputName)

    I get the same result as before - a new folder on the desktop with a tmp file in it.

    Friday, November 29, 2013 8:54 PM
  • Yes, but it is not some sort of unzipping.

    The file contents is unchanged, but if you change the extension MS treats it differently, i.e a file with extension jpg is treated as a picture and extension txt is treated as a text file.

    If you rename a text file as a jpg MS will try to open it as a picture and fail.  To use a file you need to give it the right extension so MS knows what to do with it.

    Friday, November 29, 2013 9:13 PM
    Moderator
  • Argh, what was confusing me is the actual file is full of x's "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx..." etc etc.... I thought that was a big error message :/

    So - I've got the file to copy to my desktop now thank you :) 

    But as it stands the program creates a file on the desktop and a folder on the desktop, it doesn't put the file in the folder. 

    I've altered the code, but it still just makes a folder and a file on the desktop, it doesn't put / move the file into the folder. 

    TextWindow.WriteLine("Input name") 
    InputName = TextWindow.Read()

    DirName = "C:\Users\George Lenton\Desktop\" + InputName

    directory = File.CreateDirectory(DirName)

    DLFile = Network.DownloadFile("https://androidnetworktester.googlecode.com/files/1mb.txt")

    status = File.CopyFile(DLFile,DirName + ".txt")
    TextWindow.WriteLine(status)

    TextWindow.WriteLine("Move file = " + File.CopyFile(status, directory))



    Friday, November 29, 2013 9:25 PM
  • The following works for me.

    TextWindow.WriteLine("Input name") 
    InputName = TextWindow.Read()
    
    DirName = "C:\temp\"+InputName
    directory = File.CreateDirectory(DirName)
    
    DLFile = Network.DownloadFile("https://androidnetworktester.googlecode.com/files/1mb.txt")
    
    status = File.CopyFile(DLFile,DirName + "/myFile.txt")
    TextWindow.WriteLine(status)

    2 things to note:

    1] The return from File.CopyFile is just "SUCCESS" or "FAILED" and it redoes the copy so don't do it twice.

    2] Make sure your paths are properly formed, eg note the / I added before myFile.txt - the file is inside the folder.

    Friday, November 29, 2013 9:36 PM
    Moderator
  • Ah right, I can see how it makes more sense to use the DirName = "C:\Temp\" now, seeing as that's the default and it's less to type... 

    But I now have a folder in the temp folder that contains the file, which is ace... But I don't know how to get this to the desktop... 

    Should I be moving the folder after it's all downloaded or just setting desktop as the path to start with? 

    thanks

    Friday, November 29, 2013 9:45 PM
  • No I just used my C:\temp folder because I don't have the same desktop location, but this works for me too.

    TextWindow.WriteLine("Input name") 
    InputName = TextWindow.Read()
    
    DirName = "C:\Users\Steve\Desktop\"+InputName
    directory = File.CreateDirectory(DirName)
    
    DLFile = Network.DownloadFile("https://androidnetworktester.googlecode.com/files/1mb.txt")
    
    status = File.CopyFile(DLFile,DirName + "/myFile.txt")
    TextWindow.WriteLine(status)

    Friday, November 29, 2013 9:52 PM
    Moderator
  • Ah yes it works now! I wasn't using a variable for the "directory = file.CreateDirectory" part of the program......(Which is odd seeing as it's in the code above... I must have changed it for some reason)

    Thank you for helping 

    I still find the "Failed" flag a bit disconcerting, even though the file is in the folder... I tried putting in a program delay to give it time but it still comes up failed - is that just how it goes? 

    Friday, November 29, 2013 9:53 PM
  • I don't get a failed flag - assuming you are only doing the copy once it may be an issue if the file is opened in notepad or permissions issue. What code gives the failed.
    Friday, November 29, 2013 10:01 PM
    Moderator
  • I was trying to copy the file to another location after the folder and file had been created....

    It's working now though - If I understand correctly :

    I have to label the file ".txt" because the file is a txt file and by default the Network object downloads thing's to tmp files, and by labeling it he file is forced to remain in it's original state. 

    I think I was getting confused and trying to copy the files when the paths should have been set up properly I think.... Everything else seems to make sense to me now ! THanks :)

    Saturday, November 30, 2013 12:35 AM
  • Hi, really sorry to keep on with something this simple but I've just had a look at the solution and obviously I don't quite understand things (although what I have written works and does what was asked!)

    So here is the solution that is given :

    TextWindow.Write("Enter the name of the new directory: ")
    DirectoryName = TextWindow.Read()
    File.CreateDirectory(DirectoryName)
    filepath = "https://androidnetworktester.googlecode.com/files/1mb.txt"
    downloadpath = Network.DownloadFile(filepath)
     
    If File.CopyFile(downloadpath, DirectoryName) = "SUCCESS" Then  
      TextWindow.WriteLine("File has been downloaded from the network and copied to: " + DirectoryName)
      files = File.GetFiles(DirectoryName)        
      TextWindow.WriteLine("This is the content in the file: ")   
      TextWindow.WriteLine(File.ReadContents(files[1])) 
      TextWindow.Write("Enter data to be added in the file:")
      AppendedData = TextWindow.Read()
      File.AppendContents(files[1]," " + AppendedData)
      TextWindow.WriteLine("File content after adding data is as follows: ")   
      TextWindow.WriteLine(File.ReadContents(files[1])) 
    EndIf
    

    Here is the code that I wrote with your help 

    TextWindow.WriteLine("Input name")
    
    InputName = TextWindow.Read()
    
    DirName = "C:\Users\George Lenton\Desktop\"+InputName
    
    directory = File.CreateDirectory(DirName)
    
    DownloadedFilename = DirName+"\myfile.txt"
    
    DLFile = Network.DownloadFile("https://androidnetworktester.googlecode.com/files/1mb.txt")
    
    status = File.CopyFile(DLFile,DownloadedFilename)
    
    TextWindow.WriteLine(status)
    
    TextWindow.WriteLine("Contents :")
    
    TextWindow.WriteLine(""+ File.ReadContents(DownloadedFilename))
    
    TextWindow.WriteLine("Anything you would like to add to this?")
    
    AddToThis = TextWindow.Read()
    
    NewFile = File.WriteContents(DownloadedFilename, AddToThis)
    
    Program.Delay(3000)
    
    TextWindow.Clear()
    
    TextWindow.WriteLine("Here's the contents")TextWindow.WriteLine(""+ File.ReadContents(DownloadedFilename))
    
    
    

    So the main difference seems to be that they have used an IF function that I haven't. Also - they didn't rename their file so It remains a .tmp file in the created folder. 

    Have have recalled the files variable using "files[1]" - I'm not sure why they've done that? Appears to look like an array but when there's only one "files" I don't see / get why this is. 

    What's noticeable is that they've used 5 variables where as I've used 7 - so I guess theirs is neater. 



    • Edited by TeaWatch Saturday, November 30, 2013 1:00 AM code blocks...
    Saturday, November 30, 2013 12:57 AM
  • Both codes seem to work as expected,  the File.GetFiles does just that and if there is only one file then it is held in the first element of the files array "files[1]".  I guess the reason it was done this way was that the file was copied to an user input file folder and the full name was easily known by the code (just a temp file), so it had to just get the files in the folder and assume the first file was the one downloaded.

    Both versions are fine, the array one is maybe easier to extend to the case where there are several files, but either is just as good since they do the same thing and both are quite clear to read. 

    I prefer yours since it doesn't assume there is only one file in the copy folder and its extension is set correctly.

    Saturday, November 30, 2013 10:18 AM
    Moderator