check for file existence and then condition statement

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Nathan1993 wrote:no space? Actually, the file name and the recognition in the file system takes up space... so it DOES take up space, just very little...
Read my lips: "no space in the user's directory."
*****
cryptid
Newbie
Posts: 9
Joined: Thu Aug 18, 2005 2:57 am
Contact:

sorry

Post by cryptid »

actually all i wanted to do is check for a file and then append to it
i know how it is done in Batch:
======================================
if exist "C:\myfile.txt" goto append
if exist "C:\WiNDOWS\myfile.txt" goto appendix

:append
Echo Hi this lineis being putin by cryptid>>C:\myfile.txt
exit

:append1
Echo hahahahahahahahahahahahahaha>>C:\WINDOWS\myfile.txt
exit
======================================
So now how i do the same in Qbasic
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

easy.

Code: Select all

SHELL "yourbatchfilegoeshere.bat"
Image
cryptid
Newbie
Posts: 9
Joined: Thu Aug 18, 2005 2:57 am
Contact:

Post by cryptid »

Nathan1993 wrote:easy.

Code: Select all

SHELL "yourbatchfilegoeshere.bat"
well i know i can use shell but i want the program to be 100% Qbasic i dont want to generate batch files( i feel it is a bit messy) so can the same batch file be translated into QBasic i mean translate the above batch file to QB code
User avatar
{Nathan}
Veteran
Posts: 1169
Joined: Thu Aug 19, 2004 6:08 pm
Location: The wetlands of central Ohio, USA
Contact:

Post by {Nathan} »

Code: Select all

OPEN "C:\myfile.txt" FOR APPEND AS #1
IF NOT LOF(1) = 0 THEN PRINT #1, "HI this is a LINE lol IM 1337! not"
CLOSE #1
OPEN "C:\windows\mespecial.txt" FOR APPEND AS #1
IF NOT LOF(1) = 0 THEN PRINT #1, "hEhEhEhE! im specialer than that 1337 guy!"
untested, but should work. been so long since I did anything with files...
Image
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Nathan1993 wrote:

Code: Select all

OPEN "C:\myfile.txt" FOR APPEND AS #1
IF NOT LOF(1) = 0 THEN PRINT #1, "HI this is a LINE lol IM 1337! not"
CLOSE #1
OPEN "C:\windows\mespecial.txt" FOR APPEND AS #1
IF NOT LOF(1) = 0 THEN PRINT #1, "hEhEhEhE! im specialer than that 1337 guy!"
untested, but should work....
Excellent, Nath. I think that's what cryptid wants.
However, there is a little suble difference between how his batchfile works and your code. If either file was a zero-length-file, the batchfile would treat it as existing, and append a record. In your code, if either file was zero-length, your code would not append a record. I don't think he cares about zero-length-files, so your solution will work fine.


NOTE: You other guys may want to notice that the LOF function is not only for files opened as binary. The manual says: "When a file is opened in any mode, the LOF function returns the size of the file in bytes." I tried it and it works.
*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
matt

Post by matt »

yeah, man, i've been meaning to ask you when this came up:

What kind of programs zero length files/is there a stanard practice for implimenting them (eg. .zlf or some shit like that?).

In otherwords: Is it common practice to generate .bmp/.txt/.mpeg(you know, standard file extention) zero length files?

or is it more like any unusual file extension/lack thereof...

Or is there a naming system (like 000.000 )?

matt
MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows »

To the best of my knowledge, the main reason was so that two processes, piped together (as linux can do it best) could listen for each other this way without needing to communicate with each other per se.

Program1 for example would perform it's task...once it was done it would create a zero length file just so that program 2 would know it was now time (by finding that file in that folder) to start performing it's task. for example. I remember hearing that reason quite often. Today you can do that with task handling...but back then, when these files were used, inter process communication either didn't exist or no one knew how to use them efficiently so they did this instead. but I think they didn't exist period back then. :-).
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
Antoni
Veteran
Posts: 132
Joined: Wed Jun 15, 2005 3:01 pm
Contact:

Post by Antoni »

In the ugly and powerless batch scripting language of DOS and W9x, zero length files were used by the more 1337 programmers to overcome the limitations of the language.
Zero length files made good flags. And I remember a way to separe a file name from its extension using a dummy file and REN...

I must add those files were duly erased before the .BAT file ended.
matt

Post by matt »

right, so it was more of a hack (unless you were using parallel processing) then a standard programing practice... Good to know I'm not fork anthing major up then...

matt
moneo
Veteran
Posts: 451
Joined: Tue Jun 28, 2005 7:00 pm
Location: Mexico City, Mexico

Post by moneo »

Antoni wrote:In the ugly and powerless batch scripting language of DOS and W9x, zero length files were used by the more 1337 programmers to overcome the limitations of the language.
Zero length files made good flags. And I remember a way to separe a file name from its extension using a dummy file and REN...

I must add those files were duly erased before the .BAT file ended.
Antoni, I agree with most of what you say, but consider the following. ERRORLEVEL codes are the most common flags used for communicating between programs and batchfiles, although many programmers are not familiar with generating these errorlevel codes. For example, if you use a batchfile to compile your QuickBasic programs, you can do "IF ERRORLEVEL 1" after the BC (compiler) to test for any compile errors and avoid doing the LINK.

A batchfile can invoke a program and test for an ERRORLEVEL, but a subsecuent program cannot. So, if someone wrote a program that needed to communicate something like "got an error" to the next program, he could use a zero-length-file as a flag or switch.

Yes, these zero-level-files should be duly erased after being tested, but in many instances they are not, and appear in the user's directory.
*****
Post Reply