The Blog of a Programmer
Archive for July, 2006
StumbleUpon
Jul 19th
Go there and stumble, stumble long, stumble hard.
Their advertising program is damn nice too!
The XML Document Object Model
Jul 15th
I’m writing a bunch of functions that communicate with Amazon using classic asp, it requires me to learn much more about the XMLDOM, so that I don’t manually parse through every XML file. For anyone else who is interested in the XMLDOM I recommend the article, “XML“, over at Dev Articles who, by the way, tend to produce many many very helpful and articulate articles written in a manner most people can either understand or learn to understand.
JJ
How To Do a Search Through Folders – Two Levels Deep
Jul 14th
I used the following code to search through my “galleries” folder and search for images through every folder inside galleries sub-folders.
Huh?
I used each folder inside of galleries as the “category” for each set of images. What it ended up doing was this. Each time I wanted to create a new category I would just make a new folder inside of “galleries”, each time I wanted a new album I’d put another folder inside each category folder. For example I could have a folder structure as follows:
/galleries
—/wedding
——-/getting_ready
——-/court_house
——-/reception
—/first_home
——-/before
——-/after
As you can see my categories are under galleries and they are called “wedding” and “first_home”. My galleries are called “getting_ready”, “court_house”, “reception”, “before” and “after”.
Oh I see!
I’m not going to be releasing the code to the entire program because an ex co-worker of mine and I plan on selling a gallery solution based on this, but here is the code for the folder searching! It is possible to modify this to do a full recursive search, I’ve done it before. Try creating another folder search function, while building the folder path you are passing into the functions as you go. It’s possible to build a recursive output that will show every file in an entire site directory, if you set root_folder = “/”
The Code
root_folder = “/galleries/”
SearchFolders root_folder
Sub SearchFolders(root_folder)
set FileSysObj=CreateObject(“Scripting.FileSystemObject”)
strFullPath = server.mappath(root_folder)
set fldr=FileSysObj.GetFolder(strFullPath)
set FolderList = fldr.SubFolders
For Each FolderIndex in FolderList
Response.Write(FolderIndex.name & ”
“)
Next
set FileList = fldr.Files
For Each FileIndex in FileList
Response.Write(FileIndex.name & ”
“)
Next
End Sub
Sub DisplaySubFolders(root_folder, parent_folder)
set FileSysObj=CreateObject(“Scripting.FileSystemObject”)
strFullPath = server.mappath(root_folder)
set fldr=FileSysObj.GetFolder(strFullPath)
set FolderList = fldr.SubFolders
For Each FolderIndex in FolderList
Response.Write(FolderIndex.name & ”
“)
Next
set FileList = fldr.Files
For Each FileIndex in FileList
Response.Write(FileIndex.name & ”
“)
Next
End Sub
Enjoy,
JJ
API’s Gone Wild
Jul 13th
Google, Amazon, Ebay
For all those developers interested in making some seriously interesting or possibly revenue generating programs the google, amazon, and ebay api’s may be a good place to start. I’ve used them in the past always to make something small for a client.
Now it’s my turn, and although the programs I would like to make may be small they’ll be a great learning experience and my own! It’s just that special little feeling of accomplishment when that first XML transaction goes through and you recieve something other than an error code! Ah, yes that’s a good feeling indeed!
So yeah I went a little overboard today and signed up for every major API at the three.
If you’re interested take a look!
Google Maps API (This one is just fun.)
Google Ajax Search API (Might be interesting.)
Google Adwords API (Maybe make some money off this.)
Google Checkout API (Sign up for sandbox if developing. Looks great in my opinion.)
Amazon Web Services (You can make some interesting things out of this!)
Ebay API (Hmmm webservices with ebay… definately could build something interesting!)
Enjoy!
Automating FTP
Jul 12th
The Problem
I need to automate an FTP transaction. No big deal right? I need to automate my FTP transaction from inside a .net console program, then execute the same program with multiple instances that process each downloaded file.
Shell()I’m really loving this command. It’s nothing new really but for some reason or another it tends not to be common knowledge. I suppose the land of DOS and those wonderful black and white (although sometimes colorful) programs has been left slowly behind by most newer developers.
The Answer
Technically I already told you the answer to my problem, but I didn’t exactly explain it in full. Here is the code, I’ll explain it more later.
Shell(“ftp -s:ftp.script”)
Isn’t that just great!? It turns out that the normal MS-DOS FTP command already has a basic scripting control in it. So using Shell() to execute ftp, which is in my system PATH, and passing the -s:filename argument into it the ftp. The program then opens your script and uses it to answer each of the prompts it would normally give. My FTP script is very simple and here is a copy.
prompt
open ftp.myserver.com
USERNAME
PASSWORD
cd directory
mget *.txt
bye
Here is a quick breakdown of the script.
prompt: is an FTP program command which disables or enables the interactive prompt
I want this disabled so that I can automatically overwrite files, and accept file downloads.
open: standard open command, set the server IP or server name here
USERNAME: the server always asks for a username – set it here by replacing USERNAME with the real username
PASSWORD: replace it with your password
Repeat your password
cd: change directory – usage: cd /directory/to/work/in/
mget: multiple get – wildcards can be used for filename and extension
Example:
the*developer.txt would match:
thecrazydeveloper.txt, themaddeveloper.txt, and so on.
*.txt matches all files with .txt in the filename
*.* matches all files in the directory
bye: logs you out of the server
Enjoy!
Visual Basic.net Console Applications
Jul 11th
Microsoft Adds Improved Support for Console Apps in .Net
When I learned that .net had improved support for console apps I became immediately excited about the whole thing. I love data processing, parsing, and conversion; I’m a geek in all every possible sense of the word. It can be a true mental challenge to convert something one company believes to be a complete and sensible data format to what another company believes to be the same.
In this past week I came across the need to build a program which will be capable of automation and quickly converting one companies file format to another.
O’ The Possibilities
Since I am mostly a web based developer my immediate thought was VBScript (Classic ASP) hosted on an IIS capable server on which we would have a batch calling ftp to download the files which needed to be parsed, saving them to a specific folder, and executing the .asp script.
Unfortunately the server which would be running the program (decided on by the client company) does not run IIS, so there goes that idea. Another idea was to run it as a .vbs script using windows internal scripting. I’ve always had a bad feeling about using those files and decided that would not be a grand idea. Out of a gut feeling I scratched that idea.
What about .net?
I had worked with VB.net only a few times before and thought it would be easy enough to do the job with. What about automation? Having a form without need of human intervention would allow the program to run by itself BUT why do I even need that? It would make the program larger and that is completely unnecessary. The next thought that came to my mind was, “I miss DOS.”
DOS and Console Applications!
Most of you past the age of 20 or so should remember console apps. Those DOS console windows, the constant use of “cmd” or “command” from your start —> run menu, and (for those of us who like to play those old dos games) free dos or DOSBox! Either way, those wonderful apps didn’t just become useless or out of style with the advent of easily built apps with nifty GUI’s!
Console apps are great for processing and well automating processes! Which is how I am using my newest little application.
How to Write Console Apps with .Net 2003Okay first off, I know .net 2005 is out. I’m currently using .net visual studio 2003, and although the process should be similar in 2005, perhaps even better; I’m using 2003 and that’s what you’ll learn here. That is, until I get upgraded.So open up your .net enviroment.
Create a new project selecting Visual Basic Projects (left hand side), and Console Application (right hand side). Name your application and have it save to whatever location you’d like.
Module1.vbIf you are anything like me you’ll love the fact that the first thing you see after creating this new application is a code window! None of the GUI or form clutter. Straight to the heart of the matter! Now unless your application is completely static, as in it always processes the same file(s), you’ll need your program to handle arguments. In my case it needs to handle 1 argument and that would be the file name of the text file to be parsed.Handling Arguments
Sub Main()
Main(System.Environment.GetCommandLineArgs())
End Sub
Wonderful little piece of code! Since this is the first function executed in your program all it will do is collect the arguments from the command line and pass them into a private sub also called Main in your application, in which you can check for the correct arguments and actually do your processing! Here is a basic example:
Private Sub Main(ByVal arguments() As String)
If arguments Is Nothing OrElse arguments.Length <> 2 Then
Console.WriteLine(“You must enter atleast one argument for this program to execute!”)
Else
System.Console.WriteLine(“–==Welcome to My Console Application==–”)
If (arguments(1) = “-V”) Then
System.Console.WriteLine(“–==Written by JuanJose H. Galvez==–”)
Exit Sub
Else
Dim user_name
System.Console.WriteLine(“What is your name?”)
user_name = System.Console.ReadLine()
System.Console.WriteLine(“Your name is: ” & user_name)
End If
End If
End Sub
From the above example you should be able to figure out how to initiate variables, set user input into a program usable variable, how to write out strings to the screen! Also, you’ll either run into this or figure it out yourself but arguments(0) is actually equal to the filename of the program, which is why the argument(1) is used when seeking the input argument used to run the program.
Example: “ConsoleApplication1.exe” -V
Example: “ConsoleApplication1.exe” AnythingCanGoHere
This is obviously a useless program!
You’ve wasted my time!Actually, this little program was indeed a useless example, in that it does nothing of use; thus the use of the term useless; but it does end up teaching someone who is new to console applications how to get started. It also teaches the basic concepts behind arguments and how to do basic input output operations between the program and the end user.And you wasted your own time reading this! I wasted time writing! :)
Recent Comments