How to use a template to create new files in new folder with a string entered by the user replacing placeholders?

How to use a template to create new files in new folder with a string entered by the user replacing placeholders?

2
NewbieNewbie
2

    Mar 30, 2017#1

    Hi there!

    This software is the way for me to achieve the results I need.

    Unfortunately, the more I read about macros in this forum the less I understand. (I've definitively lost a part of my mind here 8O .)

    Any help required, thank you in advance for that.

    My needs:
    1. OPEN an existing file (same name, same path all the time - it is a template).
    2. GET a string from the user (var NEWSTRING).
    3. FIND each occurrence of the string "toto" in the file and REPLACE it with the variable entered in point 2 (NEWSTRING).
    4. IDENTIFY "blocks" of text in the page. (I've separated blocks by starting each one by a subsequent number which means that: 1 to 2 is the first block, 2 to 3 is the second block, 3 to 4 the 4th, etc.) I can edit and change that easily with any other method as you wish.
    5. CREATE a different new file with each block of text.
    6. CREATE a folder in the same path as the original file with name "var:NEWSTRING".
    7. NAME each new file "var:NEWSTRING" with an incremental value ( -1,-2,-3,..), in HTML format, in the folder newly created.
    8. CLOSE original file without saving any modification.
    So because I am really not convinced on the fact that my explanations are clear at all ;) ( native French ), I try again a bit differently here (that way, if you understand both to be the exact same request, it's sure, you've got it :D ) :
    1. Open the file //XYZ/Desktop/Files/Template.html
    2. Ask the user for the new string, i.e. I will hack this macro!
    3. Find "toto" in the whole text and replace it with the value of the new string.
    4. Cut the document in several blocks.
    5. Paste each block into a new different file.
    6. Create a new folder with the name of the variable given in pt 2 in the same path as the original file.
    7. Save each new file with the filename "I will hack this macro! -X.html".
    8. DISCARD any modification in the original file and close it without saving.
    And the result should be (for the filenames): "I will hack this macro! -1.html" "I will hack this macro! -2.html" "I will hack this macro! -3.html".

    NOTES:
    • I use the HTML format only for the convenience of another app that I will then use to act upon the newly generated files. There is only text in those files.
    • There is no other number in the whole original file than the separators and they are not meant to appear anywhere in the new files. (They act only as separators in the original one.)
    Thank you.

    6,681583
    Grand MasterGrand Master
    6,681583

      Mar 30, 2017#2

      UltraEdit macros don't support variables. The only possibility is to use the 10 clipboards to hold strings and re-use them.

      Which version of UltraEdit do you use?

      The task could be easily done using an UltraEdit script instead of an UltraEdit macro. But that requires at least UltraEdit for Windows v13.00.

      One more question: Is the template file a Unicode file with Unicode characters or an ASCII/ANSI encoded file?

      In case of template file is a small ASCII/ANSI encoded file and depending on version of UltraEdit everything most could be done in memory.

      But you have to additionally configure a user tool executed by the UltraEdit script for creating the folder. UltraEdit is a text editor and not a file manager. It does not have any command to create a folder. On saving a new file with a new name the folder in which to save the file must already exist. Well, the user tool command line is simple: mkdir "%sel%". The rest is done by the script.
      Best regards from an UC/UE/UES for Windows user from Austria

      2
      NewbieNewbie
      2

        Apr 02, 2017#3

        Thanks for your reply.

        I am using UE version 16.10.0.22 for Mac OS X.

        Text is with usual text characters and when I manually do a find/replace it works very well.

        I am open to any kind of solution here, scripts would be perfect ( if you can write it for me :-) ).

        6,681583
        Grand MasterGrand Master
        6,681583

          Apr 03, 2017#4

          Here is the script working with UltraEdit on Windows:

          Code: Select all

          var sTemplateFile = "C:\\Temp\\template.html";  // Template file name with full path.
          var sBaseTargetPath = "C:\\Temp\\";             // Base path for the created files.
          var sFileExtension = ".html";                   // File extension of each file to create.
          var sBlockSeparator = "<!-- BLOCK -->\r\n"      // Separator string for the blocks.
          var sDirectorySeparator = "\\";                 // Directory separator according to OS.
          var rPlaceHolder = new RegExp("toto",'g');      // String to replace as RegExp object.
          
          // Define environment for this script.
          UltraEdit.insertMode();
          if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
          else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
          
          // Loop through all currently opened files and close the template file
          // without saving if it is already opened according to a case-sensitive
          // comparison of file name with full path.
          for(var nDocIndex = 0; nDocIndex < UltraEdit.document.length; nDocIndex++)
          {
             if(UltraEdit.document[nDocIndex].path == sTemplateFile)
             {
                UltraEdit.closeFile(UltraEdit.document[nDocIndex].path,2);
             }
          }
          
          // Open the template file and check if that was really successful.
          // An error message is displayed automatically by UltraEdit if the file
          // could not be opened because not existing as specified in this script.
          UltraEdit.open(sTemplateFile);
          if(UltraEdit.activeDocument.path == sTemplateFile)
          {
             // Get everything in template file loaded into a string variable.
             UltraEdit.activeDocument.selectAll();
             if (UltraEdit.activeDocument.isSel())  // Is template file not empty?
             {
                var sTemplateContents = UltraEdit.activeDocument.selection;
          
                // Cancel the selection by moving caret to top of file.
                UltraEdit.activeDocument.top();
          
                // Prompt script user for the string until a
                // non empty string is entered by the user.
                var sUserText;
                do
                {
                   sUserText = UltraEdit.getString("Please enter the string:",1);
                }
                while(!sUserText.length);
          
                // Replace the placeholder by user entered string and
                // split up the file contents into separate blocks.
                sTemplateContents = sTemplateContents.replace(rPlaceHolder,sUserText);
                var asBlocks = sTemplateContents.split(sBlockSeparator);
          
                // Replace in user string every character not possible
                // in a directory name or a file name by an underscore.
                var sFileName = sUserText.replace(/[:?=<>|+~\/\[\]\"\\]/g,"_");
          
                // Write the base target path with user text into template file.
                UltraEdit.activeDocument.write(sBaseTargetPath+sFileName);
          
                // Select the just written directory path and
                // run the user tool to create the directory.
                UltraEdit.activeDocument.selectToTop();
                UltraEdit.runTool("Create Directory");
          
                // Close the modified template file without saving.
                UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
          
                // Create the file name with full directory path.
                var sFullFileName = sBaseTargetPath + sFileName + sDirectorySeparator + sFileName + '-';
          
                // Write each block into a new file and save it with appending
                // to full file name an incrementing number (decimal) and the
                // file extension as defined at top of the script.
                for(var nBlockIndex = 0; nBlockIndex < asBlocks.length; nBlockIndex++)
                {
                   UltraEdit.newFile();
                   UltraEdit.activeDocument.write(asBlocks[nBlockIndex]);
                   var sFileNumber = (nBlockIndex+1).toString(10);
                   UltraEdit.saveAs(sFullFileName + sFileNumber + sFileExtension);
                   // UltraEdit.closeFile(UltraEdit.activeDocument.path,2);
                }
             }
          }
          
          Some additional information about the script.
          • I don't have a Mac. Therefore I don't know if that script works with UltraEdit for Mac at all.
          • The two strings at top contain Windows paths. They need to be modified with valid Mac paths.
          • My template file for testing the script was:

            Code: Select all

            block1: toto
            <!-- BLOCK -->
            block2: toto
            <!-- BLOCK -->
            block3: toto
            toto is the placeholder string replaced by the string entered by the script user.

            <!-- BLOCK --> with carriage return + line-feed is the block separating string. An incrementing number is not good as it makes the file contents splitting into blocks more difficult than using a simple string.

            Text files on Mac OS X have by default just a line-feed as line termination (Unix). So if your template file is a Unix file, remove \r from string assigned to variable sBlockSeparator.
          • The user tool with name Create Directory called by the script is for Windows defined as follows:

            Tab Command:
            Menu item name: Create Directory
            Command line: mkdir "%sel%"
            Working directory: nothing
            Toolbar bitmap/icon (file path):  not important

            Tab Options:
            Program type: DOS program
            Save active file: not checked
            Save all files first: not checked

            Tab Output:
            Command output: Append to existing
            Show DOS box: not checked
            Capture output: not checked
            Replace selected text with: No replace

            The command line must be adapted for Mac to create a complete directory tree without an error even if the directory already exists.
          • The character (string) assigned to variable sDirectorySeparator must be most likely / instead of \\ on Mac.
          Best regards from an UC/UE/UES for Windows user from Austria