Why does my program not work?

Trivino Y Garcia Abelardo 0 Reputation points
2023-05-05T05:07:55.4766667+00:00

I wrote a small program in "Micorsoft SmallBasic".
The program should read the contents of a file, select contents, and create a new file with the same contents but with a new file name.

The file is correctly read, and the new filename is correctly constructed.

Yet, nothing is saved in the current directory with the new file name.
What am I doing wrong?

Example of a file: 1.txt

BEGIN:VMSG
VERSION:1.1
X-IRMC-STATUS:
X-IRMC-BOX:INBOX
X-NOK-DT:20101201T030805Z
X-MESSAGE-TYPE:DELIVER
BEGIN:VCARD
VERSION:3.0
N:
TEL:+00000000000
END:VCARD
BEGIN:VENV
BEGIN:VBODY
Date:01.12.2010 03:08:05
END:VBODY
END:VENV
END:VMSG

Example of the program:

For i = 1 To 4
  sourcefile = "E:\vb\" + i + ".txt"
  inhoud = File.ReadContents(sourcefile)
     ymd = File.ReadLine(sourcefile, 5)
     tel = File.ReadLine(sourcefile, 10)
     	TextWindow.WriteLine(inhoud) 'test the read contents
     	TextWindow.WriteLine(ymd + tel) 'test the read line
   
   destinationfile = "E:\vb\" + ymd + tel + ".txt"
    TextWindow.WriteLine(destinationfile) 'test the name of the new file  
	File.WriteContents(destinationfile, inhoud)    

EndFor
Developer technologies | Visual Studio | Debugging
Developer technologies | Visual Studio | Other
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Tianyu Sun-MSFT 34,441 Reputation points Microsoft External Staff
    2023-05-05T08:52:12.3733333+00:00

    Hello @Trivino Y Garcia Abelardo ,

    Welcome to Microsoft Q&A forum.

    First of all, in Windows OS, a file name can’t contain any of the following characters: \ / : * ? " < >

     User's image

    I see that in 1.txt file, line 5 and line 10, : is included. So, I believe the name of the newly generated file may not be set correctly in real folder, but the output in terminal may be correct.

    Second, the main code is included in For i=1 To 4 … EndFor, so the whole process will run for four times. I’m not sure if on your side, you do have four files(1.txt, 2.txt, 3.txt, 4.txt), and they are not empty.

    Please note => File.WriteContents(filePath, contents) => "Opens a file and writes the specified contents into it, replacing the original contents with the new content". You may check if the content was replaced by empty content during the loop.

    Best Regards,

    Tianyu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. Trivino Y Garcia Abelardo 0 Reputation points
    2023-05-06T05:52:25.2266667+00:00

    I tried the new filename with a short program and here are the results.

    FilePath = "E:\vb\X-NOK-DT:20101201T034825ZTEL:+66867259977.txt"
    TextWindow.WriteLine("Write Content = " + File.WriteLine(FilePath, 1, "Shakespeare was a great writer."))
    TextWindow.WriteLine("Append Content = " + File.AppendContents(FilePath, "He wrote many plays."))
    TextWindow.WriteLine("Read Content = " + File.ReadContents(FilePath))
    RESULT: FAILED

    FilePath = "E:\vb\X-NOK-DT20101201T034825ZTEL+66867259977.txt"
    TextWindow.WriteLine("Write Content = " + File.WriteLine(FilePath, 1, "Shakespeare was a great writer."))
    TextWindow.WriteLine("Append Content = " + File.AppendContents(FilePath, "He wrote many plays."))
    TextWindow.WriteLine("Read Content = " + File.ReadContents(FilePath))
    RESULT: SUCCESS

    So, the error is caused by the semicolum ":" in the filename.
    Question is how to format the DestinationFileName without the semicolumn.
    Any tips?

    0 comments No comments

  3. Trivino Y Garcia Abelardo 0 Reputation points
    2023-05-07T03:01:53.32+00:00

    I finished the program.
    Working now.
    The result could be written better, but this is my first attempt at writing a program in Small Basic.
    The problem:

    1. I have about 4,000 ".vmg" files on my computer which I would like to sort by Date + Time + Tel".
      A "
      .vmg* file was a Nokia SMS file.
      The Date+Time+Tel are in each file.
    2. The "*.vmg" files are in Unicode format and have 2 bytes per character (eg.: 65 00).
      The second byte should be omitted in the file name.

    The program is working now, altough some unwanted characters are still in the file name.
    But these characters can be easily removed by a File Rename program.

    For i = 1 To 3600
      lees = "E:\vb\" + i + ".vmg"
      inhoud = File.ReadContents(lees)
      
      ymdold = File.ReadLine(lees, 5)
      ymdnew = ""
       For y = 1 To Text.GetLength(ymdold)
          char = Text.GetSubText(ymdold,y,1)
          charCode = Text.GetCharacterCode(char)
           If (charCode = 00) Then
             ymdnew = ymdnew
           Else
             ymdnew = ymdnew + char
            EndIf
        EndFor  
      ymdnew = text.GetSubText(ymdnew, 10, 16)
      
      
      telold = File.ReadLine(lees, 10)
      telnew = ""
       For t = 1 To Text.GetLength(telold)
          char = Text.GetSubText(telold,t,1)
          charCode = Text.GetCharacterCode(char)
           If (charCode = 00) Then
             telnew = telnew
           Else
             telnew = telnew + char
            EndIf
        EndFor  
      telnew = text.GetSubText(telnew, 5, 13)
    
      result = Text.Append(ymdnew, telnew)
      schrijf = "E:\vb\new\" + result + ".txt"
      
      File.WriteContents(schrijf, inhoud)    
    
    EndFor
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.