One way to go about this is read in the file names - this way you don't have to explicitly check to see if they exist.
path = SystemDialogInput["Directory", NotebookDirectory[]];
fileNames = FileNames["*.txt", path];
allFiles = Table[Import[fileNames[[num]]], {num, 1, Length[fileNames]}];
This reads the names of all files (in the specified path
directory) that have the extension ".txt" and places the file names in a list called fileNames
. It then reads all the files in this list. They can be be named anything at all, as long as they have the specified extension.
Of course you could use a different extension than .txt
, you could specify the directory namepath in the code (instead of using the GUI) and, you can use Get
instead of Import
, and Do
instead of Table
. You could also filter out files you don't want before importing, if this is desirable.