Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I run a script to process a CSV file once an hour. At the beginning of the script, I need to exit out if the CSV file has not changed. Let's pretend that file timestamps aren't an option. (I'm asking this question for educational value)

I was considering slurping in the entire file and computing the hash on the contents, like so:

$fileData = get-content \path\to\file
$hashCode = $fileData.GetHashCode()

I would then save those contents from run to run, and if the hash is the same on subsequent runs, exit out of my script.

Is there a better way to do this, again, assuming file timestamps are not available?

share|improve this question

2 Answers 2

up vote 5 down vote accepted

You'll end up using the System.Security.Cryptography namespace to generate that hash. The PowerShell Community Extensions have already done the work for you. There is a Get-Hash cmdlet that returns a HashInfo object you could use for your comparison.

share|improve this answer
    
That is awesome - thanks. This is what I was looking for. –  Larold Sep 30 '11 at 17:41

One thing to note, the Get-HashCode() is not meant to be used as a unique object identifier. It will always be different every time you run it, even in the same session.

Check here and here for more information.

share|improve this answer
    
Awesome - I had no idea. That is valuable info - thanks! –  Larold Sep 30 '11 at 17:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.