Archive for the 'programming' Category

PERL script that does a search and replace on all files in folder

Here is a script I wrote a few days ago to make a mass change to one of my sites. I write PERL scripts often, but this one seems like one that I’ll be using again in the future.

Copy this PERL script to the folder you want to process. A backup will be made of all changed files.

For more regular expressions that you can use, search the regular expressions library: http://regexlib.com/

# Takes all files in folder (filter by filename), and uses regular expression to find a string to replace.
# If string exists, it makes a backup (appends .bak extension) and does replacement
@files = <*>;

# regular expressions allowed

# only change these files, $filefilter = "*" for all files
$filefilter = "\.php";  

$searchfor = "google\.com";
$replacewith = "strivingafterwind\.com";

foreach(@files){
	$filename = $_;
	if($filename =~ /\.php$/i){

		open(MYINPUTFILE, "<$filename");
		@all = <MYINPUTFILE>;
		close(MYINPUTFILE);

		$completefile = join("",@all);

		if($completefile =~ /$searchfor/s)
		{
			$backupfilename = $filename.".bak";
			rename($filename, $backupfilename) || die "Couldn't rename: $filename to $backupfilename!\n"; 

			$completefile =~ s/$searchfor/$replacewith/gs;
				# /g to replace all occurances.
				# /s for single line mode (since the file is all one string now)

			#print $completefile;
			open(MYOUTPUTFILE, ">$filename");
			print MYOUTPUTFILE $completefile;
			close(MYOUTPUTFILE);
		}
	}
}

