Showing posts with label command line. Show all posts
Showing posts with label command line. Show all posts

Tuesday, July 7, 2009

Perl scripting


So I was checking out Daniweb and trying to answer some questions, and I came across this one:
Sort Files by Date

Since I'm slowly learning perl, I thought why not try:
@someData = `command`;

I tried ls -t which happens to be the unix command to list the directory contents in time order.
After some searching for information for a bit about dir so we can see if this works on windows, I found out you can use the dir /od command instead.
From MSDN

Here's the tidbit of code:

@x = `ls -t`;

foreach $x (@x)
{
print $x;
}
$asdf = <>;

The @x holds the return output from the command and the ls -t can be substituted for dir /od on a windows computer.

I then printed each line, which of course can be redirected to a file, or however you wish to use it for handling.

The $asdf = <>; serves as a "pause" in the script so the script doesn't start run and close before you can see anything.

Let me know in the comments if you can run ls -t from your windows computer standard using perl...

Sunday, June 14, 2009

Random Post: "Handbrake Perl Script"


I have Google Analytics setup on my blog and I can see the keywords that have actually brought people to my site.... I find this interesting and this inspires me...If it's obscure, why not try to "guess" what the person was searching for and then do a post on that subject, if it's pretty straight forward,why not solve it..
Today's keyword subject is : perl script handbrake windows

This one doesn't look too bad.

Per Handbrake.fr's site:
HandBrake is an open-sourced, GPL-Licensed,multiplatform, multithreaded, video transcoder, available for Mac OS X, Linux, and Windows
Looking at the documentation on handbrake's site, there is a command line interface, so we can use that to make any calls to the handbrake program.

The basic command we are going to use would be:
HandBrakeCLI -i source -o destination
So we can immediately start with a perl script like this:
$source = "sourcefile";
$destination = $source . "_converted";
print `HandBrakeCLI -i $source -o $destination`;

This will simply take whatever specified source file we have and output to the destination location.

But if that was all we wanted to do we wouldn't need a perl script would we?

How about if we have a list of files in a document (along with paths)?

We can open that document grab all the paths then simply do a mass conversion!

The final "guess" would simply grab all files of extension x and convert them and dump them in a folder. Again this isn't hard as well.

Lets start with this one, it shouldn't be too bad, of course we'll want to do some error checking, such as if we already have a converted filename of the same type then we definitely want to hold off on converting.
Regardless let's see what we can come up with real quick!
@file_list = <*.mpg *.avi *.vob>;
@converted_list = <*.mp4>;
$extension = ".mp4";
Here we are grabbing a list of all the files we might want to convert, note that you can update that list to contain whatever extensions you might have/like to use. We also grab the list of converted files in there just to do some error checking (in case you leave all your files in the same folder), also the output type should match the type of files you are trying to convert to. We also should define our extension for later use!

Now it's a matter of rolling through each item in the filelist, so we'll start a for loop:
foreach $file (@file_list)
{
@filename = split(/./, $file);
$exists = 0;
}
Something to note is that we want to do a search on the filename in the converted files so in order to do that we'll need to break up the filename from the extension.

Now inside this loop we'll want to do a quick check to see if the file we have a handle on is already listed
foreach $converted (@converted_files)
{
if ($converted =~ m/@filename[0]/i)
{
$exists = 1;
}
$baseName = @filename[0];
}
So now we've done a check and if we have a file that our converted files contains.

What we can now do is make an if/then that will basically skip the file altogether if we see that $exists contains a 1!
if ($exists == 1)
{
print "File already converted!!";
}
else
{
$source = "$file";
$destination = $baseName . "_converted" . $extension;
print `HandBrakeCLI -i $source -o $destination`;

}
Something to remember:
If you want to save to a separate folder I would recommend doing a check for whether that folder exists rather then grabbing a list of files in the current directory.
Entire Script (small text...just copy paste):
@file_list = <*.mpg *.avi *.vob *.AVI>;
@converted_list = <*.mp4>;
$extension = ".mp4";


foreach $file (@file_list)
{
@filename = split(/\./, $file);
$exists = 0;

foreach $converted (@converted_list)
{
if ($converted =~ m/@filename[0]/i)
{
$exists = 1;

}
$baseName = @filename[0];
print @filename[0] . "\n";
}

if ($exists == 1)
{
print "File already converted!!";
}
else
{
print "Converting";
$source = "$file";
$destination = $baseName . "_converted" . $extension;
print "Ready?\n";
$asdf = <>;
print `HandBrakeCLI -i $source -o $destination`;
}
}

The only difference between the above script and one that will open a document and pull all the filenames from it is that you would use the command:

open DATAFILE, "$inputfilename" or die "Missing $inputfilename file.\n"; open (DATAFILE, $inputfilename);
@file_list = ;
close (DATAFILE);
The above would replace the line:
@file_list = <*.mpg *.avi *.vob>;
That's really the only difference!

Well let me know what you think in the comments and if you happen to have found this site looking for just this random post please let me know!!!

Wednesday, May 6, 2009

Batch Scripts Perl scripts and Bears Oh My!!!

I don't know but maybe I'm the only one who has these problems but I don't know how many times I'll run a batch script and find that it's managed to start run and close before I even saw the screen come up, now of course you can get around this by putting a pause in your script, but honestly I don't always want to pause I just want to see that it successfully completed and not wait for me to hit enter.

One way to work around this is by following this steps:

Purpose: The point of this is to make the window stay open when you run a batch file.
  1. Windows Key + R
  2. type regedit in the window
  3. Back up Registry
    1. File
    2. Export>
    3. Save somewhere you can find it in case you need to revert
  4. Click the Plus sign next to HKEY_CLASSES_ROOT
  5. scroll to batfile
  6. Click the little plus next to it
  7. Click the little plus next to shell
  8. Click the little plus next to open
  9. Click the folder command
  10. For the REG_SZ on the right window double click the (Default)
  11. Make sure the string looks like this:
    • "cmd.exe" /k "%1" %*


Now to explain, what I have set it to do is to basically keep the window open after running so you are returned to a command prompt.

Another place I find this to be very helpful is for example if you're using perl scripts right?

How many times do you run a perl script (to test) just by double clicking on it, and it opens calls an error and closes before you get a chance to even look at it?

Almost all the same steps as you'll see:
  1. Windows Key + R
  2. type regedit in the window
  3. Back up Registry
    1. File
    2. Export>
    3. Save somewhere you can find it in case you need to revert
  4. Click the Plus sign next to HKEY_CLASSES_ROOT
  5. scroll to Perl
  6. Click the little plus next to it
  7. Click the little plus next to shell
  8. Click the little plus next to open
  9. Click the folder command
  10. For the REG_SZ on the right window double click the (Default)
  11. Make sure the string looks like this:
    • "cmd.exe" /k "perl %1" %*
Of course yours may have looked a little different BEFORE but in order to enable the window to stay open when you run the script you basically use the same thing what we're saying is open a command prompt (and stay open), /k says "run the following command" which happens to be the perl %1 and as a reminder the %1 is a parameter passing in what was called to it.

The beauty of this is it can be applied to almost any script/exe etc. additionally you could add other arguments, for example if you want your perl scripts to always run in "debug" mode to give you the most information when you run a script (in case it fails) that would be a -w right in front of the %1:
  • "cmd.exe" /k "perl -w %1" %*
I hope you enjoyed and let me know how it works for you!!!

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.