add code to multiple files by line number?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
SXGuy
Posts: 13
Joined: 17 Sep 2011 08:57

add code to multiple files by line number?

#1 Post by SXGuy » 04 Jan 2012 03:40

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.

aGerman
Expert
Posts: 4705
Joined: 22 Jan 2010 18:01
Location: Germany

Re: add code to multiple files by line number?

#2 Post by aGerman » 04 Jan 2012 14:03

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

SXGuy
Posts: 13
Joined: 17 Sep 2011 08:57

Re: add code to multiple files by line number?

#3 Post by SXGuy » 05 Jan 2012 05:31

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/" /> 

aGerman
Expert
Posts: 4705
Joined: 22 Jan 2010 18:01
Location: Germany

Re: add code to multiple files by line number?

#4 Post by aGerman » 05 Jan 2012 11:47

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

SXGuy
Posts: 13
Joined: 17 Sep 2011 08:57

Re: add code to multiple files by line number?

#5 Post by SXGuy » 05 Jan 2012 13:06

perhaps something like sed may be a better option then.

Aacini
Expert
Posts: 1926
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: add code to multiple files by line number?

#6 Post by Aacini » 06 Jan 2012 21:19

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...
Last edited by Aacini on 11 Jan 2012 17:34, edited 2 times in total.

SXGuy
Posts: 13
Joined: 17 Sep 2011 08:57

Re: add code to multiple files by line number?

#7 Post by SXGuy » 07 Jan 2012 05:47

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.

SXGuy
Posts: 13
Joined: 17 Sep 2011 08:57

Re: add code to multiple files by line number?

#8 Post by SXGuy » 09 Jan 2012 11:33

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.

Aacini
Expert
Posts: 1926
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: add code to multiple files by line number?

#9 Post by Aacini » 09 Jan 2012 17:59

Ops! This line is wrong:

Code: Select all

findstr /N ^^ "%%f% > "%%Nf.tmp"
It should be:

Code: Select all

findstr /N ^^ "%%f" > "%%Nf.tmp"

SXGuy
Posts: 13
Joined: 17 Sep 2011 08:57

Re: add code to multiple files by line number?

#10 Post by SXGuy » 10 Jan 2012 04:28

Thanks, ill try it later and report if any issues.

Aacini
Expert
Posts: 1926
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: add code to multiple files by line number?

#11 Post by Aacini » 10 Jan 2012 11:22

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... :|

SXGuy
Posts: 13
Joined: 17 Sep 2011 08:57

Re: add code to multiple files by line number?

#12 Post by SXGuy » 10 Jan 2012 13:57

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.

Aacini
Expert
Posts: 1926
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: add code to multiple files by line number?

#13 Post by Aacini » 10 Jan 2012 19:24

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.

SXGuy
Posts: 13
Joined: 17 Sep 2011 08:57

Re: add code to multiple files by line number?

#14 Post by SXGuy » 11 Jan 2012 02:10

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.

Aacini
Expert
Posts: 1926
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: add code to multiple files by line number?

#15 Post by Aacini » 11 Jan 2012 17:41

This is very strange! :( To solve the problem, change

Code: Select all

echo/!line:*:=!
by

Code: Select all

echo(!line:*:=!
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! :o 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.

Post Reply