Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Visual Basic >> Snippet

13 Files / Folders


'Files ===========================
'VB can access files as text or as binary data
' generally text files are separated into lines - string data followed by a pair of LF+CR characters
' binary files have no such LF+CR separator

'basic functions
Name   rename a file
Kill   delete a file
FileCopy  copy a file To a New location
FileLen    Get length of a file In bytes
FileDateTime   Get Date / Time file was last modified
GetAttr    Get Readonly / system / hidden / directory / archive Attribute of a file Or folder
SetAttr   Sets Readonly / system / hidden / archive Attribute of a file Or folder

'Open - most important VB function for reading and writing files
' files can be opened in 4 modes - append, binary, input, output, random

'Input - error if file does not exist
Open "myfile.txt" For Input As #1
 Line Input #1, temp  'read a line of text at a time
  Input #1, a,b,c   'reads list of variables
Close #1

'Example of reading text file from start to end
Open "myfile.txt" For Inpu As #1
While Not EOF(1)
 Line Input #1, temp
Wend

'output - creates file if it does not exist.  erases the file before creating it.
Open "myfile.txt" For Output As #1
  Print #1, "information"    'prints data into file. auto-inserts LF+CR
  Write #1, "dog" , "cat"    'prints data into file.  quotes around strings, commas between items.  auto-inserts LF+CR
Close #1

'binary -
Open "myfile.txt" For Binary As #1
  Get #1, 32,  i
Close #1

'append - text mode.  write only.  writing starts at end of file
Open "myfile.txt" For Append As #1
  Put #1, 128, j
Close #1

'random - reads fixed lenght data, but may be text or binary
Open "myfile.txt" For Append As #1
  Get #1, 32, MyUDT  'reads the 32nd fixed-length record
Close #1

'Folders========================
'basic functions
ChDir  Change directory
MkDir  Make directory
RmDir  Remove directory
ChDrive Change drive
CurDir   Return current directory