Showing posts with label context menu. Show all posts
Showing posts with label context menu. Show all posts

Monday, June 29, 2009

Error Checking

I was reading the book Mastering Regular Expressions by O'Reilly (BTW a great book and one I'd like to eventually do a review on), one of the initial topics the author spoke about was using regular expressions to check a file for errors. I liked the idea, but typically again we had to open the command prompt to run it and it was just not as "friendly" as I like things to be.

Onward we look to my context menu "hack". Basically what we'll do is write a perl script that will take whatever file was passed to it check it for errors and then when you want you can close it.

Lets look at our order of events here:
  1. Discuss the script
  2. Point you to the how to add to your context menu (we'll need to use the registry editor since windows doesn't seem to like perl scripts in the file types box)
  3. Try it out!
Script:
if (scalar(@ARGV)>0)
{


$file = @ARGV[0];
print "Testing $
file \n";
}
else

{
print "Please drag the file you would like to see errors in onto the screen\n";
$file = <>;

}

open DATAFILE, "$file" or die "Missing $file file.\n";
open (DATAFILE, $file);
@getData = <DATAFILE>;
close (DATAFILE);
$errors = 0;
$lineNumber = 1;

foreach $inputLine (@
getData)
{
if ($inputLine =~ m/\berror[(s]?\b/i)
{
print "$lineNumber ) $inputLine \n";

$errors++;
}
$lineNumber++;
}

print "Complete!\n$errors Errors found!";
$asdf = <>;


Breaking it down:
if (scalar(@ARGV)>0)
{


$file = @ARGV[0];
print "Testing $file \n";
}

else
{
print "Please drag the file you would like to see errors in onto the
screen\n";
$file = <>;

}


What this line does, is checks to make sure you at least have one argument, if you don't then it waits for you to type a file in that you want to check (or as stated you can drag the file onto the window). If you use an argument we'll print the file (in case you didn't actually type the filename in)

Next,
open DATAFILE, "$file" or die "Missing $file file.\n";
open (DATAFILE,
$file);
@getData = ;
close (DATAFILE);

$errors = 0;
$lineNumber = 1;

Open our file get the contents set our error and linenumber variables to zero.

Almost done:
foreach $inputLine (@getData)
{
if ($inputLine =~ m/\berror[(s]?\b/i)
{
print "$lineNumber ) $inputLine \n";
$errors++;
}

$lineNumber++;
}


This rolls through each line of the file (each line is a different item in the array), checks for words that are error OR errors OR error(, prints that line and then increments our error counter as well as we increment our linenumber variable everytime we move through the array.

Lets look at our Regular Expression:
$inputLine =~ m/\berror[(s]?\b/i


Breaking it down it literally means
match the beginning of a word, if it matches error error( or errors and ends the word and any variation of capitalization we can find we will return a positive match.

This can be modified to work with any kind of error or even any kind of other word you might want to look for in a particular file.

The last 2 lines basically keep the window from closing immediately if you haven't setup your system to keep the window open after running.

Next comes updating our registry to handle this!!! Check out this page, it contains a detailed how to modify our registry to add a command to the context menu.
What we'll be looking for is the txtfile entry.
if there isn't a key for shell add one, then add Check_For_Errors (or insert whatever you want to call it), then add one more key for command. This is the command I stuck in for the REG_SZ:
cmd.exe /k "C:\batch\checkErrors.pl %1"

So what that's going to do is open a command prompt and run the following which happens to be our perl script. just substitute the location where you saved your perl script at.

Lets check it out, I tried the above script on a text file containing this text:
This line contains an error.
if I knew any better there would be no errors in this file.
But unfortunately I will always make some kind of Error.
Now I know this line doesn't contain one.
But this one contains one with a ( like this error(
the only problem we'll run into is if we get errors(
Good luck let me know how well it works out for you.

Monday, June 8, 2009

Windows Selected Filenames Pt II

Welcome back, well as we all know, in the words of my good friend Phil:
"But with all programming/design problems – there are a multitude of unique methods that may be employed to removed the dermal layer of the felinus domesticus"
If you don't know how to setup a "right click" menu item for files and folders I would recommend checking out the following posts: Windows Selected Filenames, Command Prompt

Lets get on with it, This "version" of the selected filenames will use the same steps to get it up and running, (right click menu), but the only difference is that now when we have our script setup we'll copy the files to the clipboard, then deslect the files and right click on a single filename and select your copy filenames script, Lets get into the meat of this script,

#! perl
use WIN32::CLIPBOARD;

$text = "";
$CLIP = Win32::Clipboard();

if ($CLIP->IsFiles())

{

@files = $CLIP->GetFiles();

foreach $file (@files)

{
@sp = split(/\\/, $file);

$text = $text .$sp[@sp-1] . "\n";
}
$CLIP->Set($text);

}
Breaking it down again:

First we declare:
use WIN32::CLIPBOARD;
This is so we can use Windows Clipboard functions

Next is
$text = "";
We want a clean text string.

Then,
$CLIP = Win32::Clipboard();
Here we setup the $clip as a clipboard object.

Next,
if ($CLIP->IsFiles())
What this is doing is checking that there are "files" in the clipboard, we want this because otherwise we'll try to write "nothing" to the clipboard and I'm sure that's not a problem, but if for some reason you really didn't mean to do this and you already had text in the clipboard then it won't overwrite your text clipboard contents, either way it won't write to the clipboard if you don't have any files in the clipboard, you can remove this if you want but I haven't tested it out, it may work just fine.

Then,
@files = $CLIP->GetFiles();
This chunk of code goes out and gets the files in the clipboard, and stores them in an array.

Next,
foreach $file (@files)
So what we're going to do is run the next couple of lines for each file (or in this case we'll be grabbing the filename)

Then,
@sp = split(/\\/, $file);
This line splits the "filename" up into segments if you were interested in the whole pathname you could omit this step and just assign the $text variable to whatever $text contained and the $file variable plus "\n", so you get each filename on a new line.

Next,
$text = $text .$sp[@sp-1] . "\n";
This takes the split up line above and grabs the last bit of it, which happens to be the actual filename. Remember if you wanted the entire pathname you could use the $file variable like so,
$text = $text . $file . "\n";

Finally,
$CLIP->Set($text);
This line sets your newly created text to the clipboard!

Give it a try and let me know how it works out for you!

I'm sure this isn't the last post about this subject, as always things are always improving, I am constantly thinking of a way to cut back on the amount of steps to run this little "tool" my plan is to eventually "simulate" the keystroke to "copy" the files then copy the filenames, not sure how to go about it, without running multiple instances, I'm sure hotkeys will be a big help on this, either way I'll be improving this script again in the future...Stay tuned I'm certain there will be more posts on getting this working better.

Tuesday, May 5, 2009

Command Prompt

I frequently find myself needing access to the command prompt when I'm inside a folder, one of the ways I do that is:
1. Click start
2. Click Run (or alternately to get to this Windows Key +R)
3. Type cmd
4. Then Type cd
5. Then drag the folder of the location I want into this window
6. Hit enter (ok so not literally, I didn't hit it)
Today, I'm going to introduce how to use the right click menu to make life easier for those (in windows) to be able to jump to command line pretty easily,

1. Open an explorer window, this can be accomplished by either pressing "Windows key + E" or just opening a folder.
2. Select Tools Folder Options,
3. Click the Tab File Types
4. Select Folder
5. Select Advanced
6. Select New
7. For Action name, Just name it something you'll remember like "command prompt"
8. For the application used to perform this action enter cmd.exe /K cd %1
9. Select Ok's to exit.
10. Try it out, now go to a folder and right click on it and select whatever you named your action.

Time to explain what I had you put in the application used to perform.
cmd.exe opens your command prompt, /K means "run the following" and the following is cd %1, cd means change directory, and %1 means what was passed to the command in this case the folder location (I.E. c:\New Folder).

I hope this helps some people out there, let me know how it works for those of you who try it!!!

Thank you for reading my first "real" post.