Common Tasks
In addition to reading and writing files, you also often need to manage the files on a computer. The most common tasks are listed in Table 3-15.
Table 3-15. Common tasks for Visual Basic’s intrinsic file functions
Task |
Function |
Comments |
---|---|---|
Check if file exists |
|
Also used to list files in a folder. |
Delete a file |
|
Deletes a file if it is not locked or read-only. |
Get the current folder |
|
Excel may change the current folder when a workbook is saved or opened by the user. |
Change current folder |
|
You can use characters like |
Change current drive |
|
Only the first letter from the argument is used. |
Create a folder |
|
May include path specifiers like |
Delete a folder |
|
Folder must be empty before it can be deleted. |
Get/change file attributes |
|
File attributes include hidden, read-only, archive. |
Make a backup copy |
|
Copies an existing file to a new filename. |
Rename a file |
|
Changes a filename. |
In general, it is not a good idea to get the current folder (CurDir
) or change the current folder (ChDir
) from Visual Basic when working with Excel because saving or opening a file from the Excel user interface may subsequently change the current folder. It is a better practice to use the path properties provided by Excel objects when working with folders in Excel.
For example, the following code ...
Get Programming Excel with VBA and .NET now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.