Since I’m running this scripts in Windows, you might need to add the perl shebang (”#!/usr/bin/perl”) at the top if you plan to run this on linux.

Simple Calendar Generator Using ASP

Someone from work was using an excel spreadsheet for making a simple calendar which displays the day of the year. every year, this calendar was updated, mostly by hand. one day, someone else requested this calendar for prior years, and we didn’t have them. so this generator was born. and man, was it simple to make.

note: in the app, i am using “julian day” to mean “day of the year” even though it isn’t technically correct. it’s just that everyone seems to make that association.

below is the ASP version, but I also converted this to PHP, and will post it when I get a chance.

ASP CODE

the main form just asks for a year, but it also automatically displays links for the past 4 years and the future 2 (to save the user the hassle of typing in the year).

calendargenerator.asp




To generate a simple printable calendar with days-of-the-year, click on the year below:


<%
	Dim iYearTmp
	For iYearTmp = Year(Now()) - 4 to Year(Now()) + 2
	Response.Write "” & iYearTmp & ” Calendar” & vbCrLf
	Next
%>


Or enter a valid year in the form. If you provide an invalid date, the current year will be used.

Enter 4-Digit Year:

the main form gets submitted to the page below. this page does all the work.

displaycalendar.asp

<% Option Explicit 

	Function IsLeapYear(iYear)
		If (iYear mod 4 = 0) AND ((iYear mod 100 <> 0) OR (iYear mod 400 = 0)) then
		    ' It is a Leap Year
		    IsLeapYear = True
		Else
			IsLeapYear = False
		End if
	End Function

	Function getDaysInMonth(iYear, iMonth)
		Dim strDays
	    Select Case cint(iMonth)
        Case 1,3,5,7,8,10,12:
			strDays = 31
        Case 4,6,9,11:
			strDays = 30
        Case 2:
			If IsLeapYear(iYear) then
			  strDays = 29
			Else
			  strDays = 28
			End If
	    End Select
	    getDaysInMonth = strDays
	End Function

	Sub CreateMonth(iYear, iMonth)

		Dim iDayCtr
		Dim iCellCtr
		Dim iMaxDays
		Dim iJulian
		Dim iStartWeekDay

		iMaxDays = getDaysInMonth(iYear, iMonth)
		iStartWeekDay = Weekday(DateSerial(iYear,iMonth,1))
		iJulian = DatePart("Y",DateSerial(iYear,iMonth,1))
		iDayCtr = 1
		iCellCtr = 1

		With Response
			.Write "<table class='monthtable' cellpadding=1 cellspacing=0 border=0>" & VbCrLf
			.Write "<tr><td colspan=7 align=center><font class='monthname'>" & MonthName(iMonth) & "</font></td></tr>" & VbCrLf
			.Write "<tr><td class='tdweekday'>S</td><td class='tdweekday'>M</td><td class='tdweekday'>T</td><td class='tdweekday'>W</td><td class='tdweekday'>TH</td><td class='tdweekday'>F</td><td class='tdweekday'>SA</td></tr>" & VbCrLf
			.Write "<tr>" & VbCrLf

			' We might not even need empty cells at first
			If iStartWeekDay > 1 Then
				While iCellCtr < iStartWeekDay
					.Write "<td></td>" & VbCrLf
					iCellCtr = iCellCtr + 1
				Wend
			End If			

			' We should not have counted that last one, so substract
			iCellCtr = iCellCtr - 1

			While iDayCtr <= iMaxDays

				.Write "<td class='tdday'><b>" & iDayCtr & "</b><br><div style='text-align: right'><i>" & iJulian & "</i></div></td>" & VbCrLf

				iCellCtr = iCellCtr + 1
				iDayCtr = iDayCtr + 1
				iJulian = iJulian + 1

				If iCellCtr Mod 7 = 0 Then
					.Write "</tr><tr>" & VbCrLf
				End If

			Wend

			While iCellCtr Mod 7 <> 0
				.Write "<td></td>" & VbCrLf
				iCellCtr = iCellCtr + 1
			Wend

			.Write "</tr>" & VbCrLf
			.Write "</table>" & VbCrLf

		End With
	End Sub

	Dim SCRIPT_NAME
	SCRIPT_NAME = Request.ServerVariables("SCRIPT_NAME")

	' Various stuff used throughout
	Dim sUserName
	Dim strSQL
	Dim iYear
	Dim iJulian

	sUserName = Trim( Mid( Request.ServerVariables("LOGON_USER"), 12, 32 ) )
	iYear = Trim(Request.QueryString("iYear"))
	If Not IsNumeric(iYear) Or iYear = "" Then iYear = Year(Now()) End If

	'Response.ContentType = "application/vnd.ms-excel"
	'Response.AddHeader "content-disposition", "attachment; filename=Calendar" & iYear & ".xls"

	%>
		<html>
		<title><%=iYear%> Calendar</title>
		<head>
		<style type='text/css'>

		.monthtable {
		border : 0px;
		border-spacing : 0px;
		font-family : Arial;
		font-size :	8pt;
		width: 210px;
		border-collapse: collapse;
		}

		.tdday {
		padding: 0px 0px 0px 2px;
		border: 1px solid black;
		border-spacing: 0px;
		font-family: arial;
		font-size:	8pt;
		width: 70px;
		border-collapse: collapse;
		}

		.tdweekday {
			/*padding: 2px 2px 2px 2px;*/
			border: 1px solid black;
			border-spacing: 0px;
			font-family: arial;
			font-size:	8pt;
			width: 70px;
			text-align: center;
			border-collapse: collapse;
		}

		.monthname {
		font-family: arial;
		font-size :	10pt;
		font-weight: bold;
		}
		</style>
		</head>
		<body style="margin: 0px 0px 0px 0px">
	<%
	With Response
		.Write "<table border=0 cellpadding=5 cellspacing=0><tr valign=top><td colspan=3 align=center><font face=arial size=+4>" & iYear & "</font></td></tr>"
		.Write "<tr valign=top><td>"
			CreateMonth iYear, 1
		.Write "</td><td>"
			CreateMonth iYear, 2
		.Write "</td><td>"
			CreateMonth iYear, 3
		.Write "</td></tr>"
		.Write "<tr valign=top><td>"
			CreateMonth iYear, 4
		.Write "</td><td>"
			CreateMonth iYear, 5
		.Write "</td><td>"
			CreateMonth iYear, 6
		.Write "</td></tr>"
		.Write "<tr valign=top><td>"
			CreateMonth iYear, 7
		.Write "</td><td>"
			CreateMonth iYear, 8
		.Write "</td><td>"
			CreateMonth iYear, 9
		.Write "</td></tr>"
		.Write "<tr valign=top><td>"
			CreateMonth iYear, 10
		.Write "</td><td>"
			CreateMonth iYear, 11
		.Write "</td><td>"
			CreateMonth iYear, 12
		.Write "</td></tr></table>"
	End With
%>



Categories

Calendar

November 2008
S M T W T F S
« Mar    
 1
2345678
9101112131415
16171819202122
23242526272829
30  

Random Image

Now Reading

None