If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Rate Thread | Display Modes |
#31
|
|||
|
|||
Adding one line to a directory full of text files
On Thu, 08 Jun 2017 20:23:43 -0400, Paul wrote:
https://stackoverflow.com/questions/...e-copy-command COPY FILE1+FILE2=FILE1 FILE2 would contain the single line of text you want appended. Thank you for that helpful advice as that's the only suggestion in this thread that actually worked even partially on Windows. I'm still working the problem but I have 2 of 3 issues solved. Linux aside, there are three components to a Windows solution. 1. For loop (to find all the files of the given extension) FOR: https://ss64.com/nt/for2.html 2. If query (to ascertain whether the desired line already exists) IF: https://ss64.com/nt/if.html 3. Append action (to append the desired line to the end of the file). ECHO: https://ss64.com/nt/echo.html TYPE: https://ss64.com/nt/type.html COPY: https://ss64.com/nt/copy.html REDIRECT: https://ss64.com/nt/syntax-redirection.html These each work inside a script to always append a given line to all files of a particular extension. -------- @echo off for %%i in (*.bat) do (echo add this command %%i) -------- @echo off for %%i in (*.bat) do type append_me2.txt %%i -------- @echo off for %%i in (*.bat) do copy %%i+append_me3.txt=%%i -------- So all I need now is to figure out the IF syntax to query if a certain line already exists anywhere in the text batch file. |
Ads |
#32
|
|||
|
|||
Adding one line to a directory full of text files
Chaya Eve wrote:
On Thu, 08 Jun 2017 20:23:43 -0400, Paul wrote: https://stackoverflow.com/questions/...e-copy-command COPY FILE1+FILE2=FILE1 FILE2 would contain the single line of text you want appended. Thank you for that helpful advice as that's the only suggestion in this thread that actually worked even partially on Windows. I'm still working the problem but I have 2 of 3 issues solved. Linux aside, there are three components to a Windows solution. 1. For loop (to find all the files of the given extension) FOR: https://ss64.com/nt/for2.html 2. If query (to ascertain whether the desired line already exists) IF: https://ss64.com/nt/if.html 3. Append action (to append the desired line to the end of the file). ECHO: https://ss64.com/nt/echo.html TYPE: https://ss64.com/nt/type.html COPY: https://ss64.com/nt/copy.html REDIRECT: https://ss64.com/nt/syntax-redirection.html These each work inside a script to always append a given line to all files of a particular extension. -------- @echo off for %%i in (*.bat) do (echo add this command %%i) -------- @echo off for %%i in (*.bat) do type append_me2.txt %%i -------- @echo off for %%i in (*.bat) do copy %%i+append_me3.txt=%%i -------- So all I need now is to figure out the IF syntax to query if a certain line already exists anywhere in the text batch file. The Windows "findstr" utility is similar to Linux "grep". I think at some point in this thread, findstr might have been mentioned. Perhaps you can look for a string that way. ******* In this example: https://stackoverflow.com/questions/...-find-a-string you will see lines like this: findstr /c:"stringToCheck" "c:\MyFolder\fileToCheck.bat" nul 2&1 The second parameter is the thing they're looking for. The third parameter is the file name. Now, if this was "grep", it would spit out one line of text, for each line that matches the specification. The redirection to NUL, which would be NUL , tosses the text away. Just the ERRORLEVEL, the return code from the call, is all we want, to decide whether there is at least one instance in the file. In Windows, NUL means the same thing as /dev/null in Linux would mean. And 2&1 redirects STDERR to STDOUT, and STDOUT is going to NUL. So all possible text output from the findstr is going into the dumper. At least with grep, you could also get it to print out the line number of each found occurrence. And then you could apply some logic, to see if the last line of some grep output, happened to match the last line number of the file. The thing is, at some point, a batch file "runs out of juice" for the job. It becomes inappropriate to do it that way, because the batch just isn't outfitted well for the job. And perl or gawk or some other text processing tool is more appropriate. Even then the chore will be difficult, and could require five lines of code :-) If you added the necessary logic to your existing scripts (the scripts that generate the files), then you already know the necessary line isn't in the file, and can forgo doing this check. Paul |
#33
|
|||
|
|||
Adding one line to a directory full of text files
On Mon, 12 Jun 2017 16:34:23 -0400, Paul wrote:
In this example: https://stackoverflow.com/questions/...-find-a-string you will see lines like this: findstr /c:"stringToCheck" "c:\MyFolder\fileToCheck.bat" nul 2&1 Thank you for finding that find-string command! I will try it out as that's the only flaw so far in the process of adding a command to a text batch file already filled with commands. It turns out that having the command multiple times doesn't hurt anything, but it's not elegant. |
#34
|
|||
|
|||
Adding one line to a directory full of text files
"Chaya Eve" wrote
| I will try it out as that's the only flaw so far in the process of adding a | command to a text batch file already filled with commands. | | It turns out that having the command multiple times doesn't hurt anything, | but it's not elegant. I find that to be a common but odd view. In many cases, several lines of code can be compressed into a single line. I once saw a single line of Perl that was claimed to be able to write a CD. If I remember correctly it was several hundred characters, written by someone at MIT. Elegant? No. Just compact. But it also loses readability. If readability suffers than re-usability suffers. That Perl line was merely a useless novelty. And the process that compact code represents is not necessarily compact in itself. That depends on what it does. It might take several pages of programming code to make a button show in a window. That can be done in a single line with a component. More efficient or more elegant? No. The component is only a "wrapper". The same code is now inside the component and you've added the overhead of an external library! In other words, the amount of code has no direct relationship to the efficiency or streamlining of the functionality, nor to the usability of the code. |
Thread Tools | |
Display Modes | Rate This Thread |
|
|