Tuesday, November 5, 2013

5. Schedule a batch File/ Run a task at particular time



Batch files can be scheduled to run at particular time/ time frequency you want. Here is how it’s done.

Say you want to run batch file named “Test.bat”. 
Test.bat  opens a Microsoft word document named “AI.docx”. 
It contains following syntax.

start "c:\Program Files (x86)\Microsoft Office\Office14\winword.exe" C:\Users\Test_User\Documents\Curr\AI.docx

So you have batch file ready.

How to schedule:
1)      From windows search “Task Scheduler” and open it. 
2)      Click on “Task Scheduler Library” located at left of screen. Don’t forget this step. I had spent almost 30 mins to figure out where my created tasks are going.
3)      From Action menu click on Create task. Follow the self-explanatory tabs from there on.
4)      In Trigger tab you can give when your batch file would run. In Action Tab you have to give your batch file path.
5)      In Condition tab. Do not forget to Uncheck “Run task only on AC power option” . 

And that’s it, document AI.docx would open now at scheduled time.


Tuesday, September 17, 2013

4. Batch Files - All about echo, How to echo without newline



ECHO
One of the most frequently used command is echo
Echo can be used in 3 ways
Echo  :: will tell you echo is on or off 
echo Hello, world!  :: prints message on screen
@echo :: @ makes sure this line does not get echoed
 ::most common use of this is to turn echo off, otherwise all the commands would get printed on the screen, with echo off, only the stuff you choose to echo will get displayed
@echo off           

Echo without newline character
You will notice that whenever you use echo command, it append a new line character in the end.
Believe me, pretty soon after you start to write batch files, you would want to do echo without new line. Here is the solution for you.

Suppose you want to echo "Hello, World" without new line

echo | set /p dummyVar="Hello, World"
::Similarly if you have variable you, can echo that as well
set var1="Hello, World 2!"
echo | set /p dummyVar=%var1% 


There is a exe called "clip" which can be used to copy stuff to clipboard. you can use. This would copy "Hello, World" to clipboard without any newline character

echo | set /p dummyVar="Hello, World" | clip 



Circular Buffer - Find distance between head and Tail


Problem is to find distance between head and tail in a circular buffer, where head is always ahead of tail. But at any point, head or tail may roll over to 0th location.
Assume that we are rolling over after uint8 i.e 255 entries.
Simple case is like this. Head = 10 Tail =4.
Distance = 10- 4 =6
But say Head is at 4 and tail is at 253.
Distance = 4 – 254 = wont work out (head rolled back to zero after 255)

There is a very simple solution to this problem. .
Formula would be:
 
Distance = Head + 2’s complement of tail

e.g.
0000 1010 (10)
+
­1111 1100 (2’s complement of 4)
========================
0000 0110 (6)

Lets take other example
0000 0100 (4)
+
0000 0010 (2st complement of 254)
===========================
0000 0110 (6)

Monday, July 22, 2013

Command Prompt - Copy/Paste wihout mouse

Windows command prompt - short cuts to copy/paste

Alt + space + e + k  -> will copy the content
Alt + space + e + p  -> Will paste the clipboard content.

Monday, July 1, 2013

3. Batch File, How to declare Variables, Using FOR loops in batch file


How to declare a variable in batch file
Answer is to use set command.

Set command has 3 options
set     => Declares a string variable
set /a  => declares an integer variable
set /p  => Gets the input from the user

When you want to refer a variable use %var%. e..g
 
 ::Declaring a variable, declare a variable using set  
 ::remember not put spaces around "=" while assigning values to the variable  
 set var1=hello, World!  
 echo %var1%  
 set var2= This is NeoTheSaviour  
 echo %var1%%var2%  




How to declare integer variables


 ::When you set any variable by default its a string, as you have seen in above example  
 ::set has 2 other options, 1) /a which interprate given input as integer  
 set /a var1=10  
 set /a var2=5  
 set /a var3=%var1%+%var2%  
 echo Sum 0f %var1% and %var2% is %var3%  
 set /a var3=%var1%*%var2%  
 echo Product is %var3%  






Taking input from user



 ::second option set has to take input from User /p  
 echo Addition of Two numbers  
 set /p var1=Input First Integer   
 set /p var2=Input Second Integer   
 set /a var3=%var1%+%var2%  
 echo Sum is %var3%  




You can download above batch file from here


For Loops
Syntax is like below. Note that we use %% while referring iterator.

 ::For loops and variable  
 :: For loop uses iterator with %%  
 for %%a in (a b c d) do echo %%a  
 ::second way of for loop  
 FOR /L %%i IN (0,1,5) do (  
      echo %%i  
      )  



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.