Deleting empty line only at the beginning of the file

Deleting empty line only at the beginning of the file

3
NewbieNewbie
3

    Jul 18, 2016#1

    Hi all,

    I want to run a macro across thousands of files that will delete an empty line only if it is the very first line of the file - can this be done? I have the regular expression that finds the line and it would work fine if all of the files had an empty line at the beginning, but unfortunately that is not that case.

    Any solutions out there?

    Thanks

    Jim

    6,654559
    Grand MasterGrand Master
    6,654559

      Jul 18, 2016#2

      Does the first non empty line always start with the same string as in XML or HTML or XHTML files which does not exist anywhere else in the file?

      That would make it very easy to delete all empty lines at top of each file using a regular expression Replace in Files without the need to code a macro which opens each file, deletes the empty lines at top, saves and closes each file.

      What I mean is that for example HTML/XHTML files start with <!DOCTYPE which is a string usually not existing anywhere else in an HTML/XHTML file. Or XML files start with <?xml also usually not existing anywhere else in each XML file.

      Have your files always the same string after 1 or more empty lines at top? Have your files a common header line?
      Best regards from an UC/UE/UES for Windows user from Austria

      3
      NewbieNewbie
      3

        Jul 19, 2016#3

        No they do not - they have varied contents, nothing unique like that.

        6,654559
        Grand MasterGrand Master
        6,654559

          Jul 19, 2016#4

          Okay, then we can't use a regular expression Replace in Files. It would be perhaps possible with a regular expression Replace in Files, but this depends on the file sizes and is therefore not secure for usage on any text file of any file size.

          Here is the code of a macro which first runs a recursive Find in Files
          • on the specified directory C:\Temp\ (backslash at end is important)
          • with the specified file type specification *.txt (can be also just * for all files or a list of file types separated with a semicolon)
          • with an empty search string
          to get output into a results file a list of file names with full path found in the directory tree starting in C:\Temp\.

          English UltraEdit v23.20 appends at end of the file names list the summary information

          Code: Select all

          Search complete, found '' x time(s). (0 file(s)).
          
          
          This summary information is selected with a case-sensitive Find for Search complete, in upwards direction with selecting everything from bottom of file up to beginning of found string. The selected block is deleted if this string could be really found. The search string must be modified if not using English UltraEdit. Run manually a Find in Files with an empty search string in Files listed to see what your UltraEdit in your configuration outputs.

          The results file created by Find in Files is a Unicode file since UE v12.00 and UEStudio v5.50. For that reason the results file with the list of file names is converted to ASCII/ANSI before processing the file names.

          In a loop executed until caret reaches end of the results file the next file name with path is selected and the file is opened. In the opened file with caret at top a check is made if the first character is a carriage return or a line-feed in which case a single regular expression Replace is executed to delete the empty line(s) at top of the file. The modified file is saved and next closed. Files starting with any other character than CR or LF are closed without being modified at all, i.e. keep their last modification time.

          Finally the results file with the list of file names is closed without saving it.

          The file with the file names must end with a line termination to reach with Key HOME and Key DOWN ARROW the end of the file. But this is definitely the case on list file created with Find in Files.

          Code: Select all

          InsertMode
          ColumnModeOff
          HexOff
          UltraEditReOn
          FindInFiles Recursive "C:\Temp\" "*.txt" ""
          Find MatchCase Up Select "Search complete,"
          IfSel
          Delete
          EndIf
          Top
          UnicodeToASCII
          Loop 0
          IfEof
          ExitLoop
          EndIf
          StartSelect
          Key END
          Open "^s"
          Top
          IfCharIs 13
          Find MatchCase RegExp "[^r^n]+"
          Replace ""
          Save
          Else
          IfCharIs 10
          Find MatchCase RegExp "[^r^n]+"
          Replace ""
          Save
          EndIf
          EndIf
          CloseFile NoSave
          EndSelect
          Key HOME
          Key DOWN ARROW
          EndLoop
          CloseFile NoSave
          
          Best regards from an UC/UE/UES for Windows user from Austria

          3
          NewbieNewbie
          3

            Jul 19, 2016#5

            Awesome! Thanks!

            326
            Basic UserBasic User
            326

              Jul 21, 2016#6

              Can't you just use a perl regular expression using the "\A Match only at beginning of string" zero-width assertion?

              Find: \A\r?\n
              Replace: empty

              6,654559
              Grand MasterGrand Master
              6,654559

                Jul 21, 2016#7

                That does not work for large files. I tested that before I wrote the macro. \A means start of buffer. For small files the buffer size is >= file size and \A can be used. But for large files with several MB a search string like \A\r?\n can find multiple empty lines within a file, not only the empty line at top of the file. Large files are not completely loaded into memory by UltraEdit.
                Best regards from an UC/UE/UES for Windows user from Austria

                326
                Basic UserBasic User
                326

                  Jul 22, 2016#8

                  Good to know. I've used \A to add text at the top of files, never failed me but they were all small files - I use \z quite often to append to the end of files, again never failed me but also small files.

                  I do use a variety of external (command line) tools as well, often easier to write / modify a batch file.