Sunday, April 8, 2012

Scripting Batch File: Split String


Its been a very long time that I have posted something. I was creating certain batch files and came across the requirement of splitting strings and searching for specific strings. I thought of sharing the same to all, so that one can refer and get help in case one needs.

In order to split a string, one can use the FOR loop. The syntax of the command that one may use to split a string is as follows:

FOR /F "" %% IN ("") DO (statement/s)

Set of arguments to be used for splitting a string can be any combination of the below:
  • delims=xxx : specifies a delimiter set.  The default delimiters are space and tab (if not specified).
  • tokens=x,y,m-n : specifies which tokens from each line are to be passed to the for body for each iteration. This will cause additional variable names to be allocated.  The m-n form is a range, specifying the mth through the nth tokens.  If the last character in the tokens= string is an asterisk, then an additional variable is allocated and receives the remaining text on the line after the last token parsed.
  • usebackq : specifies that the new semantics are in force, where a back quoted string (``) is executed as a command and a single quoted string ('') is a literal string command and allows the use of double quotes ("") to quote file names in file-set.


Lets look into some examples now:

a) FOR /F %a in ("Testing this is a very simple task") do @echo %a

The above command would print Testing as output. That means, if you do not specify any argument, it would split the variable and set the value of variable %a as the first fragment.

b) FOR /F "tokens=1-7" %a in ("Testing this is a very simple task") do @echo %a %f %g

The above command would print "Testing simple task" as the output. When we specify the above command with tokens=1-7, it would split the string for each Space and assign it to a variable starting from %a. In detail, the command would set variables with values as follows:
    %a = Testing
    %b = this
    %c = is
    %d = a
    %e = very
    %f = simple
    %g = task

In the above command, one can also set numerals as variables. i.e., FOR /F "tokens=1-7" %1 in ("Testing this is a very simple task") do @echo %1 %6 %7 is also a valid command and would return the same output.

c) Now we can use the "delims=" argument to specify any different delimiters based on which the string should be split. For example, the following command splits the string based on delimiter "," (comma).

    FOR /F "tokens=1-7 delims=," %a in ("Testing,this,is,a,very,simple,task") do @echo %a %f %g

Output of the command would be same as that in example b.

d) Now, if you want to get a command executed and then split the output string, then use the command string within single back quotes ( '' ). For example, the command

FOR /F "tokens=1-2 delims=." %a in ('dir /b') do @echo %a would return all the file names without their extension.

    or else,

one can force the new semantics and then insert the "usebackq" argument and specify the command within back-quotes (``). Example: FOR /F "usebackq tokens=1-2 delims=." %a in (`dir /b`) do @echo %a

e) If you want to parse through a text file and then split strings with delimiters present in the lines, use the command without any quotes. Example: FOR /F "tokens=1-5 delims=," %a in (a.txt) do @echo %a
   
or else,

one can force the new semantics and then insert the "usebackq" argument and define the file name within double-quotes (""). Example: FOR /F "usebackq tokens=1-5 delims=," %a in ("a.txt") do @echo %a

f) If one uses the new semantics by inserting the "usebackq" argument, and wants to split a string, then the string should be provided between single quotes (''). Example: FOR /F "usebackq tokens=1-5 delims= " %a in ('dir /b') do @echo %a

Friday, January 14, 2011

Microsoft Excel Chart Exporting

Someone in my company came across a requirement for exporting charts in Microsoft Excel. It was then that he came across some automation for the same. He got a tool for himself, but it was quite buggy and very small in features. It was then that I thought of creating one for myself and what I could see was that there are tools available in the market, but people have kept the code password protected and licensed. I thought of making an excel add-in and making the keeping the code open for use. 

I have used the “Sheets ().ChartObjects(i).Chart.Export” to export the charts in excel. Please download the OpenChartExporter add-in here. It is simple to use.

Saturday, December 11, 2010

Mixing multi-dimensional thoughts into Testing

I was thinking for a solution to the question what should be the team composition to cover the maximum possible test scenarios that can be thought of, while testing an application/product. It was then that the idea of different people testing the same application in different perspectives came into my mind.

Different people always have different personalities and the same leads them look into things in with a different mentality. Once when I asked a person involved in embedded testing whether he wanted to look for a job into the application testing, the guy furiously refused it. I have had a similar experience from another person involved in application testing, regarding website testing. Another incident, which I have come across many a time, is that people get adhered and addicted to the environment that they had been working for a certain time period. There has been many a time that I listened to people complaining about the new work places due to be using some management application, just because they have been comparing it with the application that they used in their earlier company.

