Add line of text to beginning of multiple files
-
@guy038 said in Add line of text to beginning of multiple files:
In case of your replacement regex ends with the forms \r\n, \n or \r, depending of your file(s) structure, as above, it/they must not begin with one/several true empty lines !
So I found myself with just such a case. Hmmm, what to do…?
Here’s what I wanted:
- Add
new first line\r\nto a bunch of files as, obviously, the new first line data.
Here’s what I did:
I did a two-step replacement; first I did:
Find what box:
^\A
Replace with box:new first lineFIRSTLINEPSEUDOENDINGSecondly,
Find what box:
FIRSTLINEPSEUDOENDING
Replace with box:\r\nOf course
FIRSTLINEPSEUDOENDINGcould be any unique string that doesn’t already appear in the files I’m applying this to. - Add
-
Hi All,
i’m trying to add the below 6 lines into the top of 300 batch scripts, but none of the above worked
@echo off
call D:\Tidal\bin\SetupEnv.bat
if errorlevel 1 (
echo Could not retrieve environment variables.
exit /b 1
) -
@sean-o-sullivan said in Add line of text to beginning of multiple files:
none of the above worked
What exactly did you try?
Additional question: Do all/some/none of the files you want to affect have empty line(s) at the beginning?
-
none of the above worked
Notepad++ search/replace boxes use a single line for inputting the expression, so if you want to insert multiple lines, you have to encode the line endings in a way the search engine understands: since you are likely using Windows CRLF line endings for a batch file, every newline in the text you want to insert needs to be converted to
\r\n; further, any special character (like the\) needs to be escaped. So, to give an example, the first two lines of your insertion would be something like@echo off\r\ncall D:\\Tidal\\bin\\SetupEnv.bat\r\n, and you’d have to prepare the rest of the lines similarly.But really, when you’re getting to multi-line-text inserts and hundreds of files, you’re starting to move beyond what is the primary focus of text editor software: it focuses more on one or a few files, rather than bulk editing (though Find in Files is a nice move toward bulk-file-editing).
Operating systems like Windows and Linux are primarily GUI nowadays, but they still ship with one or more command line systems; there’s a reason for that: you can do powerful things at the command line, often on lots of files in a short amount of time, with very little code.
- Backup any critical batch files: your files are always your responsibility
- Create a file called
prefix.in, which has your 6 lines that you want to insert (make sure to have a NEWLINE at the end of the last line), and save. - Put
prefix.inin the same directory as the three-hundred*.batfiles - open
cmd.exeand change to that directory for %F in (*.bat) do @( copy prefix.in + %F %F.out & move /Y %F.out %F )
This will look up the name (
%F) of every batch file in the folder, it will concatenate the contents ofprefix.inand%Finto a new temporary file%F.out, then it will rename%F.outback to%F. (I would have usedRENorRENAMEinstead ofMOVE, butRENdoesn’t have the/Yto force allowing overwriting of the destination.)With one line of command-line syntax, I just prefixed all 300 batch files with the 6 lines you wanted.
(Whenever I need to look up
cmd.exesyntax, I use https://ss64.com/nt/syntax.html for topic-level, and the “CMD” linkhttps://ss64.com/nt/for a per-commmand reference. I know, usingcmd.exeis old fashioned, but I’ve never gotten the hang of powershell.)Notepad++ is great, but there are other tools available to you.
-
@PeterJones does the legwork I was trying to get the OP to do. :-(
I think it’s an OK task for Notepad++. A lot of people are scared to dip down into the world of batch/CMD/Powershell…
The OP even did the research to find this thread. However, the OP should have done more than “Wah! It doesn’t work”. Not a direct quotation. :-)
Notepad++ …/replace boxes use a single line for inputting the expression, so if you want to insert multiple lines, you have to encode the line endings in a way the search engine understands: since you are likely using Windows CRLF line endings for a batch file, every newline in the text you want to insert needs to be converted to \r\n; further, any special character (like the ) needs to be escaped.
People are scared also to do Pythonscript, so I hesitate to bring this up, but a script I use can do the conversion of the data per the above. It takes the currently selected text, formats it correctly, and puts the result in the clipboard, ready for pasting into the Replace with box:
# -*- coding: utf-8 -*- from Npp import editor, notepad class T11987(object): def __init__(self): if editor.getSelections() > 1: return # acting on multiple selections or a column block probably doesn't make sense s = editor.getSelText() if len(s) == 0: return new_s = s.replace('\\', '\\\\').replace('\r', '\\r').replace('\n', '\\n').replace('(', '\\(').replace(')', '\\)') if len(new_s) > 2046: notepad.messageBox('After conversion, the selected text is too long for the ***Replace with*** box (limit: ~2046 chars)', '') else: editor.copyText(new_s) notepad.messageBox('The selected text has been converted and is now in the clipboard.', '') if __name__ == '__main__': T11987()Running the script on the OP’s replace data yields the full string that Peter started:
@echo off\r\ncall D:\\Tidal\\bin\\SetupEnv.bat\r\nif errorlevel 1 \(\r\necho Could not retrieve environment variables.\r\nexit /b 1\r\n\)\r\nThere might be more characters that need escaping in a regex replace string, but in practice I haven’t encountered them yet.
-
@PeterJones You sir are a legend, that worked perfectly!!! Thank you so much for taking the time to help me out
-
A lot of people are scared to dip down into the world of batch/CMD/Powershell…
@PeterJones You sir are a legend, that worked perfectly!!!
…and some people are not afraid. :-)
One point of clarification on my previous reply:
@PeterJones had previously stated:
Notepad++ search/replace boxes use a single line for inputting the expression
but my reply concentrated only on the replace box. The reason for that is that if you want to get multiline text that you already have in the Notepad++ window into the “search box”, all you have to do is select it and press ctrl+f (if ctrl+f is so configured – to copy into the box automatically). No conversion of the text (or escaping) is necessary.
UNLESS, you are going to be doing a multiline replacement – in which case you need to be in Regular expression search mode – and your search data contains any of the special regex metacharacters. In this case the best thing to do might be to add a
\Qat the very start of the “search box”.Yikes, this might be really “obscure”!
-
Glad you got something that worked. I hope you took the opportunity to learn from what @Alan-Kilborn shared as well, because it really is helpful search/replace advice, too.
-----
@Alan-Kilborn said in Add line of text to beginning of multiple files:
I think it’s an OK task for Notepad++.
Well, the 6 lines aren’t bad to encode into the replace box. But when the next poster wants to insert 1023 lines at the beginning of every config file? As I said, “you’re starting to move beyond” (emphasis added): inserting 6 lines at the beginning of 300 files has started down the slippery slope out of the realm of pure text-editor, but not so far that it’s not doable.
Pythonscript, … It takes the currently selected text, formats it correctly, and puts the result in the clipboard, ready for pasting into the Replace with box
I didn’t read correctly / realize what you were doing there until the second read: that’s just cool.
A side question, unrelated to OP: why create an object with the
__init__, rather than just have aT11987()function instead?my reply concentrated only on the replace box. … No conversion … necessary
True enough.
-
@PeterJones said in Add line of text to beginning of multiple files:
inserting 6 lines at the beginning of 300 files has started down the slippery slope out of the realm of pure text-editor
Agreed. Maybe that’s why I said it’s an “OK” task for Notepad++ and not “a great” task. :-)
why create an object with the init, rather than just have a T11987() function instead?
It’s just my new framework for scripts (I copy from some boilerplate when I make a new script). Obviously in this case it is fairly trivial, but in scripts that use callbacks a object-based approach has bigger benefits. I picked up on this by observing how @Ekopalypse does some of the more complicated scripts he’s presented. BTW, I picked up on using the topic/posting id in the name of a script/function/class from YOU. :-)
-
Some seven years later: exactly my query too. Many thanks. 😊😊