Here are some BATCH script tips which I discovered on the web that helped me a lot in Windows.
Why did I use BATCH scripts? Why not Ruby or Python, or even JScript? Well, if you're writing some basic automation which should be run on deployment PCs, etc. BATCH files are standard and sufficient enough to work. Installing Ruby, etc. on all the machines first is a pain and an unnecessary step (IMO).
I am keeping this for future reference. There is a website that is dedicated to this kind of thing: http://www.robvanderwoude.com/
Now I assume you have basic BATCH scripting knowledge.
Hiding Commands
If you want to hide all the ECHO and COPY, etc. commands from being displayed, insert this in the top line of your script file:@ECHO OFF
Printing an Empty line
Sometimes you want to produce an empty line in the console output. This can be accomplished by:ECHO.
Note: The . (DOT) is directly after the ECHO command and has no spaces in between!
Variables
If you have a variable to hold certain values, to set it you type:SET MYVAR=SOME VALUE
You would preferably type the value without quotes, since the quotes will be included in the value.
Using a value:ECHO %MYVAR%
Which should output:SOME VALUE
User input
If you would like the user to type in something, you use the /P option of the SET command:SET /P CHOICE=Type a letter and ENTER:
This will output:Type a letter and ENTER:
Now the user can type in something and press ENTER. This will store the typed in text in the variable CHOICE.
Substring
If you want to take a substring from a variable, you can do so by:%CHOICE:~0,1%
This will take the 'User Input' sample's CHOICE variable's 1st character. As you have noticed, it starts with the variable's first index (0) and by using length (1), like in Java's substring() method, you will get the first character of the variable.
If statements
Now, if you want to do a check by comparing a variable's value by a certain string, you can make use of the IF command.IF /I '%CHOICE%'=='A' GOTO CHOICEA
IF /I '%CHOICE%'=='B' GOTO CHOICEB
CHOICEA and CHOICEB are just labels to where the batch process should jump to if the given conditions are true. It doesn't have to be GOTO statements, it can be single statements, such as ECHO or SET. The /I option tells the IF statement to do a case-insensitive comparison. If you compare your variable against a string, you have to place it inside quotes, like above.
Labels
A label is a place to where the batch processor jumps to after executing a GOTO statement. Example::CHOICEA
Current path variable
This will output the current directory:ECHO "%CD%"
Path of batch file
Sometimes you want to work relatively from the path of the batch script file, rather than the current directory.ECHO %~dp0
This will output the path of the batch file that is running.
Friday, May 09, 2008
Batch Scripting Tips
Posted by LA at 12:57 PM 0 comments
Keywords: automation,BATCH,scripting,tasks
Thursday, May 01, 2008
Ruby and Win32 API
If you are using Ruby as an automation process rather than a development tool, you sometimes might want to use some of Windows' GUI controls, such as a message box or folder browser box. In those cases, you might want to try the DL library.
Some of you have read this post about OLE Automation and DL. In a way it is quite simple but it can take a while to get right, especially if you're a beginner like me. However, after some time I have managed to get my folder browser to work and I haven't found Ruby code on the web that does this yet.
However, I feel like it is rather simple to do and to find out how to make your own version, all you need to do is to see how people who wrote VB code do it.
Why? Because it is that straight forward, in fact, Ruby reduces the amount of lines of code to do it.
I must warn and say that doing this kind of programming requires excessive use of the Win32API docs (one form or another) and some skilled programming, especially in the C-language area, because you are interfacing to the actual C-functions from Ruby.
Many VB code snippets that does this, have referenced these 2 c-functions (from the shell32 DLL):
They also require a structure to send data to the BrowseForFolder function.
Here is a site to get you started.
Why don't I just paste the code? Well, actually the method I wrote to bring up the dialog box, requires a whole Module and I would like to write my own Ruby module before I publish anything.
Yeah, I might sound like a bugger but hey, I have posted lots of lines of code on this site before so... take it like a man! ;)
The hardest part is creating a Ruby C-like structure from the VB structure, so I think I'll help you out on that one:
ptr = DL.malloc(DL.sizeof('LLSSLLLL'))
ptr.struct!('LLSSLLLL', :br_hOwner, :br_pidRoot, :br_displayName, :br_title,
:br_flags, :br_fn, :br_lparam, :br_iImage)
Now don't say I didn't give you anything! :D
For those who still don't know what all those L's and S's are for, it is basically saying "This is a Long integer type" or "This is a string type". Its to determine the size of the structure, but you know that there are other data-types so you need to dig in on your own regarding that.
Now there is one thing I'm having trouble with and it is the Open/Save File Dialog box. Aparently if you take the exact same data-types as the prescribed docs say, Ruby fails to bring it up. After hours of struggling, I decided to skip the VB code and look on the web if someone already did this, and to my "surprise", nobody has posted any code on this. Bummer! So I decided to have a look on the MSDN site, and BAM! I found my answer. The reason why Ruby didn't bring up my dialog box was due to the fact that Ruby created a structure that had a different size than what the function was expecting! So now I am trying to find out where, how and why.
I might post my code and findings at a later stage, for this is a necessary thing for Windows Ruby Automation Programmers, like me! ;)
Posted by LA at 8:43 PM 0 comments
Keywords: automation,productivity,programming,ruby,scripting,tasks,windows
Wednesday, April 30, 2008
My "facial expression"
Someone sent me this link which I rather think is funny.
Computer languages and facial hair
Hmmm... good thing I'm growing mine! :D
Posted by LA at 5:41 PM 0 comments
Keywords: funny
Netbeans 6.1
As if Netbeans 6.0 wasn't enough to patiently wait for (not in a bad way), the new 6.1 is ready for download/order!
Check this link out to find out more of what they did in this version.
Posted by LA at 5:25 PM 0 comments
Keywords: programming,software
AutoIT Script for EasyWorship
EasyWorship (EW) is a wonderful app for church services and beats Powerpoint in that it can display Powerpoint slides on a different monitor (than your primary for the projectors) and you can view all the slides in advance of what it beholds. This is great unless you want to import some old songs from a Word document or Powerpoint slides into its Song database...
Well why not just import the Powerpoint slides and use that? Well in my case (as I've been doing this at my church for over a year now) this can take a long time in preparation and is very error prone. What we usually did was create a Powerpoint slide and create our whole church service presentation in it, which meant opening up the Word or Powerpoint Doc with the original songs inside, search for it and then copy-paste. Sometimes we accidentally took the wrong songs (like Psalm 256 instead of 265) and found out in the Sunday morning service! :D
With the EW Song DB, you can type in the song title and you just drag it into your schedule. Thats it! Save me about ... hmm... 3-8 minutes per song!
Also this means that if we imported the wrong song and found out the Sunday morning, we can correct it in a matter of seconds!
We already have all our Psalms and Songs on Word Doc and Powerpoint and its not 1 song per doc, its a hundred at least! EW can only import 1 thing at a time and they haven't released a tool that can do convertions and imports for you like we needed so I searched the web to find out that everyone else who has the same problem as I did, import it manually! Man, I can't imagine importing over a 1000 songs manually by copy & paste! 8-0
So I stumbled on AutoIT. This is a wonderful util to automate just about any task. By writing a BASIC script (pun intended) you can tell the mouse pointer to move to a certain window's control and wait for a while and then issue a click action. Now that's what I mean by GUI automation ;-). You can also do basic scripting automation like a BATCH or BASH script can.
Now back to my scenario... I played around and found how easy it actually was to write a script that can import all my hundreds of songs in minutes (it literally took just over 20 mins to import over 800 songs!) since there wasn't a way for me to to it programatically in EW. This script is tested on EW 2006, so if you want to do the same kind of thing like I did, maybe your fortunate to have Google'd onto this page :D
#cs
A little script to import songs into Easy Worship 2006.
Written by Dandre Jansen van Vuuren
#ce
;Select the files to import
$var = FileProcessSelectionDialog()
;Cancelled
If $var == -1 Then
MsgBox(0, "Error", "No files selected, quitting...")
Exit (1)
EndIf
$selectedSourceFolder = False
$sourceFolder = $var[1]
StartEasyWorship()
;Iterate through all the selected files to import
$total = Number($var[0])
For $i = 2 To $total
$curFile = $var[$i]
SelectNewSong()
OpenFileOpenDialog()
$ext = MapFileTypeToTypeName(GetExtensionFromFile($curFile))
SelectAFileFromOpenDialog($curFile, $ext)
WinWaitActive("New Song")
Send("!o")
WinWaitActive("EasyWorship")
Next
MsgBox(0, "Progress", "Finished.")
Func FileProcessSelectionDialog()
$filter = "HTML Files (*.html;*.htm)|Word 97-2002 (*.doc)|Text Files (*.txt)"
$importFiles = FileOpenDialog("Select the files to import into EasyWorship", "", $filter, 1 + 4)
If @error Then
return -1
Else
If StringInStr($importFiles, "|") Then
return StringSplit($importFiles, "|")
Else
;A single selected file isn't in the same form as a multiple
;selection of files.
$pos = StringLastIndexOf($importFiles, "\")
$folder = StringLeft($importFiles, $pos)
$file = StringMid($importFiles, $pos + 1)
$newString = $folder & "|" & $file
return StringSplit($newString, "|")
EndIf
EndIf
EndFunc
Func StartEasyWorship()
Run("C:\Program Files\Softouch\EasyWorship\EasyWorship.exe")
WinWaitActive("EasyWorship")
EndFunc
Func SelectNewSong()
Send("!s")
Send("n")
WinWaitActive("New Song")
EndFunc
Func OpenFileOpenDialog()
;The open button doesn't have a shortcut
;find out where the window is positioned And
;relatively position the mouse onto the open button
$winPos = WinGetPos("New Song")
MouseMove($winPos[0] + 305, $winPos[1] + 45)
MouseClick("left")
WinWaitActive("Open")
EndFunc
Func SelectAFileFromOpenDialog($file, $type)
Send("!t")
ControlCommand("Open", "", "ComboBox3", "SelectString" , $type)
sleep(10)
Send("!n")
Sleep(10)
;We only need to specify the folder where all the files
;lie, once. From there on we can import the files one
;at a time.
If $selectedSourceFolder == False Then
Send($sourceFolder)
Send("{Enter}")
Sleep(10)
$selectedSourceFolder = True
EndIf
Send($file)
Send("!o")
EndFunc
Func GetExtensionFromFile($filename)
Return StringMid($filename, StringLastIndexOf($filename, ".") + 1)
EndFunc
Func StringLastIndexOf($string, $char_target)
$strlen = StringLen($string)
For $i = $strlen to 1 Step -1
$char = StringMid($string, $i, 1)
If $char == $char_target Then
return $i
EndIf
Next
EndFunc
Func MapFileTypeToTypeName($fileType)
Switch StringLower($fileType)
Case "html"
return "HTML Files"
Case "htm"
return "HTML Files"
Case "doc"
return "Word 97-2002"
Case "txt"
return "Recover Text from Any File"
Default
return "Unknown"
EndSwitch
EndFunc
Here is how the script works (you obviously need AutoIT and EW):
I don't mean to be full of myself (really I don't!) but I am glad that God made me a programmer so that I wouldn't have to do these kind of tasks manually! :D
Posted by LA at 4:07 PM 0 comments
Keywords: AutoIT,automation,productivity,scripting,software,windows
Monday, April 28, 2008
New (K/X)Ubuntu 8.04
This weekend I've spent some of my soft-cap downloads on getting the latest Ubuntu software (well I didn't get Ubuntu itself as I am not such a fan of Gnome) such as Xubuntu and Kubuntu, version 8.04 of course...
After leaving my Linux box overnight I managed to download Xubuntu (i386 - Desktop); Kubuntu (KDE 3.5; i386 - Desktop) and its KDE4 version (also i386 and currently as I'm typing downloading the 64bit version at 6Kbps ;-) ).
After obtaining Xubuntu, I ran a CD check and saw that 1 file was 'corrupt' (as in MD5 values didn't match). So I downloaded it again only to find the same problem, but when I did a MD5 hash on the ISO file this time and on the Xubuntu website, it was the same, so I guess the guys @ Xubuntu need to do a fix on that file but unfortunately I don't know if it is a text file or binary file (hopefully a non-important one).
So while I was waiting to download the KDE 4, Kubuntu, I installed the KDE 3 version of Kubuntu and I was quite impressed. I am not such a big fan of KDE 3 either but in Kubuntu 8, it sort of had a slight of change to what I was used to. The speed at which things operated, the fact that it quickly picked up all my hardware was great. All that the Ubuntu Linux OSs need is an official nice looking GRUB loader (but apparently one can get one with Apt-Get). Kubuntu even asked me if it should download the nVidia drivers for my 6200 card (yeah I know but I don't play games so actively anymore plus its sufficient for what I'm doing) and voila!
I even had a whack at the special desktop effects and its lookin good except for the fact that it is unstable :(
Then when the KDE 4 version was ready I immediately installed it over the KDE 3 version and working and seeing KDE 4 for the first time in action was just WOW! Visuals, speed, configuration, etc. was just great. I downloaded the nVidia driver so that I could have a crack at the desktop visuals and it looked ok'ish, besides the fact that it too was unstable. Like the OpenGL version would crash almost instantly while the RenderX (I think thats what it is called) works pretty well but doing something like pressing Ctrl+10/11 (to have your windows minimize on the screen as on the Mac OSX desktop) would only show 1 window instead of the 3/4 thats open. Other than that I am quite impressed with KDE 4 and the new Kubuntu.
Xubuntu I haven't had a go at yet because I would like to use its fast desktop as my new Linux Gateway over OpenSuSE and/or (K)Ubuntu (as SuSE 10's repository is outdated and no longer maintained and I need updates plus I am getting a little tired of SuSE, and the latest SuSE versions need higher spec machines to run on), so I am still fiddling around on a proof of concept (POC) before man handling my Gateway box ;-). I would like to still have a Remote desktop option when working on the box so thats why I am not using Ubuntu Server and Xubuntu is nice and light weight (maybe even for VNC sessions) so to update is no problem as opposed to SuSE. The only things I am going to miss is the remote dial-up with smpppd (SuseMetaPPP-Daemon), and a few ease of installation GUI screens (maybe even the preconfigured firewall) but I guess thats it, I'll see how much I can duplicate on my POC version before heading over. Also I really hope the guys @ Ubuntu fixed one routing bug where HTTPS web pages didn't masquerade over the local network. This was a common issue on version 7.10 (as I had Ubuntu Server 7.10), I also tried the fix on the website which didn't work for my situation.
Well thats all I got for now, didn't spend too much time on this but for those who've waited for the new Ubuntu can be happy at the progress made by the Ubuntu guys for sure.
Thanks Mark Shuttleworth (and your cool team, also to everyone who contributed) for such great Distros! If I can get this version as a new replacement on my gateway box, then I am a Ubuntu convert! ;-) It is faster to download 700MB than 3.5GB for newer versions! :D
Posted by LA at 2:36 PM 0 comments
Tuesday, April 15, 2008
Windows Explorer alternative
For those who're tired of Windows Explorer, a very nice free alternative I found does what I want it to and more...
There are a wide variety of Explorer replacements (or alternatives) that are "better" than Windows Explorer but this one I believe beats all of them in a lot of categories.
Ever heard of UltraExplorer?
Its a Delphi project (free for windows) and most people believe that Delphi GUI apps perform faster (and there is no exception in this case), although sometimes it might just stall for a while but I think it might be more of an IO stall than a "I'm too busy to listen to you" stall. ;-)
Its got a nice appearance, with skinable components (menu and toolbar). It has views that are actually customizable and very useful (except its console, that needs some work but you can get a very nice console app from Sourceforge as alternative to Windows').
You have tabbed windows support that works just life Firefox's tabs (even with the middle click event). It remembers where it was and has all the Windows Explorer context menus, and so on.
It even picks up my USB drives instantly as soon as Windows activates them and is very responsive except maybe during heavy disk IO.
I am definitely sticking to this handy tool for Windows, I even removed 'My Computer' and 'My Documents' from my desktop to replace it with 'Ultra Explorer'.
Hope you might like it. :)
God Bless.
Posted by LA at 9:01 AM 0 comments
Monday, March 31, 2008
Ruby, ruby, ruby ...
Yeah it's been a while since the last posting. I was lacking some inspiration on what to blog on or I was just too lazy or too busy to actually log in and blog on something that I wanted to, that is until now...
You might ask, whats up with the title? Well I remember the song written by a band called Keizer Chiefs (spelling) named: Ruby. Now that reminds me of the Ruby scripting language which lots of people on the web is going on about. A dynamic typed language with multiple ways of saying one thing.
I was among those who agreed that Ruby was stupid, until I wanted to do a few things in Windows which Python nor other scripting languages could offer (or easily accomplish), except Ruby. Some of us would mock the guy who presented a brief crash course in Ruby, saying that it is too dynamic for our taste and it would make debugging a nightmare, etc. but doing some code delving and with the latest Netbeans language addition, which is Ruby and Ruby on Rails of course, I started to see how cool Ruby actually is.
Usually I despised dynamic typed languages until I saw how it can increase productivity and coding speed for smallish scripts, which is what I wanted for automation.
"What kind of automation?" you may ask. Well ordinary and OLE automation. Sure AutoIT is great for GUI and other windows automation but the scripting language is very BASIC (pun intended) lacking structure and (as in, I need structures as in C or C++'s structures or atleast...) object orientation. With Ruby we have good OO support and with the 'win32ole' lib, you can kick VBA and do MS Office automation via Ruby instead. A good starting site is Ruby on Windows which covers Ruby automation in MS Office. I managed to convert Powerpoint slides to somewhat formatted Word docs via Ruby, just to give you an idea.
I think I'll post a couple of script snippets later on, on what I worked on with Ruby and AutoIT for others to see and for future reference.
Just to go back to Python, some of you might say that Python does have OLE automation... I had a look at it and it looked very nasty! I wouldn't touch it, unless I looked at the wrong files, and you need to download a library for Python to have OLE automation ability. With Ruby you use the OLE objects exactly as in VBA but within your Ruby context.
Currently I am trying to implement a few operations in Ruby to have Browse-For-Folder ability, Open-Dialog, and so on functionality by doing Windows API calls via the DL lib in Ruby. You can catch a quick course on DL here.
I hope this will help you as much as it helped me.
God Bless!
Posted by LA at 8:55 PM 1 comments
Keywords: automation,my projects,productivity,programming,ruby,scripting,software,tasks,windows




