Glock
New Member
Posts: 13
|
Post by Glock on Dec 10, 2023 10:17:04 GMT -5
Hi
I'm working through Carl's Beginning Programming with Liberty Basic book.
He has as an example the following program and suggests running it.
open "myfile.txt" for output as #myHandle
print #myHandle, "Hello"
print #myHandle, "World!"
close #myHandle
end
It should create a .txt file but doesn't. I have tried changing it to .docx but still nothing.
Any ideas on this?
Thanks
Regards
Rob
|
|
honky
Junior Member
Posts: 63
|
Post by honky on Dec 10, 2023 10:41:44 GMT -5
Good morning, Your code is good. It must be recorded by giving it a name and put it in a folder. Then, "Myfile.txt" will be in the folder.
|
|
|
Post by mknarr on Dec 10, 2023 11:44:18 GMT -5
Just for giggles, I tried the program as you used it just to see where myfile.txt was actually saved. I searched the entire hard drive and could not find a file named myfile.txt. I suspect since a folder was not specified, the file was never created. I specifically look in the LB program folder and under my user appdata folder and did a search of the whole hard drive. Nope.
|
|
|
Post by xxgeek on Dec 10, 2023 12:16:00 GMT -5
You are all correct, kind of.
You won't see the file myfile.txt using Windows Explorer. But if you use a Command Prompt to CD into the Liberty appdata folder you will see it in there with a Dir command.
If you use the FolderDialog I edited to see files along with the folders, you will see your file as well.
It is there, but Microsoft has it hidden from the user for some odd reason.
Therefore we need to 'save' our code to file before running it to find the file with Windows Explorer.
But, as you will see in the code below, it is there and can be accessed using explorer from RUN command.
Weird, but true. At least it's true here on Window 10 Pro
Don't save this to file before running it. And be sure to delete any visible 'myfile.txt' files in the appdata folder before running.
print "Found in ";DefaultDir$ open "myfile.txt" for output as #myHandle
print #myHandle, "Hello"
print #myHandle, "World!"
close #myHandle run "explorer ";DefaultDir$;"\myfile.txt" end
Then run this code (Folderdialog) and navigate to the Liberty appdata folder. eg: "c:\Users\YOURUSERNAME\Appdata\Roaming\Liberty Basic v4.5.1"
' caption$ = "Please Select a Folder blah blah blah blah blah blah blah blah blah" call browser$ caption$ print "You selected " : print FolderDialog$ end
'sub to make folder dialog window sub browser$ caption$ global FolderDialog$ dim info$(0, 0) dim folderInfo$(0, 0) ' fs = 10 WindowWidth = 700 WindowHeight = 500'670 UpperLeftX=INT((DisplayWidth-WindowWidth)/2) UpperLeftY=INT((DisplayHeight-WindowHeight)/2) gosub [FolderDlgGetDrives] statictext #folderdlg.selection, "Selection >> ", 40, 505, 95, 15 statictext #folderdlg.caption, caption$, 150, 20, 525, 35 listbox #folderdlg.filelist, fileList$(, [fileSelect], 350, 50, 320, 310 listbox #folderdlg.list, FolderList$(, [FolderDlgSelect], 15, 50, 320, 310 button #folderdlg.default, "OK", [FolderDlgOk], UL, 220, 410, 75, 25 button #folderdlg.back, "< < <", [FolderDlgBack], UL, 10, 10, 60, 30 button #folderdlg.C, "Cancel", [FolderDlgCancel], UL, 395, 410, 75, 25 button #folderdlg.plusfont, "+", [plusFont], UL, 75, 10, 30, 30 button #folderdlg.minusfont, "-", [minusFont], UL, 110, 10, 30, 30 textbox #folderdlg.text, 15, 360, 655, 25 BackgroundColor$ = "lightgray" open "Just Basic FolderDialog" for dialog_modal as #folderdlg #folderdlg, "trapclose [FolderDlgCancel]" #folderdlg.text, "Selected (Drive \ Folder \ File) Path Appears Here" #folderdlg, "font Arial 12 bold" #folderdlg.filelist, "singleclickselect" #folderdlg.list, "singleclickselect" fontsize = 12 wait
[minusFont] fontsize = fontsize - 1 #folderdlg.list, "font arial ";fontsize;" bold" #folderdlg.filelist, "font arial ";fontsize;" bold" wait
[plusFont] fontsize = fontsize + 1 #folderdlg.list, "font arial ";fontsize;" bold" #folderdlg.filelist, "font arial ";fontsize;" bold" wait
[FolderDlgSelect] #folderdlg.list, "selection? temp$" if temp$ <> "" then level = level+1 folder$ = folder$; temp$; "\" #folderdlg.text, folder$ gosub [FolderDlgGetDir] #folderdlg.list, "reload" #folderdlg.list, "select 0" #folderdlg.default "!setfocus" end if wait
[FolderDlgBack] if level > 0 then level = level-1 if level = 0 then folder$ = "" gosub [FolderDlgGetDrives] else i = len(folder$)-1 while mid$(folder$, i, 1) <> "\" and mid$(folder$, i, 1) <> "" i = i-1 wend folder$ = left$(folder$, i) gosub [FolderDlgGetDir] end if #folderdlg.text, folder$ fileList$(0) = " F I L E S" #folderdlg.list, "reload" #folderdlg.filelist, "reload" end if wait
[FolderDlgGetDrives] c = 1 while word$(Drives$, c) <> "" c = c+1 wend redim FolderList$(c) FolderList$(0) = " D R I V E S" for i = 1 to c FolderList$(i) = word$(Drives$, i) next i redim fileList$(0) return
[FolderDlgGetDir] files folder$, info$( s = val(info$(0,0)) t = val(info$(0,1)) redim FolderList$(t) FolderList$(0) = " F O L D E R S" for i = 1 to t FolderList$(i) = info$(i+s, 1) next i
[filesBack] files folder$, "*.*", folderInfo$() numFiles = val(folderInfo$(0, 0)) redim fileList$(numFiles) for x = 1 to numFiles filename$ = folderInfo$(x, 0) fileList$(x) = filename$ next x fileList$(0) = " F I L E S" sort fileList$(), 0 , numFiles #folderdlg.filelist, "reload" return
[fileSelect] #folderdlg.filelist "selection? file$" #folderdlg.text, folder$;file$ wait
[FolderDlgOk] #folderdlg.text, "!contents? FolderDialog$" If right$(FolderDialog$,1) = "\" then if right$(FolderDialog$,2 ) = ":\" then [goAround] FolderDialog$ = left$(FolderDialog$, len(FolderDialog$) - 1) else notice "The Selection was Not a Folder or Drive" : close #folderdlg : wait ' uncomment to allow selection of "FILES" as well as folders. [goAround] end if
[FolderDlgCancel] close #folderdlg end sub
|
|
|
Post by Rod on Dec 10, 2023 12:51:15 GMT -5
Everyone is correct, the file is created but it is created in the users %appdata% folder by default which Windows has annoyingly started to hide from users.
The simplest solution is to create a new directory like c:/basic, save your program there and run it from there. Then Windows will stop hiding it and you will find it easily under c:/basic
|
|
|
Post by xxgeek on Dec 10, 2023 13:37:08 GMT -5
The .bas file 'can' be saved to the appdata folder with no issues if that is desired. File>save or File>save as
Once the file in the IDE is 'saved' and it gets RUN, myfile.txt will be visible in Windows Explorer, but not before.
But, as an 'unsaved' file it will be there, but will be invisible to Windows Explorer even though it can be accessed in the code, as shown above. LB knows it is there and has no problem accessing it. It is Windows Explorer dialog that doesn't see it.
|
|
|
Post by Rod on Dec 10, 2023 14:44:10 GMT -5
The discussion about “save” might be a little confusing for the op. You don’t need to save the .txt file the code you posted with open and close is all that’s needed to open, create, and save the file. It is automatically saved on close.
The save I was discussing was saving your . bas program to c:/ basic folder. By working on the .bas program in c:/basic you avoid all of the %appdata% issues.
|
|
Glock
New Member
Posts: 13
|
Post by Glock on Dec 11, 2023 5:52:15 GMT -5
Thanks for all your help. Much appreciated. I will have a good look at it again today.
|
|
Glock
New Member
Posts: 13
|
Post by Glock on Dec 11, 2023 8:48:44 GMT -5
I unhid the appdata folder and myfile.txt was sitting in there. Thanks again everyone.
|
|
|
Post by xxgeek on Dec 11, 2023 9:11:56 GMT -5
Glock, my point was:
Even when you unhide your hidden files, if you did NOT do a 'save' of the code in the IDE, you will still NOT see the file myfile.txt anywhere... using Windows Explorer.
When code in the IDE has NOT been saved for the first time, there will be NO file named myfile.txt visible. It will be there and can be proved with the Folderdialog code above, but Windows Explorer will not see it, hidden files unchecked or not.
The absolute easiest way to avoid this issue is to save your work prior to running it.
|
|
|
Post by Rod on Dec 11, 2023 10:04:11 GMT -5
Not sure you “need” to save the code, type the code into the ide, run it and the .txt file will be created whether you save the code or not. It may be hidden but it will exist. It is the commands in the code that create the .txt file.
|
|
|
Post by xxgeek on Dec 11, 2023 10:53:28 GMT -5
Not sure you “need” to save the code, type the code into the ide, run it and the .txt file will be created whether you save the code or not. It may be hidden but it will exist. It is the commands in the code that create the .txt file. You're right Rod it is there without a save. But Windows Explorer will NOT see it, whether you have "Show hidden file" checked or unchecked. Run this code without saving it, then go find the file 'Afile.txt' Unhide all files in Explorer. You can't find it, or at least I can't here. Now use the folderdialog posted above and navigate to the Liberty appdata folder. See it now? I do. If you don't do the experiments, you will be "Not sure you “need” to save the code." btw - I was wrong about accessing it. - it must be saved in order to access it as well. (here, but maybe not there where you are)
|
|
|
Post by Rod on Dec 11, 2023 11:45:31 GMT -5
My concern is we may be confusing the beginner to Liberty. The .txt file is created immediately the close statement is executed. It will be immediately accessible. If we opened it and read it in immediately after the close statement it will be found, accessed and read. So within BASIC the file exists.
What you are discussing are Windows issues, hiding files and directories from users, caching file updates and not updating explorer unless it is restarted.
Because Windows is multithreaded and because things are cached rather than immediately written to disc things can look strange.
But in terms of Liberty the file is real, it exists and it can be accessed without issue.
In terms of Windows we have to wait for Windows to decide to write the cache before other elements of Windows will see the file.
Me, I will stick to BASIC :) I think we are both saying the same thing just from a different perspective.
|
|
|
Post by Brandon Parker on Dec 12, 2023 21:42:57 GMT -5
Glock, my point was: Even when you unhide your hidden files, if you did NOT do a 'save' of the code in the IDE, you will still NOT see the file myfile.txt anywhere... using Windows Explorer. When code in the IDE has NOT been saved for the first time, there will be NO file named myfile.txt visible. It will be there and can be proved with the Folderdialog code above, but Windows Explorer will not see it, hidden files unchecked or not. The absolute easiest way to avoid this issue is to save your work prior to running it. Just to jump in here... The OPs question was answered. The resulting "myfile.txt" is created in the %appdata% folder when the code is simply run from a fresh IDE (as in you write it all out or paste it into the IDE and run the code). Looking at the top of the IDE should show you where the DefaultDir$ is pointed to when the IDE is initially opened; this is where any saved files will be unless a different path is specified during execution. After opening a file, the DefaultDir$ remains at the last known location even if you hit the "New File" button until it is subsequently changed by opening a file. xxgeek, I think you might be confusing "myfile.txt" with the "untitled.bak" file. Any code that is run from the IDE without having a parent .BAS file will be saved to the BAK folder as long as the preferences checkbox is selected to do so. In the OPs post, there is no filename given just code. The "myfile.txt" is an output from that code and has no path provided and thus Liberty BASIC writes the file to the DefaultDir$. As Rod has mentioned, this is the %AppData% folder for a fresh LB IDE. This is the way it always works for me on LB4.5.1... {:0) Brandon Parker
|
|
Glock
New Member
Posts: 13
|
Post by Glock on Dec 17, 2023 14:21:18 GMT -5
Thanks for all your help. It's much appreciated.
|
|