Page 1 of 2

check for file existence and then condition statement

Posted: Thu Oct 13, 2005 6:51 am
by Cryptid123
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

Posted: Thu Oct 13, 2005 7:25 am
by RayBritton
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

Posted: Thu Oct 13, 2005 9:07 am
by m2j
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

Posted: Thu Oct 13, 2005 9:10 am
by RayBritton
thanks i was struggling with that part too

Posted: Thu Oct 13, 2005 9:36 am
by Nodtveidt
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. :)

Posted: Thu Oct 13, 2005 2:30 pm
by Z!re
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.

Posted: Thu Oct 13, 2005 2:55 pm
by Antoni
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

Re: check for file existence and then condition statement

Posted: Thu Oct 13, 2005 8:19 pm
by moneo
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.
*****

Posted: Fri Oct 14, 2005 12:37 am
by Macric
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.

Posted: Fri Oct 14, 2005 9:19 am
by Guest
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

Posted: Fri Oct 14, 2005 11:47 am
by Seb McClouth
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

Posted: Fri Oct 14, 2005 2:17 pm
by Guest
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.
*****

Posted: Fri Oct 14, 2005 2:20 pm
by moneo
Damn, forgot to log in before above post. :oops:
*****

Posted: Sat Oct 15, 2005 2:51 am
by SebMcClouth
Yes sounds interesting...

grtz

Posted: Sat Oct 15, 2005 12:33 pm
by moneo
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.
*****

Posted: Sun Oct 16, 2005 12:00 pm
by Nodtveidt
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.

Posted: Sun Oct 16, 2005 12:13 pm
by 1000101
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

Posted: Sun Oct 16, 2005 12:51 pm
by Z!re
Nekrophidius wrote:fork you.
Oh.. you.. :oops: :D

Posted: Sun Oct 16, 2005 1:12 pm
by moneo
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.
*****

Posted: Sun Oct 16, 2005 2:55 pm
by {Nathan}
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...