Christian Specht avatar Christian Specht committed db7c6aa

Directory_Exists and File_Exists

Comments (0)

Files changed (3)

 Most of the functions are named after (and do the same like) useful functions in .NET that I missed in VBA.  
 Here is a short summary of the available functions and what they do in a nutshell:
 
+- **`Directory_Exists`**  
+Returns True if the specified directory exists.
+- **`File_Exists`**  
+Returns True if the specified file exists.
 - **`File_Delete`**  
 Deletes a file. If the file does not exist, nothing happens.
 - **`File_ReadAllLines`**  

vba-helpers-tests.bas

 '--------------------------------------------------------------------
 
 
+Public Sub Directory_Exists_ExistingDir_ReturnsTrue()
+
+    Assert.IsTrue Directory_Exists(Path_GetCurrentDirectory)
+
+End Sub
+
+Public Sub Directory_Exists_NonExistingDir_ReturnsFalse()
+
+    Assert.IsFalse Directory_Exists(Path_Combine(Path_GetCurrentDirectory, "doesnt.exist"))
+
+End Sub
+
+Public Sub File_Exists_ExistingFile_ReturnsTrue()
+
+    Assert.IsTrue File_Exists(Path_Combine(Path_GetCurrentDirectory, "readme.md"))
+
+End Sub
+
+Public Sub File_Exists_NonExistingFile_ReturnsFalse()
+
+    Assert.IsFalse File_Exists(Path_Combine(Path_GetCurrentDirectory, "doesnt.exist"))
+
+End Sub
+
 Public Sub Path_Combine_SeparatorAtTheEnd_IsTruncated()
 
     Assert.AreEqualStrings Path_Combine("foo\"), "foo"
 '# VBA Helpers
 '# A collection of useful VBA functions
 '#
-'# Version 20121109.174956
+'# Version 20121227.215544
 '# (the version number is just the current date/time)
 '#
 '# Copyright (c) 2012 Christian Specht
 
 '##########################################################################################################################################
 
+
+Public Function Directory_Exists(ByVal path_vbah As String) As Boolean
+    'Returns True if the specified directory exists.
+    
+    Directory_Exists = (Dir(path_vbah, vbDirectory) > "")
+    
+End Function
+
+Public Function File_Exists(ByVal path_vbah As String) As Boolean
+    'Returns True if the specified file exists.
+    
+    File_Exists = (Dir(path_vbah) > "")
+    
+End Function
+
 Public Sub File_Delete(ByVal path_vbah As String)
     'Deletes a file. If the file does not exist, nothing happens.
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.