Where can i find information about copying files, making directories, looking if a file exists?
Printable View
Where can i find information about copying files, making directories, looking if a file exists?
Your MSDN Library holds all that information, but if you can't find it, I'll share it with you.
The FileCopy statement copies a files from a directory to another directory.
The MkDir statement creates a directory.
The Dir function checks to see whether a file exists or not.
This is how you use them.
FileCopy
VB Code:
FileCopy "C:\MyFile.exe", "C:\MyDir\MyFile.exe"
MkDir
VB Code:
MkDir "C:\MyDir"
Dir (combined with the Len function for speed)
VB Code:
If Len(Dir$("C:\MyFile.exe")) <> 0 Then Msgbox "File exists" Else Msgbox "File does not exist" End If
Also, it is best to use the Dir function to check if a folder exists before using the MkDir statement, because if a folder exists, your program will cause an error.
VB Code:
Private Function CheckDir(ByVal sDir As String) As Boolean CheckDir = CBool(Len(Dir$(sDir, vbDirectory))) End Function Private Sub Command1_Click() If CheckDir("C:\MyDir") Then MsgBox "Directory exists" Else MkDir "C:\MyDir" End If End Sub
Keep in mind that you can also use the Name statement to rename and move files instead of using FileCopy and deleting the files using the Kill statement.
Rename
VB Code:
Name "C:\MyFile.exe" As "C:\YourFile.exe"
Move
VB Code:
Name "C:\MyFile.exe" As "C:\MyDir\MyFile.exe"
If you were to use the FileCopy statement, it would look like this, and would take up more code, probably a bit slower as well.
VB Code:
FileCopy "C:\MyFile.exe", "C:\MyDir\MyFile.exe" Kill "C:\MyFile.exe" 'delete the old remaining file