Tuesday, June 29, 2010

iPhone OS 4.0 upgrade. Lost my data and got it back

I recently tried to upgrade my iPhone 3Gs to iPhone OS4.0 using windows

Here's what happened

1. iTunes updated itself.
2. iTunes Backed up iPhone
3. iTunes attempted to upgrade phone.
4. iTunes hung half way through install
5. I rebooted and restarted iTunes and the install completed.
6. iTunes attempted to restore the old backup.
Result: all my data was gone. (texts, emails, notes, photos)... very disappointed.

Next:
I repeated the process.... and tried to reinstall an old backup....

Result:
No luck, turns out the new iTunes update deleted all my old backups ... not happy

How i fixed my phone:
Luckily I use Windows 7 and used the Previous Versions functionality to retrieve old backups.

1. I located the backup folder (usually C:\Documents and Settings\USERNAME\Application Data\Apple Computer\MobileSync\Backup)
2. Right click the backup folder, goto properties>Previous Versions
3. Hopefully you'll see a old version of the backup from a time before you started the process (it should be a large file, mine was 185MB).
4. Copy this file into the Backup folder.
5. Open iTunes and attempt to Restore the backup.

Result:
My phone was updated to OS4 and all my contacts,photos,texts and notes were restored :)

Friday, June 18, 2010

Add a URL to google search

Go to the following page and enter your details:
http://www.google.com/addurl/?continue=/addurl

Hopefully you'll see your site in a few days.

Tuesday, June 15, 2010

Measuring Execution time with powershell

This is an easy one - use measure-command CommandLet

measure-command {
# do your long running action in here, like loading a file
@file = get-content "c:\MyFile.txt"
}

Output Looks like this:
Days : 0
Hours : 0
Minutes : 0
Seconds : 1
Milliseconds : 638
Ticks : 16386148
TotalDays : 1.89654490740741E-05
TotalHours : 0.000455170777777778
TotalMinutes : 0.0273102466666667
TotalSeconds : 1.6386148
TotalMilliseconds : 1638.6148

You can also format the output by piping the output into the Filter-Table commandlet

measure-command {
# do your long running action in here, like loading a file
@file = get-content "c:\MyFile.txt"
} | ft -Property Hours, Minutes, Seconds, Milliseconds

Output Looks like this:
Hours Minutes Seconds Milliseconds
----- ------- ------- ------------
0 0 1 567

PowerShell and Large Files

I recently needed to write a script to get some data from a XML file, in this case all IDs of Topic Elements. Decided to use Powershell but quickly ran into an issue due to the size of the uncompressed file... 127MB. Here's my initial script:

# Get the ids
$File = .\myFile.xml
[xml]$topic = get-content $File
$arrayTopicIds = $topic.TopicResults.Topic | %{$_.Authors.Author} | %{$_.Id}

Needless to say, it consumed all my memory and froze my machine.

Undeterred, I turned to the -FilterScript. Rather than use an XML object, I used the get-content commandlet to load the file into an array of end-line-delimited strings. And since i knew the pattern i was looking for i was able to use the -FilterScript command to filter the processed lines. At the same time also formatted the string to how i wanted it:

# Get the ids
#$topic = get-content $File | Where-Object -FilterScript { $_ -ilike “*Author id=*” } | %{ $_ - replace "" } | %{ $_.Trim() }

Simple and ended up running in under 4 minutes. Which was nice.

Thursday, February 25, 2010

Changing the Powershell prompt

I wanted a way to change the powershell prompt, in particular to:
- Display the current path in the Shell window
- Change the prompt to "[LineNumber]:"
- Show the stcck level when "pushd" is used

Then a colleague showed me this.

Add the following function to your current profile - it's an override for the prompt method:

function prompt {

#the old script...

#function prompt { "$" }



# FIRST, make a note if there was an error in the previous command

$err = !$?



# Make sure Windows and .Net know where we are (they can only handle the FileSystem)

[Environment]::CurrentDirectory = (Get-Location -PSProvider FileSystem).ProviderPath

# Also, put the path in the title ... (don't restrict this to the FileSystem

$Host.UI.RawUI.WindowTitle = "> {0} ({1})" -f $pwd.Path,$pwd.Provider.Name



# Determine what nesting level we are at (if any)

$Nesting = "$([char]0xB7)" * $NestedPromptLevel



# Generate PUSHD(push-location) Stack level string

$Stack = "+" * (Get-Location -Stack).count



# my New-Script and Get-PerformanceHistory functions use history IDs

# So, put the ID of the command in, so we can get/invoke-history easier

# eg: "r 4" will re-run the command that has [4]: in the prompt

$nextCommandId = (Get-History -count 1).Id + 1

# Output prompt string

# If there's an error, set the prompt foreground to "Red", otherwise, "Yellow"

if($err) { $fg = "Red" } else { $fg = "Yellow" }

# Notice: no angle brackets, makes it easy to paste my buffer to the web

Write-Host "[${Nesting}${nextCommandId}${Stack}]:" -NoNewLine -Fore $fg



return " "

}
The easiest way to do this is:
1. In PS type "$profile" this will show you the location of the current active profile
2. Open that file in notepad and paste the function above into it
3. In PS type ".$profile" to reload the altered profile

Wednesday, February 17, 2010

A Powershell GREP

Here's a handy one for doing a GREP in PowerShell

Example 1
Get-ChildItem -include *.txt -recurse | Select-String "Hello"

Example 2
Get-ChildItem -include *.txt -recurse | Select-String "Hello" | Format-Table

Example 3
Get-ChildItem -include *.txt -recurse | Select-String "Hello" | Format-Table -Property Path, LineNumber, Line -Autosize

This gets a collection of all "txt" files in the current directory and its sub directories, passes it to the "Select-String" which opens the file and look for the pattern "Hello".

The Format-Table option allows you to display the results in a more readable format. You can also use "Format-List"

In example 3 I added the AutoSize property so the Path info wouldn't get truncated.

Tuesday, February 16, 2010

Using jQuery to add a templated row to a html table

I wanted a quick easy way of adding rows to a table - preferably by designing the layout in html and then copying to where i needed it and changing the elements values where needed. This took me longer to figure out than it should have - so I thought I'd share it.

The snippet copies a row from one table and inserts it to the target table.
<table id="dataTable">
</table>
<table id="templateTable" cellspacing="0" style="display: none;">
<tr id="templateRow">
<td id="nameRow" class="rowItem" width="25%">
<a></a>
</td>
<td id="positionRow" class="rowItem" width="60%">
</td>
<td id="numberRow" class="rowItem" width="15%">
</td>
</tr>
</table>

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).click(function() {
addNewRow("mailto://johnny@here.com", "John Ryan", "Developer", "555 123456");
addNewRow("mailto://johnny@here.com", "Bill Smith", "Developer", "555 768648");
addNewRow("mailto://johnny@here.com", "Buck Rogers", "Developer", "555 675843");
addNewRow("mailto://johnny@here.com", "Frank Wlliams", "Developer", "555 675843");
});

// Copy the template row and insert it to the target table.
function addNewRow(link, name, position,number){
var clonedRow = $('#templateRow').clone(true);
$("#nameRow a", clonedRow).attr("href", link);
$("#nameRow a", clonedRow).html(name);
$("#positionRow", clonedRow).html(position);
$("#numberRow", clonedRow).html(number);
$(dataTable).append(clonedRow);
}
</script>