Working with Cron Jobs

For a while now, I have been using my shared hosting to backup/store numerous image files. Retrieving those files over the internet has always been a pain. Especially when there are hundreds of files in a given folder. The end goal is to be able to download the archived folder to my computer for easy viewing.

So, I took the time to code something up. Here’s the psuedo code:

  1. zip folder
  2. rename zip to include date stamp
  3. move zip to another destination
  4. delete contents of folder for future backups
  5. give myself a high five

Of course, I Google’d and looked for an easy way out… to see if some has posted the exact code I was looking for. No such luck but here are the links that were handy:

What I learned:

  • Cron Jobs on shared web hosting gives us the ability to run automated shell level scripts.
  • Cron Jobs coding was essentially Unix, nice I can code Unix!

Once I got started, I did run into a few Cron Jobs specifics:

  1. you have to escape the ‘%’ sign with a ‘\’ in a cron job, http://www.webhostingtalk.com/showthread.php?t=678062
  2. bonus: && operator, helped me reduce my script to one line, http://forums.freebsd.org/showthread.php?t=30964

Nevertheless, here’s the script that will hopefully help someone else looking to compress and backup a whole directory then delete its contents:

zip /destination/of/directory/with/date/label/$(date +\%Y\%m\%d).zip /directory/to/be/zipped/* && rm /directory/to/be/zipped/cleared/*

Happy Coding!

The Subtract Join

Subtract Join

I needed a way of selecting all primary key(s) from a certain table (Table_1) that was not used as a foreign key in a sub-table (Table_2). So I went at it with my SQL know-how to no end. It was obvious that I needed to do a little catching up. 🙂

I googled the following:

• all instances of unique id not found in sub-table
• primary key not found as foreign key in sub-table
• select all rows if not exist in sub-table
• sql tutorial
• sql join
• sql reference
• sql relational databases

…and found nothing useful. However, my research did lead me to one conclusion… what I needed was the opposite of a join. I googled for it, it as in “opposite of a join”. What I found was a very useful MS KB, http://support.microsoft.com/kb/136699.

The described “Subtract Join” was exactly what I was looking for and it works! What’s odd is of all the SQL tutorials I ran into, none of them mentioned the SUBTRACT JOIN. That makes me think there might be a better way of doing this query. Is there a better way? Am I simply not using the right keywords when I search? Anyways, if there is I couldn’t find it…

Batch BOL/EOL Editing

I needed a way of adding periods to the end of every line in text documents. The text documents in question had 100+ lines, so going through each document and pressing ( END > . > ENTER ) repetitively was not ideal. I needed a program, but after some brief googling I gave up and decided that I needed to write this program. I started my project by getting some advice and direction, here, and I am glad I did or else I would have went with my original plan of working with “regex” 🙁 …

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim oFile As System.IO.File
Dim oWrite As System.IO.StreamWriter
Dim Needy As String = tbx_main.Text
Dim Added As String = Regex.Replace(Needy, “(\p{P})(?=\Z|\r\n)”, “.”)
oWrite.Write(Added)
oWrite.Close()
oWrite = oFile.CreateText(“C:\Text_to_Speech.txt”)
End Sub

The end result is a little program that adds any desired character(s) to the beginning or end of all lines pasted into its textbox. The code is as follows…

Try
‘Make sure big tbx is not empty
If tbx_main.Text = “” Then
tbx_main.Text = “Please enter text!”
ElseIf tbx_main.Text = “Please enter text!” Then
tbx_main.Text = “Please enter text!”
Else
‘Make sure small tbx is not empty
If tbx_desired.Text = “” Then
tbx_desired.Text = “Char?”
ElseIf tbx_desired.Text = “Char?” Then
tbx_desired.Text = “Char?”
Else
‘Declare: file, stream and savefiledialog
Dim oFile As File
Dim oMyStream As Stream
Dim saveFileDialog1 As New SaveFileDialog
‘Set savefiledialog defaults
saveFileDialog1.Filter = “txt files (*.txt)|*.txt”
saveFileDialog1.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True
‘Run nested code if user presses OK button in dialog
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
oMyStream = saveFileDialog1.OpenFile()
Dim oWrite As New StreamWriter(oMyStream)
For Each LineInput As String In tbx_main.Lines
‘Core Code, add periods to beginning or end
If rbtn_begin.Checked = True Then
‘Beginning
oWrite.WriteLine(tbx_desired.Text + LineInput)
ElseIf rbtn_end.Checked = True Then
‘End
oWrite.WriteLine(LineInput + tbx_desired.Text)
End If
Next
oWrite.Close()
End If
‘Periods added, display success msgbx, close form
MessageBox.Show(“Success: file was created!”)
Me.Dispose()
Me.Close()
End If
End If
Catch exc As Exception
‘Catch Errors, display messagebox
MessageBox.Show(“Error: file was not created!”)
End Try

Sounds useful? If you want to download the program and use it at your own risk, click here.

YouTube Enabled

With the YouTube craze spreading like a wildfire, last summer (’07) I took some time to enable Noteful.COM with YouTube. Users can now visually depict their thoughts using YouTube videos. The code wasn’t hard and thanks to C.K. Sample’s blog (here), it was even easier…

<object type="application/x-shockwave-flash" data="[INSERT VIDEO URL FROM EMBED CODE PROVIDED HERE]" width="400" height="326"><param name="movie" value="[INSERT VIDEO URL FROM EMBED CODE PROVIDED HERE]" /><param name="FlashVars" value="playerMode=embedded" /></object>

With this code I can also allow users to embed Google Videos as well, this would have to be done soon before I fall behind. More and more video streaming sites are hitting the web that are a must for me. Stage6, for example is on the top of my list. If anyone has any advice on this, please comment. 🙂

ASP.net 1.1 Repeater Columns

While writing a script to mass approve posts on a site, similar to deleting all unwanted email using: a repeater (not a datagrid), a checkbox in each indexItem, and a button; I ran into a common problem for many… ASP.net repeaters don’t have columns. I searched the net and visited two of my favorite forums looking for help and came up with nothing…

Here’s my Psuedo/Logic:
1) Load Repeater with list of “UNmoderated” items
2) Put a checkbox inside the repeaters ItemTemplate
3) Add code to button (dosomething if checkbox is checked)

Problem:
I couldn’t locate the column with the PrimaryUniqueID because there are no columns in repeaters, but my Stored Procedure “requires” the PrimaryUniqueID.

Button code:
Private Sub MassApprove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MassApprove.Click
Try
Dim MyRptItem As RepeaterItem
For Each MyRptItem In MyRepeater.Items
Dim chbxMassApprove As CheckBox = CType(MyRptItem.FindControl(“chbxMassApprove”), CheckBox)
Dim PrimaryUniqueID As String = MyRptItem.DataItem(PrimaryUniqueID) PROBLEMATIC LINE
If chbxMassApprove.Checked = True Then
‘DO THE MASS APPROVE
‘LOOP THRU STORED PROCEDURE
End If
Next
Catch exc As Exception
Response.Write(exc.ToString)
End Try
End Sub

Solution:
I used this as a workaround if this helps anyone…

Front End:
Text='<%# DataBinder.Eval(Container, “DataItem.PrimaryUniqueID”) %>’

Back End:
Dim PrimaryUniqueID As String = ckbxMassApprove.Text.ToString

Code Commenting

I recently took full control of a clients website.  And as I was doing a few requested changes, I kept running into coding difficulties.  My main problem was that I had to spend a substantial amount of time figuring out what was changed for what reason.  If not do it for the next webmaster :), do it for yourself… comment on your coding. A nicely documented page of code will always be read twice as fast!

ASP.net Paging

So, the users of a certain website of mine have been requesting that I add paging to one of my controls. I initially didn’t want to do it, not because I couldn’t do it. But because the control didn’t really call for it. But they are my users, without them the site wouldn’t be there right? So I went ahead with it. Its been a while since I used any paging code, so I was refreshed after reviewing ASP.net paging webcontrols:

System.Web.UI.WebControls.PagedDataSource

What we have to do for us to paging through our data vs. displaying all of it at once? We simply inject the paging into our data before displaying it. In my case, I am using a repeater:

'get the data
Dim myData As System.Collections.ArrayList
myData = ** myStoredProcedure **
'ensure there is data to work with
If myData .Count > 0 Then
'create a new instance of PagedDataSource
myPagedDataSource = New System.Web.UI.WebControls.PagedDataSource
'set my pagin preferences
myPagedDataSource.DataSource = myData
myPagedDataSource.AllowPaging = True
myPagedDataSource.PageSize = 5
myPagedDataSource.CurrentPageIndex = myCurrentPageIndex
'then finally bind the data to my repeater
myRepeater.DataSource = myPagedDataSource
myRepeater.DataBind()
End If

Quite simple to use. Knowing that the webcontrol is there really is the hard part of it all.

osCommerce Must Have…

I have been doing alot of osCommerce work for a client over the pass year and a half. And although there are many “contributions” that are a must for any production osCommerce shop, without a doubt when it comes to designing a site, the “Simple Template System” aka STS contribution created by Brian Gallagher is a must have.

If you are new to osCommerce, you will soon find out that designing your osCommerce website can be quite painful. But once you add the STS contribution to your site, you essentially add a skinning method to it… making your job half as easy. Link? Click Here.