Difference between revisions of "NL/Documentation/BASIC Guide/Files and Directories (Runtime Library)"

From Apache OpenOffice Wiki
Jump to: navigation, search
(Searching Through Directories)
(Creating and Deleting Directories)
Line 81: Line 81:
 
{{Documentation/VBAnote|De specificaties voor het pad zoals vermeld in <tt>Dir</tt> mogen de tijdelijke plaatsaanduidingen * en ? gebruiken in zowel VBA als in {{OOo}} BASIC. In {{OOo}} BASIC kan de tijdelijke plaatsaanduiding * echter slechts het laatste teken van een bestandsnaam en/of bestandsextensie zijn, wat niet het geval is in VBA. }}
 
{{Documentation/VBAnote|De specificaties voor het pad zoals vermeld in <tt>Dir</tt> mogen de tijdelijke plaatsaanduidingen * en ? gebruiken in zowel VBA als in {{OOo}} BASIC. In {{OOo}} BASIC kan de tijdelijke plaatsaanduiding * echter slechts het laatste teken van een bestandsnaam en/of bestandsextensie zijn, wat niet het geval is in VBA. }}
  
=== Creating and Deleting Directories ===
+
=== Maken en verwijderen van mappen ===
  
{{OOo}} Basic provides the <tt>MkDir</tt> function for creating directories.
+
{{OOo}} BASIC verschaft de functie <tt>MkDir</tt> voor het maken van mappen.
  
 
<source lang="oobas">
 
<source lang="oobas">
Line 89: Line 89:
 
</source>
 
</source>
  
This function creates directories and sub-directories. All directories needed within a hierarchy are also created, if required. For example, if only the <tt>C:\SubDir1</tt> directory exists, then a call
+
Deze functie maakt mappen en sub-mappen. Alle benodigde mappen binnen een hiërarchie worden, indien vereist, ook gemaakt. Bijvoorbeeld: als alleen de map <tt>C:\SubDir1</tt> bestaat, dan maakt de aanroep
  
 
<source lang="oobas">
 
<source lang="oobas">
Line 95: Line 95:
 
</source>
 
</source>
  
creates both the <tt>C:\SubDir1\SubDir2</tt> directory and the <tt>C:\SubDir1\SubDir2\SubDir3</tt> directory.  
+
zowel de map <tt>C:\SubDir1\SubDir2</tt> als de map <tt>C:\SubDir1\SubDir2\SubDir3</tt>.  
  
The <tt>RmDir</tt> function deletes directories.
+
De functie <tt>RmDir</tt> verwijdert mappen.
  
 
<source lang="oobas">
 
<source lang="oobas">
 
