Action if file does not exist

Help with writing and running scripts

Action if file does not exist

Postby mightymax » Fri Dec 04, 2009 6:33 pm

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.
User avatar
mightymax
Basic User
Basic User
 
Posts: 30
Joined: Tue Jul 27, 2004 11:00 pm
Location: San Diego, CA

Re: Action if file does not exist

Postby Mofi » Sat Dec 05, 2009 8:32 am

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;
}
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4039
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Scripts