Showing posts with label BatchFiles. Show all posts
Showing posts with label BatchFiles. Show all posts

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 



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.

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.