Action if file does not exist

Action if file does not exist

74
Advanced UserAdvanced User
74

    Dec 04, 2009#1

    Is it possible to have UE check a folder to see if a file exist, if it doesn't create it?

    So if i have a list of files:

    Test1.txt
    Test2.txt
    Test3.txt

    And Test2.txt doesn't exist can I trigger the script to create it?

    What I'm trying to do is make a batch file that runs on a list of files (from Mofi's GetListOfFiles.js). It's suppose to open the file and run function A or if file doesn't exist run function B.

    Thank you for your help.

    6,686585
    Grand MasterGrand Master
    6,686585

      Dec 05, 2009#2

      GetListOfFiles.js produces always a list of existing files. But to answer your question, no there is no command to check if a file exist or not.

      It is also not possible to simply open a file and then check if the active file has the name used in the open command. If you try from within a script to open a not existing file you will get an error message and the script halts until you close the error message.

      You can check file existence with a batch file, for example see checking if files exist.

      But I have had an idea how to check file existence from within an UltraEdit script. Use the function below:

      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;
         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();
         
         // 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();
         UltraEdit.outputWindow.write(UltraEdit.clipboardContent);
      
         // 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;
      }
      Best regards from an UC/UE/UES for Windows user from Austria