Procedure ExistFile()

AmigaOS specific forum
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Roxxler.

Hi,
PureBasic stellt keine Funktion zur Verfügung, welche prüft ob eine Datei
oder ein Verzeichnis existiert. Hier eine kleine Procedur, die Abhilfe schafft.

There ist no command in PureBasic to check if a file or a directory will exist.
Here a little procedure which will help

Procedure.b ExistFile(path$)
;
; Das Unterprogramm prüft mittels der DOS-Funktion Lock() ob eine
; Datei oder ein Verzeichnis existiert.
; The procedure checks if a given file or directory is present.
; This will be done via the Lock() function of the dos.library.
;
; Paramater: path$ = Zu prüfende Datei oder zu prüfendes Verzeichnis
; the file or directory to check for
; Rückgabe : 0 = FALSE, Datei oder Verzeichnis existiert nicht
; Returns: FALSE, file or directory doesn't exists
; -1 = TRUE, Datei oder Verzeichnis existiert
; TRUE, file or directory is present
;
; Voraussetzungen: Die dos.library muß geöffnet sein (OpenDosLibrary_(version.l)).
; Wird von PureBasic am Programmende wieder geschlossen.
; Requirements: You have to open the dos.library (OpenDosLibrary_(version.l)).
; PureBasic will close the library automatically at the programend.

lock.l=Lock_(path$,-2)
If lock0
UnLock_(lock)
ProcedureReturn -1
Else
ProcedureReturn 0
EndIf
EndProcedure


DosBase.l=OpenDosLibrary_(36)

If DosBase0
ok.b=ExistFile("ram:test.info")
If ok=-1
PrintN("File or directory exists")
Else
PrintN("File or directory doesn't exist")
EndIf
Else
Printn("Cannot open dos.library")
Endif
End
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.
There ist no command in PureBasic to check if a file or a directory will exist.
Here a little procedure which will help
Hi Roxxler, that's not true for files exactly but I know what you mean.

In PureBasic, you can check (more or less) for a file's existence (or if it's damaged) by using the returned state of the OpenFile or ReadFile command.

If you refer to the manual, "If OpenFile(FileNum, "C:\MyFileName.txt") 0

if the returned state is 0, the file doesn't exist or is damaged. If it's not 0, it exists and it's okay to open.

Regards

Fangles
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Better use ReadFile() because OpenFile() will create it if it doesn't exists.

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Fangbeast.
Better use ReadFile() because OpenFile() will create it if it doesn't exists.

Fred - AlphaSND
Yes, I forgot that. Jus trying to show that they both return a 'state' so you can tell if they exist or not :):)

/me hits brain with dead fish

Fangles
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Roxxler.
There ist no command in PureBasic to check if a file or a directory will exist.
Here a little procedure which will help
Hi Roxxler, that's not true for files exactly but I know what you mean.

In PureBasic, you can check (more or less) for a file's existence (or if it's damaged) by using the returned state of the OpenFile or ReadFile command.

If you refer to the manual, "If OpenFile(FileNum, "C:\MyFileName.txt") 0

if the returned state is 0, the file doesn't exist or is damaged. If it's not 0, it exists and it's okay to open.

Regards

Fangles
Yes, you are right. You can check files in this way, but not any existing directories.
Greetings ..

Roxxler
Post Reply