What these made me understand is that people become biased to the type of work that they do. If a person is involved in testing a Visual C# Application for some time, he molds his thoughts to such a direction where all applications behave in the same way that the applications he had been working with. This is a common human mentality, because we always make things to become habits.

Now, when I thought of a method in which we combine the efforts of different people who have different skill levels and domain backgrounds, into testing an application along with some who have been testing the application, it let me make some sense. I thought putting together some people from different domains into one test team, which is testing a totally new product, could prove better than a team of common minds. The thoughts and principles that testers from different backgrounds follow are different and this brings in a lot of variety in testing, once these people come together as a team. If a team which is testing a gaming application includes telecom domain testers, web application testers, network testers etc., then the coverage of testing would be overwhelmingly better than one could imagine. Of course, one thing that is implicit is that, it needs each to be good in their respective fields.

I tried this idea practically into one of those projects that we undertook for an IPTV application. That experience boosted my confidence in this ideology to a great extent. We were only a group of two along with the original testers who were involved from the beginning. Both of us had experiences from the past which are of different backgrounds – one a web tester with experience with different domain accounting systems and the other, experienced in embedded telecom devices, banking applications etc. Together, we were able to test the application and make the product owner change the work-flow a couple of times. The main reason, now I understand, is that we, having expertise working in different domains, were able to create and execute diverse scenarios which were unknown to the other fellow testers. Although the product owner had to change his architecture couple of times, this helped him deliver a product in which his clients, with their own test team, couldn’t find a single bug. This resulted in increased confidence of the client on us, helping us to bag some more projects of the same kind.

There are certain things which need care, while we deal with teams including testers from different domains. It is the fact that the team selection needs to be very even; else it may lead to the testing getting diverted into a single way more and the application development getting affected. A good management should always be able to handle this, because this is common with all teams. I would like to try and implement it in a more projects. Mixing and applying multi-dimensional thoughts for testing an application could prove much more worthy than the usual strategies.

Wednesday, April 21, 2010

Network Testing Utilities and Functions with AutoIT

This is interesting to a very high level. I came across the networking utilities of AutoIT. The tool has a very good set of network related functions which could be made use in network testing. There are options for sending and receiving packets through AutoIT. Both TCP and UDP messages could be handled with the tool! Interesting, isn’t it?

One can do socket programming with this testing tool. Believe me, you can open a socket, send and receive TCP/UDP packets, Listen on a specific port, do messaging, etc. with AutoIT. If one has the idea, then creating a server is not so tough with this tool! Below listed are the functions which are available with AutoIT for playing around with the Messages. These are other than what I have mentioned in the earlier post, which talked about FTP programming.
  • TCPStartup – To Start TCP services
  • TCPConnect – To create a socket and connect to the specified server
  • TCPSend – Send TCP data on a connected socket
  • TCPListen – Creates and starts listening on a socket for incoming connections
  • TCPAccept – Permits an incoming connection on a listening socket
  • TCPRecv – Receives data from a connected socket
  • TCPNameToIP – To convert internet name into the IP address
  • TCPCloseSocket – Closes a TCP socket
  • TCPShutdown – Shuts down the TCP services on the machine
  • UDPStartup – To start the UDP services
  • UDPOpen – Opens and connects a UDP socket to a server
  • UDPBind – Creates a socket bound to an incoming connection
  • UDPSend – Send UDP data on a connected socket
  • UDPRecv – Receives UDP data on a connected socket
  • UDPCloseSocket – Closes the UDP socket
  • UDPShutdown – Shuts down the UDP services on the machine
For explanatory purpose, I have created an example of PC-to-PC Messaging system utilizing these functions. It utilizes the TCP messaging functions. There are two codes which I have created, of which one acts as the server and the second as client. The client establishes connection with the server and gets authenticated with a welcome message. There onwards, messages could be exchanged between these two softwares. This is very much similar to a chat engine, and could in fact be used as such, with some little modifications. Please find the executable here. I have also uploaded the Source code, which could be downloaded here.

I will come up with more examples and more on automated testing of UDP based protocols like TFTP. It is really easy and handy to use AutoIT, because it is a very simple function call which needs to be made for all these.

Friday, March 5, 2010

VBScript Type Conversion

It's very often for people who are interested in VBScripting to encounter the problem of type conversion from string to number. There is no predefined method that could be found to convert the string variable type to numeric.