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

Cryptid123

check for file existence and then condition statement

Post by Cryptid123 » Thu Oct 13, 2005 6:51 am

ok this is wat i want to do
if exist "C:\readme.txt" goto condition
condition:
SHell "Del/y C:\readme.txt"


i know that the first line is wronge so please help me correct it i hope u got the idea of wat i am trying to do

RayBritton
Veteran
Posts: 109
Joined: Thu Feb 10, 2005 12:38 pm

Post by RayBritton » Thu Oct 13, 2005 7:25 am

Hi - this works:

Code: Select all

CLS
CHDIR "c:\"
a$ = "readme.txt"
b$ = DIR$(a$)
b$=lcase$(b$)
IF b$ = "readme.txt" THEN deleteit = 1
IF deleteit = 1 THEN
        SHELL "DEL readme.txt"'if you put /y it says invaild switch
        PRINT "Deleted"
ELSE
PRINT "Nothing found"
END IF
another thing:
i don't know if this applys to your os but mine doesn't need /y because it automatically deletes

hope this helps

m2j

Post by m2j » Thu Oct 13, 2005 9:07 am

Alternativly you can open the file for input/output access, and check if the length of the file is 0. If it is, then the file did not previously exist, and if you write nothing to it, will not exist.... So you can tell if the file exists or not....


basicly the code is:

Code: Select all

open filename$ for binary as #1

   if lof(1) = 0 then 'file does not exist
   if lof(1) <> 0 then 'file does exist

close #1
matt

RayBritton
Veteran
Posts: 109
Joined: Thu Feb 10, 2005 12:38 pm

Post by RayBritton » Thu Oct 13, 2005 9:10 am

thanks i was struggling with that part too

User avatar
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt » Thu Oct 13, 2005 9:36 am

Here's a full function for you...note that as it is, it will cause an error on read-only media so it might not be useful for QB4.5.

Code: Select all

Function FileExists(filename As String) As Integer
Dim fh As Integer
fh = Freefile
Open filename For Binary As #fh
If Lof(fh) = 0 Then
  FileExists = False
  Close fh
  Kill filename
Else
  FileExists = True
  Close fh
End If
End Function
Hint: this can be further expanded by way of the ACCESS keyword but I'll leave that up to you to figure out. :)

Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re » Thu Oct 13, 2005 2:30 pm

Nek, it removes 0 lenght files, bad practice.. use DIR$() instead..

It's available in PDS 7.1 and FB


Just because a file has a 0 lenght does not mean it does not exist, it just means the file is empty.. And while not many users have empty files, it's still bad practice to remove any such files should there happen to be one..

Code properly.
I have left this dump.

Antoni
Veteran
Posts: 132
Joined: Wed Jun 15, 2005 3:01 pm
Contact:

Post by Antoni » Thu Oct 13, 2005 2:55 pm

Well, you just want DEL not to mess your screen layout with it's error message if the file does not exist, is'nt?
In windows XP, 2000,( NT?) you can just redirect the error messages to the NUL device.

Code: Select all

SHELL "DEL /y README.TXT >NUL 2>&1"
It will not work in W9x or DOS

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

Re: check for file existence and then condition statement

Post by moneo » Thu Oct 13, 2005 8:19 pm

Cryptid123 wrote:ok this is wat i want to do
if exist "C:\readme.txt" goto condition
condition:
SHell "Del/y C:\readme.txt"
....
Before using any of the posted suggestions, you have to decide whether or not you want to delete zero-length-files which show up as an existing file.
*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.

Macric
Coder
Posts: 34
Joined: Fri Mar 25, 2005 11:11 pm
Location: Mexico

Post by Macric » Fri Oct 14, 2005 12:37 am

Antoni wrote:

Code: Select all

SHELL "DEL /y README.TXT >NUL 2>&1"
It will not work in W9x or DOS
whoops,
in windows9X and Dos 6.22 the command DEL does not have the option '/Y', just the option '/P' asking for confirmation to erase.

