Basic File Operations in VB
The basic file operations in VB are :
Name Function
FileCopy Copies a file
Kill Deletes a file
Name Renames/moves a file
To copy a file, you use the FileCopy function, which is very simple:
FileCopy SourceFile, DestinationFile
So,
FileCopy “C:\test.txt”, “C:\testdir\test2.txt”
copies text.txt to C:\testdir and renames it test2.txt
To delete a file, you use the Kill (!) function.
Kill FilePath
So,
Kill “C:\test folder\test.txt”
deletes text.txt from C:\test folder . This does not send it to the recycle bin!
To rename/move file:
Name OldNameandPath As NewNameandPath
So,
Name “C:\test.txt” As “C:\test2.txt”
Renames test.txt as test2.txt, and
Name “C:\test.txt” As “C:\testdir\test2.txt”
moves text.txt to C:\testdir and renames is test2.txt You can also use this function to rename folders.
To see if a file exists, you can use the Dir function. If it returns nothing, then the file does not exist:
If Dir(FileName) = “” Then
‘// file does not exist
End If
