Page 1 of 2
add code to multiple files by line number?
Posted: 04 Jan 2012 03:40
by SXGuy
hi, ive basically generated A LOT of pages and forgot to add some code to each one on the same line number.
how can i edit each file inside a folder to add specific code to the same line number on each file?
Thanks for any help in advance.
Re: add code to multiple files by line number?
Posted: 04 Jan 2012 14:03
by aGerman
SXGuy wrote:hi, ive basically generated A LOT of pages
HTML text? What's the maximum length of the lines? What's the character encoding?
SXGuy wrote:how can i edit each file inside a folder to add specific code to the same line number on each file?
You have to read each of them line by line and write them new. But first answer the questions please. It's important to know that first.
Regards
aGerman
Re: add code to multiple files by line number?
Posted: 05 Jan 2012 05:31
by SXGuy
Thanks for your reply, i had subscribed to the thread but didnt get a notification so sorry if im replying late. ill check my spam folders incase its been sent there.
anyway, they are php pages,
Code: Select all
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
its not really an issue, because if need be i could rename them all using a batch file renamer to any text extension, i just simply require adding the same peice of code to the same line number on each file.
so what i require i guess is, a batch file which will take the first file in the folder, add a line of code to line 14, save and move on to the next file until every file in that folder has been done.
Do i have to read each file first before placing the line of code, or is there a way to just add the code to a predefined line number?
The piece of code i wish to add on line 14 is
Code: Select all
<link rel="canonical" href="http://www.domain.com/" />
Re: add code to multiple files by line number?
Posted: 05 Jan 2012 11:47
by aGerman
The reasons for my questions:
- There is no limit for the line length in HTML, but there is a maximum string length you could process via batch.
- You can write ASCII and ANSI. Unicode is badly supported, but there is no way to write UTF-8 encoded.
As I told you before, you have to process the files line by line and write them new. The above are the knockout criterions in your case. I'm afraid you have to choose a different language.
Regards
aGerman
Re: add code to multiple files by line number?
Posted: 05 Jan 2012 13:06
by SXGuy
perhaps something like sed may be a better option then.
Re: add code to multiple files by line number?
Posted: 06 Jan 2012 21:19
by Aacini
If you mean
insert a new line in the position 14, and shift the rest of lines one position, the Batch file below solve your problem:
Code: Select all
@echo off
setlocal EnableDelayedExpansion
for %%f in (*.php) do (
findstr /N ^^ "%%f" > "%%~Nf.tmp"
echo :EOF>> "%%~Nf.tmp"
call :InsertLine14 < "%%~Nf.tmp" > "%%~Nf.out"
REM del "%%f"
REM del "%%~Nf.tmp"
REM ren "%%~Nf.out" "%%~Nf.php"
)
goto :EOF
:InsertLine14
for /L %%i in (1,1,13) do (
set /P line=
echo(!line:*:=!
)
echo ^<link rel="canonical" href="http://www.domain.com/" /^>
:copyRestOfLines
set /P line=
if "!line!" == ":EOF" exit /b
echo(!line:*:=!
goto copyRestOfLines
This program have several restrictions on the lenght of the line and the characters it may process. Just test it with a few files and review that the results are correct. If so, you may remove the REM in the DEL and REN commands and use it with all your files, BUT FIRST DO A BACKUP OF ALL OF THEM. I no accept any liability for any data loss!
EDIT: I fixed a typo of " by %, and several ~ missed. Sorry...
Re: add code to multiple files by line number?
Posted: 07 Jan 2012 05:47
by SXGuy
thats great! thank you so much.
i will test it out on a few dummy files before i run it properly.
Thanks for your help.
Re: add code to multiple files by line number?
Posted: 09 Jan 2012 11:33
by SXGuy
ok so i just tried this on a couple of dummy files and, if i leave REM in place, no files are overwriten but a %Nf.temp and %Nf.out files are produced.
%Nf.out looks like
Code: Select all
line:*:=
line:*:=
line:*:=
line:*:=
line:*:=
line:*:=
line:*:=
line:*:=
line:*:=
line:*:=
line:*:=
line:*:=
line:*:=
<link rel="canonical" href="http://www.domain.com/" />
while %Nf.temp is blank
if i remove the rem, then the original php files are deleted, and a single %Nf.php file is created, whiping all original data and only displaying output the same as above.
Re: add code to multiple files by line number?
Posted: 09 Jan 2012 17:59
by Aacini
Ops! This line is wrong:
It should be:
Re: add code to multiple files by line number?
Posted: 10 Jan 2012 04:28
by SXGuy
Thanks, ill try it later and report if any issues.
Re: add code to multiple files by line number?
Posted: 10 Jan 2012 11:22
by Aacini
Excuse me. It seems I confused a preliminary version of the program instead of the good one. I fixed the errors in the original program above. Please copy it again. Sorry...

Re: add code to multiple files by line number?
Posted: 10 Jan 2012 13:57
by SXGuy
i tried the amended code, however the files it produces, dont include the text specified, and it also adds line numbers and colons to the file.
Re: add code to multiple files by line number?
Posted: 10 Jan 2012 19:24
by Aacini
The *.TMP files have the contents you said, but the *.OUT files should have the right result. Please post a small .php test file and the .out produced file you got.
Re: add code to multiple files by line number?
Posted: 11 Jan 2012 02:10
by SXGuy
ok, originally it was a php file from a website im working on, but as to not expose my code to the web, ive changed it to just a few lines of text instead, so i can show you the result.
ok, so the php file is called first.php and inside it has 16 lines of data
Code: Select all
test
test
test
test
test
test
test
test
test
test
test
test
test
test
test
test
when i run the bat file the tmp file produced shows
Code: Select all
1:test
2:test
3:test
4:test
5:test
6:test
7:test
8:test
9:test
10:test
11:test
12:test
13:test
14:test
15:test
16:test
:EOF
and the .out file
Code: Select all
line:*:=
line:*:=
line:*:=
line:*:=
line:*:=
line:*:=
line:*:=
line:*:=
line:*:=
line:*:=
line:*:=
line:*:=
line:*:=
<link rel="canonical" href="http://www.domain.com/" />
line:*:=
line:*:=
line:*:=
so as you can see, the tmp file adds line numbers to it, but doesnt include the text to add. ans the .out file adds line:*:= and clears all other text.
Re: add code to multiple files by line number?
Posted: 11 Jan 2012 17:41
by Aacini
This is very strange!

To solve the problem, change
by
There are two instances of this line. I already done this change in the program above.
However, I don't understand the cause of this error!

Supossedly if the value of the line is "TEST", the command "ECHO/!LINE!" should not have any problem with it! Perhaps someone of the Batch experts may clearify this point.