Solved Changing one line in a php script multiple times?

December 23, 2012 at 11:46:55
Specs: Windows Vista
I run a football league. I'll try to give as much detail as I can. Each season is within its own folder,(2003,2004,2005,etc) are the seasons and each is its own directory.

Most of my code is in php. Php might be better used for what I'm needing, but a batch program will do the trick.

So what Id like to do is run a batch program within each folder.

Currently what I do for the new season is just copy the previous season's folder and rename it +1. So the scripts would be $season = 2005.(inside the 2006 folder). With about 50 or so files within the 2006 directory, I open each script and just change that one line to read $season = 2006.....So either I can declare the variable $season = 2006....or have it make the season = (file directory its in).

Sorry Its hard to describe the real thing im wanting.
A batch to change one line for every file in the directory if it exists($season = XXXX;) Now that $season statement isnt in every file, but most.

Thanks in advance


See More: Changing one line in a php script multiple times?

Report •


#1
December 23, 2012 at 13:56:44
Here's a crude batch that may or may not work consistantly enough to use:
::===== begin batch
@echo off & setlocal
for %%a in (%cd%) do set dir=%%~na
for %%f in (*.php) do (
   find /i "($season =" < %%%f && (
     for /f "tokens=1,2*" %%a in (%%f) do (
       if /i "%%a %%b" equ "($season =" (
         >> tempfil echo ^($season = %dir%;^)
       ) else (
         >> tempfil echo %%a %%b %%c
       )
     )
   )
)
::===== end batch

this is just set up to do one directory (whatever directory it's sitting in). I guess you only do this once a year? so no need to loop it through a series of dirs?
Here's a vbscript that can handle the replacement somewhat better:
'===== begin vbscript SEASON.VBS
set fso=createobject("scripting.filesystemobject")
c=fso.opentextfile(wscript.arguments(0),1).readall
p=instr(lcase(c),"$season = ")
if p>0 then
c=left(c,p+9)+wscript.arguments(1)+mid(c,p+10+len(wscript.arguments(1)))
fso.createtextfile(wscript.arguments(0)).write c
end if
'===== end vbscript
and use it like this (replaced into above batch):
for %%f in (*.php) do find /i "$season =" %%f >nul && cscript season.vbs %%f %dir%

be sure back up any material used for testing, it WILL change the files and might very well mess them up.


Report •

#2
January 2, 2013 at 11:19:52
thanks for the fast reply, and Im sorry Im just now getting to this. Im very new to this, so to respond to nbranes reply
the vbscript

do i write that in notepad?
call it SEASON.VBS?

and im not sure what you mean by this
and use it like this (replaced into above batch):
for %%f in (*.php) do find /i "$season =" %%f >nul && cscript season.vbs %%f %dir%

sorry your having to hold my hand thru this step.
thanks again


Report •

#3
January 2, 2013 at 13:09:17
✔ Best Answer
Yes, notepad, clipbrd, any "plain text" editor. You could cut/paste the code from my post to avoid typos etc.
Here's the batch that uses vbscript, save it as whatever.BAT:
::===== begin batch
@echo off & setlocal
for %%a in (%cd%) do set dir=%%~na
for %%f in (*.php) do find /i "$season =" <%%f >nul && cscript season.vbs %%f %dir%
::===== end batch
Then you also need to cut/save the vbscript as SEASON.VBS (unless you want some other name, but just make sure the batchscript uses the same name as the vbs). Like I said, these are designed to run from the "working" directory.
ps: while I'm at it, i guess I'll go ahead and make a stand-alone vbs:
'===== begin vbscript SEASON2.VBS
set fso=createobject("scripting.filesystemobject")
set folder=fso.getfolder(".")
for each file in folder.files
  n=file.shortname
  p=instrrev(n,".")
  if p>0 then x=lcase(mid(n,p+1)) else x=""
  if x="php" then
    c=fso.opentextfile(n,1).readall
    p=instr(lcase(c),"$season = ")
    if p>0 then
      c=left(c,p+9)+folder.name+mid(c,p+10+len(folder.name))
      fso.createtextfile(n).write c
    end if
  end if
next
'===== end vbscript

but it's not tested. Probably just go with what we already had to avoid confusion.

Report •

Related Solutions


Ask Question