check for file existence and then condition statement
check for file existence and then condition statement
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
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
-
- Veteran
- Posts: 109
- Joined: Thu Feb 10, 2005 12:38 pm
Hi - this works:
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
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
i don't know if this applys to your os but mine doesn't need /y because it automatically deletes
hope this helps
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:
matt
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
-
- Veteran
- Posts: 109
- Joined: Thu Feb 10, 2005 12:38 pm
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.
Hint: this can be further expanded by way of the ACCESS keyword but I'll leave that up to you to figure out.
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
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.
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.
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.
It will not work in W9x or DOS
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"
Re: check for file existence and then condition statement
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.Cryptid123 wrote:ok this is wat i want to do
if exist "C:\readme.txt" goto condition
condition:
SHell "Del/y C:\readme.txt"
....
*****
If you are ahead of me, lead.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
If you are behind me, follow.
If you are not doing anything,
Get out of the way.
whoops,Antoni wrote:It will not work in W9x or DOSCode: Select all
SHELL "DEL /y README.TXT >NUL 2>&1"
in windows9X and Dos 6.22 the command DEL does not have the option '/Y', just the option '/P' asking for confirmation to erase.
I'd suggest to use a batch-file instead:
filename.bat (it needs the extenstion bat)
grtz
Seb
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
Seb
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.
*****
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.
*****
- SebMcClouth
- Veteran
- Posts: 240
- Joined: Fri Apr 29, 2005 2:20 am
- Location: Inside the Matrix
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.Z!re wrote:Nek, it removes 0 lenght files, bad practice.. use DIR$() instead..
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.
fork you.Z!re wrote:Code properly.
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
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.Nekrophidius wrote: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.Z!re wrote:Nek, it removes 0 lenght files, bad practice.. use DIR$() instead..
2. ....
*****