RmDir ("C:\SubDir1\SubDir2\SubDir3\")
 
RmDir ("C:\SubDir1\SubDir2\SubDir3\")
 
</source>
 
</source>
 +
Als de map mappen of bestanden bevat worden deze '''ook verwijderd'''. U zou daarom voorzichtig moeten zijn met het gebruik van <tt>RmDir</tt>.
  
If the directory contains sub-directories or files, these are '''also deleted.''' You should therefore be careful when using <tt>RmDir</tt>.
+
{{Documentation/VBAnote|In VBA hebben de functies <tt>MkDir</tt> en <tt>RmDir</tt> alleen invloed op de huidige map. Aan de andere kant kunnen <tt>MkDir</tt> en <tt>RmDir</tt> in {{OOo}} BASIC worden gebruikt om niveaus van mappen te maken of te verwijderen.}}
  
{{Documentation/VBAnote|In VBA, the <tt>MkDir</tt> and <tt>RmDir</tt> functions only relate to the current directory. In {{OOo}} Basic on the other hand, <tt>MkDir</tt> and <tt>RmDir</tt> can be used to create or delete levels of directories.}}
+
{{Documentation/VBAnote|In VBA produceert <tt>RmDir</tt> een foutbericht als een map een bestand bevat. In {{OOo}} BASIC worden de map '''en alle bestanden daarin''' verwijderd. Als u de functie <tt>CompatibilityMode ( true )</tt> gebruikt, zal {{OOo}} BASIC zich gedragen zoals VBA.}}
 
+
{{Documentation/VBAnote|In VBA, <tt>RmDir</tt> produces an error message if a directory contains a file. In {{OOo}} Basic, the directory '''and all its files''' are deleted. If you use the <tt>CompatibilityMode ( true )</tt> function, {{OOo}} Basic will behave like VBA.}}
+
  
 
=== Copying, Renaming, Deleting and Checking the Existence of Files ===
 
=== Copying, Renaming, Deleting and Checking the Existence of Files ===

Revision as of 14:21, 27 January 2013

Book.png


Werken met bestanden is een van de basistaken van een toepassing. De Apache OpenOffice API verschaft een groot scala aan objecten waarmee u kantoordocumenten kunt creëren, openen en aanpassen. Deze worden in detail gepresenteerd in Introductie voor de Apache OpenOffice API. Niettegenstaande dit, in sommige gevallen zult u direct het bestandssysteem moeten benaderen, zoeken in directory's of tekstbestanden bewerken. De runtime bibliotheek van Apache OpenOffice BASIC verschaft verscheidene fundamentele functies voor deze taken.

Template:Documentation/Note

Beheren van bestanden

modus Compatibiliteit

Het argument en de functie CompatibilityMode verschaffen grotere compatibiliteit met VBA, door de werking van bepaalde functies te wijzigen. Het effect on een bepaalde functie wordt, hieronder, bij die functie beschreven.

Als een argument krijgt CompatibilityMode( waarde ) een Booleaanse waarde om de modus in te stellen of uit te schakelen. Als een functie geeft CompatibilityMode() de Booleaanse waarde van de modus terug.

CompatibilityMode( True ) 'modus instellen
CompatibilityMode( False) 'modus uitschakelen
 
Dim bModus as Boolean
bModus = CompatibilityMode()

Zoeken in mappen

De functie Dir in Apache OpenOffice BASIC is verantwoordelijk voor het zoeken door mappen naar bestanden en sub-mappen. Bij het eerste verzoek dient een tekenreeks, die het pad bevat naar de mappen die moeten worden doorzocht, te worden toegewezen aan Dir als diens eerste parameter. De tweede parameter van Dir specificeert het bestand of de map waarnaar moet worden gezocht. Apache OpenOffice BASIC geeft de naam weer van de eerste map die wordt gevonden. Om het volgende item te vinden, zou de functie Dir moeten worden gevraagd zonder parameters. Als de functie Dir geen item meer vindt, geeft het een lege tekenreeks weer.

Het volgende voorbeeld toont hoe de functie Dirkan worden gebruikt om alle bestanden, die zich bevinden in één map, kunnen worden opgevraagd. De procedure slaat de individuele bestandsnamen op in de variabele AlleBestanden en geeft die dan weer in een berichtenvenster.

Sub BestandenTonen
  Dim VolgendBestand As String
  Dim AlleBestanden As String
 
  AlleBestanden = ""
  VolgendBestand = Dir("C:\", 0)
 
  While VolgendBestand  <> ""
    AlleBestanden = AlleBestanden & Chr(13) &  VolgendBestand 
    VolgendBestand = Dir
  Wend
 
  MsgBox AlleBestanden
End Sub

De 0, gebruikt in de tweede parameter in de functie Dir, zorgt er voor dat 0 alleen de namen van bestanden weergeeft en mappen worden genegeerd. De volgende parameters kunnen hier worden gespecificeerd:

  • 0 : geeft normale bestanden weer
  • 16 : sub-mappen

Het volgende voorbeeld is nagenoeg hetzelfde als het voorgaande voorbeeld, maar de functie Dir transporteert de waarde 16 als een parameter, welke de sub-mappen van een map weergeven in plaats van de bestandsnamen.

Sub MappenTonen
  Dim VolgendeMap As String
  Dim AlleMappen As String
 
  AlleMappen = ""
  VolgendeMap = Dir("C:\", 16)
 
  While VolgendeMap <> ""
    AlleMappen = AlleMappen & Chr(13) &  VolgendeMap
    VolgendeMap = Dir
  Wend
 
  MsgBox AlleMappen
End Sub
Documentation note.png VBA : Indien gevraagd in Apache OpenOffice Basic, geeft de functie Dir, die de parameter 16 gebruikt, alleen de sub-mappen van een map weer. In VBA geeft de functie ook de namen van de normale bestanden weer zodat nadere controle is vereist om alleen de mappen op te halen. Bij het gebruiken van de functie CompatibilityMode ( true ) gedraagt Apache OpenOffice BASIC zich als VBA en de functie Dir, die parameter 16 gebruikt, geeft sub-mappen en namen van normale bestanden weer.


Documentation note.png VBA : De in VBA verschafte opties voor het zoeken door mappen, specifiek naar bestanden met de eigenschappen verborgen, systeembestand, archief en volumenaam, bestaat niet in Apache OpenOffice BASIC omdat de overeenkomende functies voor het bestandssysteem niet beschikbaar zijn op alle besturingssystemen.


Documentation note.png VBA : De specificaties voor het pad zoals vermeld in Dir mogen de tijdelijke plaatsaanduidingen * en ? gebruiken in zowel VBA als in Apache OpenOffice BASIC. In Apache OpenOffice BASIC kan de tijdelijke plaatsaanduiding * echter slechts het laatste teken van een bestandsnaam en/of bestandsextensie zijn, wat niet het geval is in VBA.


Maken en verwijderen van mappen

Apache OpenOffice BASIC verschaft de functie MkDir voor het maken van mappen.

MkDir ("C:\SubDir1")

Deze functie maakt mappen en sub-mappen. Alle benodigde mappen binnen een hiërarchie worden, indien vereist, ook gemaakt. Bijvoorbeeld: als alleen de map C:\SubDir1 bestaat, dan maakt de aanroep

MkDir ("C:\SubDir1\SubDir2\SubDir3\")

zowel de map C:\SubDir1\SubDir2 als de map C:\SubDir1\SubDir2\SubDir3.

De functie RmDir verwijdert mappen.

RmDir ("C:\SubDir1\SubDir2\SubDir3\")

Als de map mappen of bestanden bevat worden deze ook verwijderd. U zou daarom voorzichtig moeten zijn met het gebruik van RmDir.

Documentation note.png VBA : In VBA hebben de functies MkDir en RmDir alleen invloed op de huidige map. Aan de andere kant kunnen MkDir en RmDir in Apache OpenOffice BASIC worden gebruikt om niveaus van mappen te maken of te verwijderen.


Documentation note.png VBA : In VBA produceert RmDir een foutbericht als een map een bestand bevat. In Apache OpenOffice BASIC worden de map en alle bestanden daarin verwijderd. Als u de functie CompatibilityMode ( true ) gebruikt, zal Apache OpenOffice BASIC zich gedragen zoals VBA.


Copying, Renaming, Deleting and Checking the Existence of Files

The following call creates a copy of the Source file under the name of Destination:

FileCopy(Source, Destination)

With the help of the following function you can rename the OldName file with NewName. The As keyword syntax, and the fact that a comma is not used, goes back to the roots of the Basic language.

Name OldName As NewName

The following call deletes the Filename file. If you want to delete directory (including its files) use the RmDir function.

Kill(Filename)

The FileExists function can be used to check whether a file exists:

If FileExists(Filename) Then 
  MsgBox "file exists."
End If

Reading and Changing File Properties

When working with files, it is sometimes important to be able to establish the file properties, the time the file was last changed and the length of the file.

The following call returns some properties about a file.

Dim Attr As Integer
Attr = GetAttr(Filename)

The return value is provided as a bit mask in which the following values are possible:

  • 1 : read-only file
  • 16 : name of a directory

The following example determines the bit mask of the test.txt file and checks whether this is read-only whether it is a directory. If neither of these apply, FileDescription is assigned the "normal" string.

Dim FileMask As Integer
Dim FileDescription As String
 
FileMask = GetAttr("test.txt")
 
If (FileMask AND 1) > 0 Then
  FileDescription = FileDescription & " read-only "
End IF
 
If (FileMask AND 16) > 0 Then
  FileDescription = FileDescription & " directory "
End IF
 
If FileDescription = "" Then
  FileDescription = " normal "
End IF
 
MsgBox FileDescription
Documentation note.png VBA : The flags used in VBA for querying the concealed, system file,archived and volume name file properties are not supported in Apache OpenOffice Basic because these are Windows-specific and are not or are only partially available on other operating systems.


The SetAttr function permits the properties of a file to be changed. The following call can therefore be used to provide a file with read-only status:

SetAttr("test.txt", 1)

An existing read-only status can be deleted with the following call:

SetAttr("test.txt", 0)

The date and time of the last amendment to a file are provided by the FileDateTime function. The date is formatted here in accordance with the country-specific settings used on the system.

FileDateTime("test.txt")   ' Provides date and time of the last file amendment.

The FileLen function determines the length of a file in bytes (as long integer value).

FileLen("test.txt")      ' Provides the length of the file in bytes

Writing and Reading Text Files

Apache OpenOffice Basic provides a whole range of methods for reading and writing files. The following explanations relate to working with text files (not text documents).

Writing Text Files

Before a text file is accessed, it must first be opened. To do this, a free file handle is needed, which clearly identifies the file for subsequent file access.

The FreeFile function is used to create a free file handle:

FileNo = FreeFile

FileNo is an integer variable that receives the file handle. The handle is then used as a parameter for the Open instruction, which opens the file.

To open a file so that it can be written as a text file, the Open call is:

Open Filename For Output As #FileNo

Filename is a string containing the name of the file. FileNo is the handle created by the FreeFile function.

Once the file is opened, the Print instruction can create the file contents, line by line:

Print #FileNo, "This is a test line."

FileNo also stands for the file handle here. The second parameter specifies the text that is to be saved as a line of the text file.

Once the writing process has been completed, the file must be closed using a Close call:

Close #FileNo

Again here, the file handle should be specified.

The following example shows how a text file is opened, written, and closed:

Dim FileNo As Integer
Dim CurrentLine As String
Dim Filename As String
 
Filename = "c:\data.txt"            ' Define file name 
FileNo = FreeFile               ' Establish free file handle
 
Open Filename For Output As #FileNo         ' Open file (writing mode)
Print #FileNo, "This is a line of text"      ' Save line 
Print #FileNo, "This is another line of text"   ' Save line 
Close #FileNo                  ' Close file

Reading Text Files

Text files are read in the same way that they are written. The Open instruction used to open the file contains the For Input expression in place of the For Output expression and, rather than the Print command for writing data, the Line Input instruction should be used to read the data.

Finally, when calling up a text file, the eof instruction is used to check whether the end of the file has been reached:

eof(FileNo)

The following example shows how a text file can be read:

Dim FileNo As Integer
Dim CurrentLine As String
Dim File As String
Dim Msg as String
 
' Define filename 
Filename = "c:\data.txt"
 
' Establish free file handle
FileNo = Freefile
 
' Open file (reading mode)
Open Filename For Input As FileNo
 
' Check whether file end has been reached
Do While not eof(FileNo)
  ' Read line 
  Line Input #FileNo, CurrentLine   
  If CurrentLine <>"" then
    Msg = Msg & CurrentLine & Chr(13)
  end if
Loop
 
 
' Close file 
 
Close #FileNo               
Msgbox Msg

The individual lines are retrieved in a Do While loop, saved in the Msg variable, and displayed at the end in a message box.


Content on this page is licensed under the Public Documentation License (PDL).
Personal tools