Sunday, June 30, 2013

2. Batch Files, Redirecting Input/Output, Command Lines Arguments to batch file



 Redirection operators/ Redirecting Input/Output
 < operator means take input from whatever follows e.g. sort < file.dat
 > redirects the output
 >> appends the output to a file
 |  is called pipe, gives output of one command as input to the next command. e.g. 

type  abc.txt | more   
would take output form the type command and will give it to the more command



Standard Input/Output/Errors

-When system boots up, default input is keyboard, default output is display screen. But its important to notice there is one more type of entity called the standard 'errors'. By default errors are directed to output.  
- Symbol ">" directs the standard-out but not the error. So if a command has error even though you are using > operator output wont go to file.
Try this, make sure file1.txt exists.
dir file1.txt > out.txt

Now type a file name which does not exists
dir file_not_found.txt > out.txt 

In this case you will still see some message "File Not found" printed on screen. That is error being directed to standard error.

In DOS numbers are assigned to all input/output/errors as below:
0 = standard input
1 = standard out
2 = standard Error

I you want to direct standard errors to a file you can do this
2>&1

You can try "File Not found " again with below command


dir file_not_found.txt > out.txt 2>&1

 This time you would notice that "File Not Found" message is pushed to out.txt. Hence you have successfully directed standard error to a standard out, which is already directed to a file.



 Command Line Arguments to a Batch File
You can always provide command line argument to batch file
mybat.bat Hello 1 2 
 if your provide parameters to the batch file, in side batch file %1 represent first argument %2 second etc. 
in mybat.bat you can do following  

type %1  :: would print Hello
type %2   :: would print 1 etc.

1. Batch Files Tutorial for a C/C++ Programmer, Introduction, Batch File Comments, Commenting Multiple lines

In next few posts I would scribe about basics of batch files. The post heading :  "Batch Files Tutorial for a C/C++ Programmer", I have  chosen because I have presume that reader is aware of basic of programming terminologies.

When I type something in blue, consider that a batch file content.

Here we go.

Commands, Comments
-Each line in batch file is a command, A command ends with line feed character(Enter) 
e.g All of below are valid batch file commands

dir
md myDir

-You can comment a line by preceding a line by "::"
-You can also precede a line by "REM" also to comment a line
TIP: In notepad++, you can select multiple lines and press Ctrl+Q, it will comments all of those lines

::This is a comment
REM This is also a comment



Line is too Long what to Do
One line is batch file is interpretative as one command. But, incare lines gets too lengthy you can use ^ to break it to multiple lines

::Multi Line command, creates a directory named MyDir1
md ^
MyDir1


- DOS gives you some to you, e.g md, dir etc. You are also allowed to call other executables from
batch file. But make sure the program directory is present in path variable

- When you type any exe, DOS searches all the directories present in PATH. If DOS finds it, command is executed, otherwise command not found found message is dispalyed.

-If you want to avoid searching of these exes, you can give absolute path of exe

c:\Dir1\Dir2\myExe  


Saturday, June 29, 2013

Batch File to Create a timestamp Named File

I came across a situation where I needed to copy FW images repeatably to a shared location. It didn't take me long to realize that this must be automated. And, when I build FW, file should automatically be copied to the shared location.

Here is the batch file which I wrote to do so.

It takes a file from your working directory and copies to shared server location.
But additionally, it adds time stamp suffix to newly created file.











This batch file can be fetched from here.

I have also scribed about batch files . I think this is bare minimum must have knowledge for any C programmer.