Guest

Post by Guest » Fri Oct 14, 2005 9:19 am

Even if you change the option from /y to /p, what will not work is the 2>&1 redirecting stderr to the same place than stdout. Unless you change command.com, you can't redirect stderr in DOS 6.22- W9x so you will always have an error message if file does not exist

Seb McClouth

Post by Seb McClouth » Fri Oct 14, 2005 11:47 am

I'd suggest to use a batch-file instead:

filename.bat (it needs the extenstion bat)

Code: Select all

@ECHO OFF
if  exist %1 goto remove

:no_remove
echo not file to delete...

goto end

:remove
del %1

:end
grtz
Seb

Guest

Post by Guest » Fri Oct 14, 2005 2:17 pm

Seb,

Your batch file will work for the simplest of cases.

However:
1) It will delete zero-length-files whether the user wants this or not.

2) If the file exists and has an attribute of read-only, you'll get an error message.

3) If the % parameter contains the name of a directory, it will attempt to delete the contents of the directory after asking a question.

4) If the % parameter has a path/filename and any part of the path is invalid, the "if exist" command will just assume that it does not exist.

These and other issues can be determined, but it is not a trivial matter. If you're interested, I have a utility program called !BAT which is intended to be run from a batch file, which covers these issues. Let me know.
*****

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

Post by moneo » Fri Oct 14, 2005 2:20 pm

Damn, forgot to log in before above post. :oops:
*****

User avatar
SebMcClouth
Veteran
Posts: 240
Joined: Fri Apr 29, 2005 2:20 am
Location: Inside the Matrix

Post by SebMcClouth » Sat Oct 15, 2005 2:51 am

Yes sounds interesting...

grtz
I know why you're here. I know what you've been doing... why you hardly sleep, why you live alone, and why night after night, you sit by your computer...<br>
Unfortunately, no one can be told what Qbinux is. You have to see it for yourself.

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

Post by moneo » Sat Oct 15, 2005 12:33 pm

SebMcClouth wrote:Yes sounds interesting...
grtz
Seb, If you're interested in the !BAT program, let me know where I can send the executable which is 65k.
*****

User avatar
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt » Sun Oct 16, 2005 12:00 pm

Z!re wrote:Nek, it removes 0 lenght files, bad practice.. use DIR$() instead..
1. 0-length files have zero purpose in a user program, and are otherwise pointless. Anyone who uses 0-length files is a moron and anyone who feels a need to retain 0-length files is an equal moron.
2. Dir$ does not exist in QB4.5. Since the vast majority of QB users use 4.5, I had to write something that works correctly in 4.5.
Z!re wrote:Code properly.
fork you.

1000101

Post by 1000101 » Sun Oct 16, 2005 12:13 pm

This is the function I used in QB:

Code: Select all

Function _MOUSEX ( Pathname As String ) As Integer

   Dim FF As Integer

   FF = FreeFile

   On Error Goto No_File

   Open Pathname For Input As #FF
   Close #FF

   _MOUSEX = 1

   Exit Function

No_File:

   Is_Exist = 0

End Function

Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re » Sun Oct 16, 2005 12:51 pm

Nekrophidius wrote:fork you.
Oh.. you.. :oops: :D
I have left this dump.

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

Post by moneo » Sun Oct 16, 2005 1:12 pm

Nekrophidius wrote:
Z!re wrote:Nek, it removes 0 lenght files, bad practice.. use DIR$() instead..
1. 0-length files have zero purpose in a user program, and are otherwise pointless. Anyone who uses 0-length files is a moron and anyone who feels a need to retain 0-length files is an equal moron.
2. ....
Nek, You're entitled to your opinion regarding zero-length-files, and in general you may be right. However, traditionally these have been used as "switch" files to communicate between one program and another or between programs and batchfiles. These zero-length-files occupy no space in the user's directory and can easily be tested for their existence.
*****

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} » Sun Oct 16, 2005 2:55 pm

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...
Image

Post Reply