File not found error handling

File not found error handling

74
Advanced UserAdvanced User
74

    Nov 17, 2011#1

    Hi, I have a test file with file names and paths. It is used to search a directory, find then open the file, run a script inside the file, close it and move on to the next line in the test file. I've got one bug I'm not sure how to handle. If the file is not found in the directory to open I get UEs error dialog box stating so. Then the whole application crashes after acknowledging the error.

    Here is the line of code to open the file. Can someone help me with the right if statement to just note the file was not found and continue?

    Code: Select all

    	if (nXmlFile < UltraEdit.document.length){
    		UltraEdit.document[nXmlFile].setActive();
    	} else { UltraEdit.open(sFileName);
    
    Thanks,
    Max

    6,682583
    Grand MasterGrand Master
    6,682583

      Nov 18, 2011#2

      Interesting would be the code after line UltraEdit.open(sFileName); Do you check if number of opened files increased by one after this command?

      Code: Select all

      if (nXmlFile < UltraEdit.document.length){
        UltraEdit.document[nXmlFile].setActive();
      } else {
         var nFileNum = UltraEdit.document.length;
         UltraEdit.open(sFileName);
         if (nFileNum < UltraEdit.document.length) {
            // Okay, file was opened. Continue with processing.
         }
      }
      Even better would be to check if file exists before opening it. This can be done for example with following function. Please note that this function performs by default a case-sensitive file name check. Uncomment the 2 lines containing toLowerCase if the file name check should be not case-sensitive.

      Code: Select all

      function FileExist (sFileName) {
      
         // Is the required parameter passed to the function?
         if (typeof(sFileName) != "string") return false;
         // Is the file name an empty string?
         if (sFileName == "") return false;
      
         // Store the index of the active clipboard.
         var nActiveClipboard = UltraEdit.clipboardIdx;
      
         // Use clipboard 9 to save current output window data, but
         // before save content of clipboard 9 into string variable.
         UltraEdit.selectClipboard(9);
         var sClipboard9 = UltraEdit.clipboardContent;
         UltraEdit.outputWindow.copy();
         UltraEdit.outputWindow.clear();
      
         // Run Find in Files with an empty search string and with the passed file
         // name as file type. The search result is written to the output window.
         UltraEdit.frInFiles.regExp=false;
         UltraEdit.frInFiles.matchCase=true;
         UltraEdit.frInFiles.matchWord=false;
         UltraEdit.frInFiles.searchSubs=false;
         UltraEdit.frInFiles.unicodeSearch=false;
         UltraEdit.frInFiles.filesToSearch=0;
         UltraEdit.frInFiles.directoryStart="";
         UltraEdit.frInFiles.useOutputWindow=true;
         UltraEdit.frInFiles.ignoreHiddenSubs=false;
         UltraEdit.frInFiles.displayLinesDoNotMatch=false;
         UltraEdit.frInFiles.searchInFilesTypes=sFileName;
         if (typeof(UltraEdit.frInFiles.openMatchingFiles) == "boolean") {
            UltraEdit.frInFiles.openMatchingFiles=false;
         }
         UltraEdit.frInFiles.find("");
      
         // Use clipboard 8 to get search result in output window, but
         // before save content of clipboard 8 into string variable.
         UltraEdit.selectClipboard(8);
         var sClipboard8 = UltraEdit.clipboardContent;
         UltraEdit.outputWindow.copy();
      
         // For a not case-sensitive file name check convert both strings
         // to lower case before continuing with the script execution.
      // sFileName = sFileName.toLowerCase();
      // UltraEdit.clipboardContent = UltraEdit.clipboardContent.toLowerCase();
         // Search for the passed file name in the search result.
         // If not found, i.e. returned index == -1, the file does not exist.
         var bFileFound = (UltraEdit.clipboardContent.indexOf(sFileName) < 0) ? false : true;
      
         // Restore content of clipboard 8.
         UltraEdit.clearClipboard();
         if (sClipboard8 != "") UltraEdit.clipboardContent = sClipboard8;
      
         // Restore content of the output window.
         UltraEdit.selectClipboard(9);
         UltraEdit.outputWindow.clear();
         var sOutputWindow = UltraEdit.clipboardContent.replace(/\n$/,"");
         if (sOutputWindow != "") UltraEdit.outputWindow.write(sOutputWindow);
      
         // Restore content of clipboard 9.
         UltraEdit.clearClipboard();
         if (sClipboard9 != "") UltraEdit.clipboardContent = sClipboard9;
      
         // Select the previous active clipboard.
         UltraEdit.selectClipboard(nActiveClipboard);
      
         // Return the result of the file existence check.
         return bFileFound;
      }

      74
      Advanced UserAdvanced User
      74

        Nov 18, 2011#3

        Thanks Mofi! I never thought of doing a find in files first.