VBA Code To Copy, Move, Delete and Manage Files
VBA Code To Copy, Move, Delete and Manage Files
com/vba-code-to-copy-move-delete-and-manage-files/
Contents
1.Check if a file exists
2.Rename a file
3.Moving a file
4.Copying a file
5.Delete files
6.Get file attributes
The code below will display True or False in a message box to indicate if the file
exists.
Sub CheckIfFileExists()
End Sub
The code below uses an If statement to carry out different actions depending on if
the file does or does not exist.
Sub PerformActionIfFileExists()
filePath = "C:\Users\marks\Documents\Folder\FileName.xlsx"
Else
End If
End Sub
When regularly checking for the existence of files, it can be easier to have a
reusable function within a Module to be called upon when required.
End Function
The code below shows how to call the reusable function as part of an If statement.
Sub UseTheFunction()
myFile = "C:\Users\marks\Documents\Folder\FileName.xlsx"
End If
End Sub
Rename a file
The code below renames a file.
Sub RenameAFile()
'Rename a file
Name "C:\Users\marks\Documents\Folder\CurrentFileName.xlsx" _
As "C:\Users\marks\Documents\Folder\NewFileName.xlsx"
End Sub
If the target filename is already an existing file, the code will error.
Therefore, it is good practice to check if the source and target file names are
already in use.
Moving a file
The code to move a file is the same syntax as the code to rename a file.
Sub MoveAFile()
'Move a file
Name "C:\Users\marks\Documents\FileName.xlsx" As _
"C:\Users\marks\Documents\New Folder\FileName.xlsx"
End Sub
Copying a file
Copying a file retains the existing file, but creates a duplicate version of it in
a new location.
Sub CopyAFile()
'Copy a file
FileCopy "C:\Users\marks\Documents\Folder\Original File.xlsx", _
"C:\Users\marks\Documents\New Folder\Copied File.xlsx"
End Sub
If the target filename is already an existing file, the code will error.
Therefore, it is good practice to check if the source and target file names are
already in use.
Delete files
Deleting files removes them completely. Files deleted using VBA are not sent to
the recycle bin, and since there is no undo functionality it can be quite
dangerous. Take extra care to ensure the code does what you expect it to.
Sub DeleteSpecificFile()
End Sub
The code below deletes files using a wildcard. In this circumstance, it deletes
all files with a .xlsx file extension.
Sub DeleteFilesWithWildcards()
End Sub
The example below deletes all the files in a folder by using wildcard characters.
Sub DeleteAllFilesInFolder()
End Sub
Character Description
* (asterisk) Any number of characters
? (question mark) Any individual characters
The example below applies the * (asterisk) and ? (question mark) wildcard
characters
'Delete all .xlsx or .xlsm files from a folder, but not .xls files
'as the ? wildcard must be atleast on character in length
Kill "C:\Users\marks\Documents\Folder\*.xls?"
Sub GetFileAttributes()
myFile = "C:\Users\marks\Documents\Folder\ReadOnlyFile.xlsx"
End If
End Sub
To check for other attributes, replace vbReadOnly in the code above with the
required settings.