Skip to main content

Importing files when a file may not exist

I have written a script to import multiple files into IDEA each month. However, there are times when some of the files I've included in my script are not available in the source location. Is there a way to write the script to skip these files if they are not in the source location?

Brian Element Wed, 02/08/2023 - 15:14

Sure, all you have to do is check to see if the file exists before doing the import.  Here is an example for you.  The function will return true if the file exists and false if it doesn't.  If you don't include the path information the it will look for the file in the project folder, if the files are stores somewhere else then include the path info.


Sub Main
	Dim sFiles(2) As String 'array to hold the list of files
	Dim i As Integer 'interger used for a loop
	Dim bReturn As Boolean 'boolean to see if file exists
	
	'list of files
	sFiles(0) = "File1.IMD"
	sFiles(1) = "File 2"
	sFiles(2) = "File 3"
	
	'loop through the array to see if the file exists
	For i = 0 To UBound(sFiles)
		bReturn = FileExists(sFiles(i))
		If bReturn Then
			MsgBox sFiles(i) & " file exists"
		Else 
			MsgBox sFiles(i) & " file doesn't exist"
		End If
	Next i
End Sub

Function FileExists(FilePath)
	Set fso = CreateObject("Scripting.FileSystemObject")
	If fso.FileExists(FilePath) Then
		FileExists=True
	Else
		FileExists=False
	End If
End Function

skoglundjeremy Wed, 02/08/2023 - 16:45

Brian, thank you for the quick response. Sorry for my lack of scripting knowledge, but is there a way to do this so it doesn't pop up a message box? I'd like it to just import the file if it exists and skip the import function if the file doesn't exist.
To add to this, once the files are imported, I have a function to append the databases. However, I need to come up with a way to write the script so if one of those files did not exist, the append function will skip that file.

Brian Element Wed, 02/08/2023 - 16:59

Sure, just remove the lines with the msgbox and on the line that has the file exist just insert your code to do the import.

skoglundjeremy Wed, 04/19/2023 - 10:18

Brian, I haven't gotten around to working on this in a couple months, but I realized that while the code you helped me with to import files worked perfectly, I forgot to follow up on the part about appending databases.
So I've gotten it to import files if they exist. What would the code look like to append the databases that were imported, considering that there will be times when it may not import all the files included in the initial script since they may not exist?